-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support dynamically loaded custom components (#804)
- Loading branch information
1 parent
a7336fc
commit 22049d5
Showing
15 changed files
with
311 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
To build a custom component: | ||
|
||
First we need to replace some of our dependencies with a shim (ensure shim/ is copied | ||
to your project directory). | ||
In package.json, update the dependencies which are available in the shim/ directory: | ||
|
||
"react": "file:./shim/react", | ||
"react-admin": "file:./shim/react-admin", | ||
"react-dom": "file:./shim/react-dom", | ||
|
||
Also repeat these in a 'resolutions' config: | ||
|
||
"resolutions": { | ||
"react": "file:./shim/react", | ||
"react-admin": "file:./shim/react-admin", | ||
"react-dom": "file:./shim/react-dom", | ||
"react-router-dom": "file:./shim/react-router-dom", | ||
"query-string": "file:./shim/query-string" | ||
}, | ||
|
||
Using the shim for atleast react-admin is required, otherwise the components will | ||
end up using different contexts to the application and will fail to function. | ||
Using the shim for other libraries is recommended as it will significantly reduce | ||
the size of your compiled module. | ||
|
||
|
||
|
||
Second, we need to ensure that it is built as an ES6 module. To achieve this, add | ||
craco to the dependencies: | ||
|
||
"@craco/craco": "^7.1.0", | ||
|
||
Then create a craco.config.js file: | ||
|
||
module.exports = { | ||
webpack: { | ||
configure: { | ||
output: { | ||
library: { | ||
type: "module" | ||
} | ||
}, | ||
experiments: {outputModule: true} | ||
} | ||
} | ||
} | ||
|
||
And replace `react-scripts` with `craco` in the 'scripts' config: | ||
|
||
"scripts": { | ||
"start": "craco start", | ||
"build": "craco build", | ||
"test": "craco test", | ||
"eject": "craco eject" | ||
}, | ||
|
||
|
||
Then the components can be built as normal: | ||
|
||
yarn install | ||
yarn build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
webpack: { | ||
configure: { | ||
output: { | ||
library: { | ||
type: 'module' | ||
} | ||
}, | ||
experiments: {outputModule: true} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "admin-js", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@craco/craco": "^7.1.0", | ||
"@emotion/react": "^11.11.1", | ||
"@emotion/styled": "^11.11.0", | ||
"@mui/icons-material": "^5.14.14", | ||
"@mui/material": "^5.14.14", | ||
"@testing-library/jest-dom": "^5.14.1", | ||
"@testing-library/react": "^13.0.0", | ||
"@testing-library/user-event": "^13.2.1", | ||
"query-string": "file:./shim/query-string", | ||
"react": "file:./shim/react", | ||
"react-admin": "file:./shim/react-admin", | ||
"react-dom": "file:./shim/react-dom", | ||
"react-router-dom": "file:./shim/react-router-dom", | ||
"react-scripts": "5.0.1", | ||
"web-vitals": "^2.1.0" | ||
}, | ||
"resolutions": { | ||
"react": "file:./shim/react", | ||
"react-admin": "file:./shim/react-admin", | ||
"react-dom": "file:./shim/react-dom", | ||
"react-router-dom": "file:./shim/react-router-dom", | ||
"query-string": "file:./shim/query-string" | ||
}, | ||
"scripts": { | ||
"start": "craco start", | ||
"build": "craco build && (rm ../static/*.js.map || true) && mv build/static/js/main.*.js ../static/admin.js && mv build/static/js/main.*.js.map ../static/ && rm -rf build/", | ||
"test": "craco test", | ||
"eject": "craco eject" | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = window.QueryString; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = window.ReactAdmin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = window.ReactDOM; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = window.ReactRouterDOM; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = window.React; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = window.ReactJSXRuntime; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { memo } from 'react'; | ||
import Queue from '@mui/icons-material/Queue'; | ||
import { Link } from 'react-router-dom'; | ||
import { stringify } from 'query-string'; | ||
import { useResourceContext, useRecordContext, useCreatePath, Button } from 'react-admin'; | ||
|
||
export const CustomCloneButton = (props: CloneButtonProps) => { | ||
const { | ||
label = 'CUSTOM CLONE', | ||
scrollToTop = true, | ||
icon = defaultIcon, | ||
...rest | ||
} = props; | ||
const resource = useResourceContext(props); | ||
const record = useRecordContext(props); | ||
const createPath = useCreatePath(); | ||
const pathname = createPath({ resource, type: 'create' }); | ||
return ( | ||
<Button | ||
component={Link} | ||
to={ | ||
record | ||
? { | ||
pathname, | ||
search: stringify({ | ||
source: JSON.stringify(omitId(record)), | ||
}), | ||
state: { _scrollToTop: scrollToTop }, | ||
} | ||
: pathname | ||
} | ||
label={label} | ||
onClick={stopPropagation} | ||
{...sanitizeRestProps(rest)} | ||
> | ||
{icon} | ||
</Button> | ||
); | ||
}; | ||
|
||
const defaultIcon = <Queue />; | ||
|
||
const stopPropagation = e => e.stopPropagation(); | ||
|
||
const omitId = ({ id, ...rest }) => rest; | ||
|
||
const sanitizeRestProps = ({ | ||
resource, | ||
record, | ||
...rest | ||
}) => rest; | ||
|
||
export const components = {CustomCloneButton: memo(CustomCloneButton)}; |
Oops, something went wrong.