diff --git a/sandbox-app/nullable-field.tsx b/sandbox-app/nullable-field.tsx index 080f4cc..55c9ba5 100644 --- a/sandbox-app/nullable-field.tsx +++ b/sandbox-app/nullable-field.tsx @@ -4,6 +4,7 @@ import { Input } from './components/Input'; interface Model { avatar?: { + id: number; url: string; } | null; } @@ -11,28 +12,20 @@ interface Model { const initialValue: Model = {}; export function NullableField(): JSX.Element { - const { - model, - changes, - fields, - onSubmit, - valid, - dirty, - submissionStatus, - reset, - } = useForm({ - model: initialValue, - onSubmit: async ({ model }) => { - // eslint-disable-next-line no-console - console.log(model); - }, - }); + const { model, changes, fields, onSubmit, valid, dirty, submissionStatus } = + useForm({ + model: initialValue, + onSubmit: async ({ model }) => { + // eslint-disable-next-line no-console + console.log(model); + }, + }); return ( <>

Nullable Field

- {model.avatar !== null ? ( + {Object.keys(model.avatar || {}).length ? ( <> ) : ( - )} +

Model

diff --git a/src/field.ts b/src/field.ts
index d23c3e5..3a5b897 100644
--- a/src/field.ts
+++ b/src/field.ts
@@ -164,6 +164,7 @@ export class FieldImplementation
 
   onChange = (value: T): void => {
     this.value = value;
+
     if (value && typeof value === 'object') {
       if (Array.isArray(value)) {
         this.#resetArray();
@@ -211,6 +212,8 @@ export class FieldImplementation
   }
 
   private get subfields(): Array> {
+    if (this.value === null) return [];
+
     return this.elements.concat(Object.values(this.#fields)) as Array<
       FieldImplementation
     >;