Skip to content

Commit

Permalink
call on update only if instanced
Browse files Browse the repository at this point in the history
  • Loading branch information
leoggonzalez committed Nov 16, 2022
1 parent dc715f6 commit 1c2a4f6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sandbox-app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { useForm } from '../src/index';
import { NestedField } from './nested-field';
import { NestedValidation } from './nested-validation';
import { NullableField } from './nullable-field';
import { ObjectsArray } from './objects-array';
import { SimpleArray } from './simple-array';
Expand Down Expand Up @@ -39,6 +40,7 @@ function App(): JSX.Element {
<NestedField />
<NullableField />
<SimpleObject />
<NestedValidation />
</div>
</>
);
Expand Down
59 changes: 59 additions & 0 deletions sandbox-app/nested-validation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React from 'react';
import { useForm } from '../src';
import { Input } from './components/Input';

interface Model {
address?: {
streetName?: string;
};
}

const initialValue: Model = {};

export function NestedValidation(): 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);
},
validations: {
address: {
streetName: 'required',
},
},
});

return (
<>
<h2 id="nested-field">Nested Field</h2>
<form onSubmit={onSubmit}>
<div className="stack">
<Input label="Street name: " {...fields.address.fields.streetName} />
<footer>
<button>Submit</button>
<button onClick={reset} type="button">
Reset
</button>
</footer>
</div>
</form>
<h4>Model</h4>
<pre>
{JSON.stringify({ model, valid, dirty, submissionStatus }, null, 2)}
</pre>
<h4>Changes</h4>
<pre>{JSON.stringify({ changes }, null, 2)}</pre>
<a href="#">Back</a>
</>
);
}
2 changes: 1 addition & 1 deletion src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class FieldImplementation<T, Model>
get fields(): MappedFields<T> {
if (!this.value) {
this.value = {} as T;
this.#onUpdate();
this.#onUpdate?.();
}
const handler = {
get: (target: MappedFields<T>, key: string) => {
Expand Down
18 changes: 18 additions & 0 deletions src/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,24 @@ describe(Form, () => {
expect(emails.dirty).toBeTruthy();
expect(form.dirty).toBeTruthy();
});
it('handles nested validation', () => {
const form = createForm({
value: {
address: {
streetName: '',
},
},
validations: {
address: {
streetName: 'required',
},
},
});
expect(form.fields.address.fields.streetName.errors).toHaveLength(1);
expect(form.fields.address.fields.streetName.errors[0]).toBe(
'required-field'
);
});
});
});

Expand Down

0 comments on commit 1c2a4f6

Please sign in to comment.