Skip to content

Commit

Permalink
fix(app-headless-cms): apply default value to single-value fields only
Browse files Browse the repository at this point in the history
(cherry picked from commit 1fc4241)
  • Loading branch information
Pavel910 committed Dec 12, 2024
1 parent 3692b51 commit 6073223
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,24 @@ export function useBind({ Bind, field }: UseBindProps) {
const isMultipleValues = index === -1 && field.multipleValues;
const inputValidators = isMultipleValues ? listValidators : validators;

// We only use default values for single-value fields.
const defaultValueFromSettings = !isMultipleValues
? field.settings?.defaultValue
: null;

memoizedBindComponents.current[componentId] = function UseBind(params: UseBindParams) {
const {
name: childName,
validators: childValidators,
children,
defaultValue
defaultValue = defaultValueFromSettings
} = params;

return (
<Bind
name={childName || name}
validators={childValidators || inputValidators}
defaultValue={!isMultipleValues ? defaultValue : null}
defaultValue={defaultValue ?? null}
>
{bind => {
// Multiple-values functions below.
Expand Down Expand Up @@ -99,7 +104,7 @@ export function useBind({ Bind, field }: UseBindProps) {
let value = bind.value;
value = [...value.slice(0, index), ...value.slice(index + 1)];

bind.onChange(value);
bind.onChange(value.length === 0 ? null : value);

// To make sure the field is still valid, we must trigger validation.
form.validateInput(field.fieldId);
Expand Down

0 comments on commit 6073223

Please sign in to comment.