-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue setting initialValue within sections #62
Comments
Ah yes, this is something I patched on a forked version, but never got round to opening a PR for it. It's because the schema It only sets values for the variant, but not from the initialValues. Note this version supports adding multiple sections at a time, so it's a little different, but the key code to get the initialValues is marked with const onSectionAdd: OnSectionAddFn = ({ sections }) => {
setOpen(false);
const isSingleSection = sections.length === 1;
sections.forEach((section) => {
const { sectionName, initialValue } = section;
/* */ let defaultInitialValue = getSectionInitialValues(sectionName, props);
const newItem = {
_type: sectionName,
/* */ ...defaultInitialValue,
...(initialValue || {}),
_key: generateItemKey(),
};
if (onItemAdd && typeof onItemAdd === "function") {
onItemAdd(newItem);
} else {
props.onItemAppend(newItem);
}
if (isSingleSection) {
props.onItemOpen([...props.path, { _key: newItem._key }]);
}
});
}; and import { ArrayOfObjectsInputProps } from 'sanity';
export function getSectionInitialValues(sectionName: string, fieldProps: ArrayOfObjectsInputProps) {
let defaultInitialValues = {};
const sectionSchema = fieldProps.schemaType.of.find((sectionSchema) => sectionSchema.name === sectionName);
if (sectionSchema && sectionSchema.jsonType === 'object') {
defaultInitialValues = sectionSchema.fields.reduce(
(acc, sectionField) => {
const fallbacks: Record<string, any> = {
'array': [],
'object': {},
};
// Some fields need must have a value to prevent errors when adding content before a page refresh, such as "array"
const fallbackValue = sectionField.type.jsonType in fallbacks
? fallbacks[sectionField.type.jsonType]
: undefined;
return {
...acc,
[sectionField.name]: sectionField.type.initialValue ?? fallbackValue,
}
},
{}
);
}
// @todo consider making this generic and handling other jsonType's, rather than just 'object'
return defaultInitialValues;
} |
Hi, and thanks for the great packages - they've made it much easier to build a great Sanity Studio experience 🙌
I've ran into an issue using the defineSection helper, where the initialValue of a field is not respected. It's probably easiest to explain with a video:
Screen.Recording.2024-06-05.at.11.54.44.mov
Here I'm using a "media" schema type I've defined, that sets some default values. They work fine when added directly to the document (using the defineField Sanity helper), but when they're using the same helper within a section, it doesn't set the default values.
Any idea what might be causing this or what I can do to fix it? Appreciate any help 🙌
The text was updated successfully, but these errors were encountered: