mirror of
https://github.com/strapi/strapi.git
synced 2025-12-12 15:32:42 +00:00
62 lines
1.3 KiB
JavaScript
62 lines
1.3 KiB
JavaScript
import { get } from 'lodash';
|
|
|
|
const createDefaultForm = (attributes, allComponentsSchema) => {
|
|
return Object.keys(attributes).reduce((acc, current) => {
|
|
const attribute = get(attributes, [current], {});
|
|
const {
|
|
default: defaultValue,
|
|
component,
|
|
type,
|
|
required,
|
|
min,
|
|
repeatable,
|
|
} = attribute;
|
|
|
|
if (type === 'json') {
|
|
acc[current] = null;
|
|
}
|
|
|
|
if (type === 'json' && required === true) {
|
|
acc[current] = {};
|
|
}
|
|
|
|
if (defaultValue !== undefined) {
|
|
acc[current] = defaultValue;
|
|
}
|
|
|
|
if (type === 'component') {
|
|
const currentComponentSchema = get(
|
|
allComponentsSchema,
|
|
[component, 'schema', 'attributes'],
|
|
{}
|
|
);
|
|
const currentComponentDefaultForm = createDefaultForm(
|
|
currentComponentSchema,
|
|
allComponentsSchema
|
|
);
|
|
|
|
if (required === true) {
|
|
acc[current] = repeatable === true ? [] : {};
|
|
}
|
|
|
|
if (min && repeatable === true && required) {
|
|
acc[current] = [];
|
|
|
|
for (let i = 0; i < min; i += 1) {
|
|
acc[current].push(currentComponentDefaultForm);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (type === 'dynamiczone') {
|
|
if (required === true) {
|
|
acc[current] = [];
|
|
}
|
|
}
|
|
|
|
return acc;
|
|
}, {});
|
|
};
|
|
|
|
export default createDefaultForm;
|