Skip to content

Commit

Permalink
Dynamic component #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Aug 8, 2023
1 parent 54d2a6f commit 43c134a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
16 changes: 14 additions & 2 deletions admin-js/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ import {
// Filters
email, maxLength, maxValue, minLength, minValue, regex, required,
// Misc
AutocompleteInput, EditButton, HttpError, WithRecord
AutocompleteInput, EditButton, HttpError, WithRecord,
// For custom components...
useCreatePath, useRecordContext, Button,
} from "react-admin";
import { createElement } from "react";
import VisibilityOffIcon from "@mui/icons-material/VisibilityOff";


import Queue from '@mui/icons-material/Queue';
import { Link } from 'react-router-dom';
import { stringify } from 'query-string';

// Hacked TimeField/TimeInput to actually work with times.
// TODO: Replace once new components are introduced using Temporal API.

Expand Down Expand Up @@ -66,7 +74,9 @@ const COMPONENTS = {
ReferenceOneField, SelectField, TextField, TimeField,

BooleanInput, DateInput, DateTimeInput, NumberInput, ReferenceInput, SelectInput,
TextInput, TimeInput
TextInput, TimeInput,

useRecordContext, useCreatePath, Button, Queue, Link, stringify, createElement
};
const FUNCTIONS = {email, maxLength, maxValue, minLength, minValue, regex, required};
const _body = document.querySelector("body");
Expand All @@ -77,6 +87,8 @@ if (STATE["js_module"]) {
// The inline comment skips the webpack import() and allows us to use the native
// browser's import() function. Needed to dynamically import a module.
MODULE_LOADER = import(/* webpackIgnore: true */ STATE["js_module"]).then((mod) => {
for (const k of Object.keys(mod.g))
mod.g[k] = COMPONENTS[k]
Object.assign(COMPONENTS, mod.components);
Object.assign(FUNCTIONS, mod.functions);
});
Expand Down
37 changes: 37 additions & 0 deletions examples/custom-clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const g = {"Queue": null, "Link": null, "stringify": null, "Button": null,
"createElement": null, "useRecordContext": null, "useCreatePath": null}

const CopyUSButton = (props) => {
const {
label = "Copy to US",
scrollToTop = true,
icon = g.createElement(g.Queue),
...rest
} = props;
const record = g.useRecordContext(props);
const createPath = g.useCreatePath();
const pathname = createPath({resource: "rhymes_us", type: "create"});
props = {
component: g.Link,
to: (
record
? {
pathname,
search: g.stringify({source: JSON.stringify(record)}),
state: {_scrollToTop: scrollToTop},
}
: pathname
),
label: label,
onClick: stopPropagation,
...sanitizeRestProps(rest)
};
return g.createElement(g.Button, props, icon);
};

// useful to prevent click bubbling in a datagrid with rowClick
const stopPropagation = e => e.stopPropagation();

const sanitizeRestProps = ({resource, record, ...rest}) => rest;

export const components = {CopyUSButton: CopyUSButton};

0 comments on commit 43c134a

Please sign in to comment.