Simplified DZ component

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-10-23 15:38:32 +02:00
parent 430cb0dc11
commit 6e05185a54
4 changed files with 36 additions and 27 deletions

View File

@ -24,8 +24,6 @@ import Wrapper from './Wrapper';
/* eslint-disable react/no-array-index-key */ /* eslint-disable react/no-array-index-key */
const DynamicZone = ({ const DynamicZone = ({
max,
min,
name, name,
// Passed with the select function // Passed with the select function
@ -34,16 +32,20 @@ const DynamicZone = ({
isCreatingEntry, isCreatingEntry,
isFieldAllowed, isFieldAllowed,
isFieldReadable, isFieldReadable,
layout,
moveComponentUp, moveComponentUp,
moveComponentDown, moveComponentDown,
removeComponentFromDynamicZone, removeComponentFromDynamicZone,
dynamicDisplayedComponents, dynamicDisplayedComponents,
fieldSchema,
metadatas,
}) => { }) => {
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const { components } = useEditView(); const { components } = useEditView();
// We cannot use the default props here
const { max = Infinity, min = -Infinity } = fieldSchema;
const getDynamicComponentSchemaData = useCallback( const getDynamicComponentSchemaData = useCallback(
componentUid => { componentUid => {
const component = components.find(compo => compo.uid === componentUid); const component = components.find(compo => compo.uid === componentUid);
@ -73,12 +75,8 @@ const DynamicZone = ({
.map(key => formErrors[key]); .map(key => formErrors[key]);
}, [formErrors, name]); }, [formErrors, name]);
const dynamicZoneAvailableComponents = useMemo( const dynamicZoneAvailableComponents = useMemo(() => fieldSchema.components || [], [fieldSchema]);
() => get(layout, ['schema', 'attributes', name, 'components'], []),
[layout, name]
);
const metas = useMemo(() => get(layout, ['metadatas', name, 'edit'], {}), [layout, name]);
const dynamicDisplayedComponentsLength = dynamicDisplayedComponents.length; const dynamicDisplayedComponentsLength = dynamicDisplayedComponents.length;
const missingComponentNumber = min - dynamicDisplayedComponentsLength; const missingComponentNumber = min - dynamicDisplayedComponentsLength;
const hasError = dynamicZoneErrors.length > 0; const hasError = dynamicZoneErrors.length > 0;
@ -89,18 +87,20 @@ const DynamicZone = ({
const hasMaxError = const hasMaxError =
hasError && get(dynamicZoneErrors, [0, 'id'], '') === 'components.Input.error.validation.max'; hasError && get(dynamicZoneErrors, [0, 'id'], '') === 'components.Input.error.validation.max';
// TODO
if (!isFieldAllowed && isCreatingEntry) { if (!isFieldAllowed && isCreatingEntry) {
return ( return (
<BaselineAlignement> <BaselineAlignement>
<NotAllowedInput label={metas.label} spacerHeight="3px" /> <NotAllowedInput label={metadatas.label} spacerHeight="3px" />
</BaselineAlignement> </BaselineAlignement>
); );
} }
// TODO
if (!isFieldAllowed && !isFieldReadable && !isCreatingEntry) { if (!isFieldAllowed && !isFieldReadable && !isCreatingEntry) {
return ( return (
<BaselineAlignement> <BaselineAlignement>
<NotAllowedInput label={metas.label} spacerHeight="3px" /> <NotAllowedInput label={metadatas.label} spacerHeight="3px" />
</BaselineAlignement> </BaselineAlignement>
); );
} }
@ -109,8 +109,8 @@ const DynamicZone = ({
<DynamicZoneWrapper> <DynamicZoneWrapper>
{dynamicDisplayedComponentsLength > 0 && ( {dynamicDisplayedComponentsLength > 0 && (
<Label> <Label>
<p>{metas.label}</p> <p>{metadatas.label}</p>
<p>{metas.description}</p> <p>{metadatas.description}</p>
</Label> </Label>
)} )}
@ -198,7 +198,7 @@ const DynamicZone = ({
<div className="info"> <div className="info">
<FormattedMessage <FormattedMessage
id={`${pluginId}.components.DynamicZone.add-compo`} id={`${pluginId}.components.DynamicZone.add-compo`}
values={{ componentName: name }} values={{ componentName: metadatas.label }}
/> />
</div> </div>
<ComponentsPicker isOpen={isOpen}> <ComponentsPicker isOpen={isOpen}>
@ -237,22 +237,33 @@ const DynamicZone = ({
DynamicZone.defaultProps = { DynamicZone.defaultProps = {
dynamicDisplayedComponents: [], dynamicDisplayedComponents: [],
max: Infinity, fieldSchema: {
min: -Infinity, max: Infinity,
min: -Infinity,
},
}; };
DynamicZone.propTypes = { DynamicZone.propTypes = {
addComponentToDynamicZone: PropTypes.func.isRequired, addComponentToDynamicZone: PropTypes.func.isRequired,
dynamicDisplayedComponents: PropTypes.array, dynamicDisplayedComponents: PropTypes.array,
fieldSchema: PropTypes.shape({
components: PropTypes.array.isRequired,
max: PropTypes.number,
min: PropTypes.number,
}),
formErrors: PropTypes.object.isRequired, formErrors: PropTypes.object.isRequired,
isCreatingEntry: PropTypes.bool.isRequired, isCreatingEntry: PropTypes.bool.isRequired,
isFieldAllowed: PropTypes.bool.isRequired, isFieldAllowed: PropTypes.bool.isRequired,
isFieldReadable: PropTypes.bool.isRequired, isFieldReadable: PropTypes.bool.isRequired,
layout: PropTypes.object.isRequired, metadatas: PropTypes.shape({
description: PropTypes.string,
// editable: PropTypes.bool,
label: PropTypes.string,
// placeholder: PropTypes.string,
// visible: PropTypes.bool,
}).isRequired,
moveComponentUp: PropTypes.func.isRequired, moveComponentUp: PropTypes.func.isRequired,
moveComponentDown: PropTypes.func.isRequired, moveComponentDown: PropTypes.func.isRequired,
max: PropTypes.number,
min: PropTypes.number,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
removeComponentFromDynamicZone: PropTypes.func.isRequired, removeComponentFromDynamicZone: PropTypes.func.isRequired,
}; };

View File

@ -8,7 +8,6 @@ function useSelect(name) {
createActionAllowedFields, createActionAllowedFields,
isCreatingEntry, isCreatingEntry,
formErrors, formErrors,
layout,
modifiedData, modifiedData,
moveComponentUp, moveComponentUp,
moveComponentDown, moveComponentDown,
@ -37,7 +36,6 @@ function useSelect(name) {
return { return {
addComponentToDynamicZone, addComponentToDynamicZone,
formErrors, formErrors,
layout,
isCreatingEntry, isCreatingEntry,
isFieldAllowed, isFieldAllowed,
isFieldReadable, isFieldReadable,

View File

@ -84,7 +84,7 @@ const EditView = ({ components, currentEnvironment, models, plugins, slug }) =>
if (isDynamicZone(block)) { if (isDynamicZone(block)) {
const { const {
0: { 0: {
0: { name, fieldSchema }, 0: { name, fieldSchema, metadatas },
}, },
} = block; } = block;
@ -92,8 +92,8 @@ const EditView = ({ components, currentEnvironment, models, plugins, slug }) =>
<DynamicZone <DynamicZone
key={blockIndex} key={blockIndex}
name={name} name={name}
max={fieldSchema.max} fieldSchema={fieldSchema}
min={fieldSchema.min} metadatas={metadatas}
/> />
); );
} }

View File

@ -86,7 +86,7 @@ const EditView = ({ components, currentEnvironment, models, plugins, slug }) =>
if (isDynamicZone(block)) { if (isDynamicZone(block)) {
const { const {
0: { 0: {
0: { name, fieldSchema }, 0: { name, fieldSchema, metadatas },
}, },
} = block; } = block;
@ -94,8 +94,8 @@ const EditView = ({ components, currentEnvironment, models, plugins, slug }) =>
<DynamicZone <DynamicZone
key={blockIndex} key={blockIndex}
name={name} name={name}
max={fieldSchema.max} fieldSchema={fieldSchema}
min={fieldSchema.min} metadatas={metadatas}
/> />
); );
} }