Skip to content

Remove check for all fields being touched for form to be valid #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/create-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ export const createForm = (config) => {
const isSubmitting = writable(false);
const isValidating = writable(false);

const isValid = derived([errors, touched], ([$errors, $touched]) => {
const allTouched = util
.getValues($touched)
.every((field) => field === IS_TOUCHED);
const isValid = derived(errors, ($errors) => {
const noErrors = util
.getValues($errors)
.every((field) => field === NO_ERROR);
return allTouched && noErrors;
return noErrors;
});

const modified = derived(form, ($form) => {
Expand Down
47 changes: 21 additions & 26 deletions test/library.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,30 @@ describe('createForm', () => {
expect(instance.isValid.subscribe).toBeDefined();
});

it('returns false if form is invalid', () => {
instance = getInstance({
it('returns true if form is valid', async (done) => {
instance
.handleSubmit()
.then(() => subscribeOnce(instance.isValid))
.then((isValid) => {
expect(isValid).toBe(true);
})
.then(done);
});

it('returns false if form is invalid', async (done) => {
await instance.form.set({
name: '',
email: '',
country: '',
country: ''
});
subscribeOnce(instance.isValid).then((isValid) =>
expect(isValid).toBe(false),
);
});

it('returns false if some fields are untouched', async () => {
const touched = await subscribeOnce(instance.touched);
const someUntouched = Object.values(touched).some((val) => val === false);
expect(someUntouched).toBe(true);

const isValid = await subscribeOnce(instance.isValid);
expect(isValid).toBe(false);
});

it('returns true if form is valid and all fields touched', () => {
instance.touched.set({
name: true,
email: true,
country: true,
});
subscribeOnce(instance.isValid).then((isValid) =>
expect(isValid).toBe(true),
);
instance
.handleSubmit()
.then(() => subscribeOnce(instance.isValid))
.then((isValid) => {
expect(isValid).toBe(false);
})
.then(done);
});
});

Expand Down Expand Up @@ -359,7 +354,7 @@ describe('createForm', () => {
});

it('calls onSubmit when form is valid', (done) => {
expect(onSubmit).not.toBeCalled();
instance = getInstance();
instance.handleSubmit().then(expect(onSubmit).toBeCalled).then(done);
});

Expand Down