forked from api-platform/admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShowGuesser.js
63 lines (53 loc) · 1.76 KB
/
ShowGuesser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React from 'react';
import PropTypes from 'prop-types';
import { Show, SimpleShowLayout } from 'react-admin';
import FieldGuesser from './FieldGuesser';
import Introspecter from './Introspecter';
import useMercureSubscription from './useMercureSubscription';
const displayOverrideCode = (schema, fields) => {
if (process.env.NODE_ENV === 'production') return;
let code =
'If you want to override at least one field, paste this content in the <ShowGuesser> component of your resource:\n\n';
code += `const ${schema.title}Show = props => (\n`;
code += ` <ShowGuesser {...props}>\n`;
fields.forEach((field) => {
code += ` <FieldGuesser source={"${field.name}"} addLabel={true} />\n`;
});
code += ` </ShowGuesser>\n`;
code += `);\n`;
code += `\n`;
code += `And don't forget update your <ResourceGuesser> component:\n`;
code += `<ResourceGuesser name={"${schema.name}"} show={${schema.title}Show} />`;
console.info(code);
};
export const IntrospectedShowGuesser = ({
fields,
readableFields,
writableFields,
schema,
schemaAnalyzer,
children,
...props
}) => {
let fieldChildren = children;
if (!fieldChildren) {
fieldChildren = readableFields.map((field) => (
<FieldGuesser key={field.name} source={field.name} addLabel={true} />
));
displayOverrideCode(schema, readableFields);
}
useMercureSubscription(props.resource, props.id);
return (
<Show {...props}>
<SimpleShowLayout>{fieldChildren}</SimpleShowLayout>
</Show>
);
};
const ShowGuesser = (props) => (
<Introspecter component={IntrospectedShowGuesser} {...props} />
);
ShowGuesser.propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
resource: PropTypes.string.isRequired,
};
export default ShowGuesser;