Skip to content

Commit

Permalink
fix: serialize intent
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed May 27, 2024
1 parent f83d133 commit 1e5cdab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/conform-dom/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ function handleIntent<Error>(
const name = formatName(intent.payload.name, intent.payload.index);

if (typeof value !== 'undefined') {
updateValue(meta, name ?? '', serialize(value));
updateValue(meta, name ?? '', value);
}

if (typeof validated !== 'undefined') {
Expand Down
37 changes: 23 additions & 14 deletions packages/conform-dom/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,12 @@ export function parse<FormValue, FormError>(
switch (intent.type) {
case 'update': {
const name = formatName(intent.payload.name, intent.payload.index);
const value = serialize(intent.payload.value);
const value = intent.payload.value;

if (typeof value !== 'undefined') {
if (typeof intent.payload.value !== 'undefined') {
if (name) {
setValue(context.payload, name, () => value);
} else {
// @ts-expect-error FIXME - it must be an object if there is no name
context.payload = value;
}
}
Expand All @@ -188,16 +187,7 @@ export function parse<FormValue, FormError>(
}
break;
}
case 'insert': {
setListValue(context.payload, {
type: intent.type,
payload: {
...intent.payload,
defaultValue: serialize(intent.payload.defaultValue),
},
});
break;
}
case 'insert':
case 'remove':
case 'reorder': {
setListValue(context.payload, intent);
Expand Down Expand Up @@ -395,7 +385,26 @@ export function getIntent(
}

export function serializeIntent<Schema>(intent: Intent<Schema>): string {
return JSON.stringify(intent);
switch (intent.type) {
case 'insert':
return JSON.stringify({
type: intent.type,
payload: {
...intent.payload,
defaultValue: serialize(intent.payload.defaultValue),
},
});
case 'update':
return JSON.stringify({
type: intent.type,
payload: {
...intent.payload,
value: serialize(intent.payload.value),
},
});
default:
return JSON.stringify(intent);
}
}

export function updateList(
Expand Down
7 changes: 3 additions & 4 deletions packages/conform-react/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,9 @@ export function getInputProps<Schema, Options extends InputOptions>(
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 =
typeof metadata.initialValue === 'boolean'
? metadata.initialValue
: metadata.initialValue === props.value;
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;
}
Expand Down

0 comments on commit 1e5cdab

Please # to comment.