diff --git a/packages/conform-react/helpers.ts b/packages/conform-react/helpers.ts index bebe699a..08c4fa9c 100644 --- a/packages/conform-react/helpers.ts +++ b/packages/conform-react/helpers.ts @@ -232,10 +232,10 @@ export function getFormControlProps( * ```tsx * // To setup an uncontrolled input * + * // To setup an input without defaultValue + * * // To setup an uncontrolled checkbox * - * // To setup an input without defaultValue - * * // To setup a radio button without defaultChecked state * * ``` @@ -257,13 +257,18 @@ export function getInputProps( }; if (typeof options.value === 'undefined' || options.value) { - if (options.type === 'checkbox' || options.type === 'radio') { - props.value = typeof options.value === 'string' ? options.value : 'on'; - props.defaultChecked = Array.isArray(metadata.initialValue) - ? metadata.initialValue.includes(options.value) - : metadata.initialValue === props.value; - } else if (typeof metadata.initialValue === 'string') { - props.defaultValue = metadata.initialValue; + switch (options.type) { + case 'checkbox': + case 'radio': + props.value = typeof options.value === 'string' ? options.value : 'on'; + props.defaultChecked = Array.isArray(metadata.initialValue) + ? metadata.initialValue.includes(options.value) + : metadata.initialValue === props.value; + break; + case 'file': + break; + default: + props.defaultValue = metadata.initialValue?.toString(); } } diff --git a/tests/conform-react.spec.ts b/tests/conform-react.spec.ts index dc26e244..9edac52f 100644 --- a/tests/conform-react.spec.ts +++ b/tests/conform-react.spec.ts @@ -155,6 +155,17 @@ describe('conform-react', () => { ...props, type: 'file', }); + expect( + getInputProps( + // This happens in Playwright + // @ts-expect-error FIXME: To fix the type + { ...metadata, initialValue: [undefined, undefined] }, + { type: 'file' }, + ), + ).toEqual({ + ...props, + type: 'file', + }); }); test('getTextareaProps', () => {