Skip to content

Commit

Permalink
useField nullable support
Browse files Browse the repository at this point in the history
  • Loading branch information
a-type committed Jul 19, 2024
1 parent 7a04f78 commit 3807414
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-bikes-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@verdant-web/react': patch
---

Fix useField for nullable fields
10 changes: 8 additions & 2 deletions packages/react/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ export function createHooks<Presence = any, Profile = any>(

const inputProps = useMemo(() => {
const fieldSchema = entity.getFieldSchema(key);
const fieldValue =
fieldSchema.type === 'boolean'
? key
: value === null || value === undefined
? ''
: `${value}`;
const props: HTMLProps<HTMLInputElement> = {
onChange: (e: ChangeEvent<HTMLInputElement>) => {
if (fieldSchema.type === 'number') {
Expand Down Expand Up @@ -447,11 +453,11 @@ export function createHooks<Presence = any, Profile = any>(
onBlur: () => {
client.sync.presence.setFieldId(undefined);
},
value: fieldSchema.type === 'boolean' ? key : value.toString(),
value: fieldValue,
};
if (fieldSchema.type === 'boolean') {
props.type = 'checkbox';
props.checked = value;
props.checked = !!value;
}
return props;
}, [value, setValue, client, entity]);
Expand Down

0 comments on commit 3807414

Please sign in to comment.