github andyrichardson/fielder v2.0.0

latest releases: v2.2.0, v2.1.1, v2.1.0...
3 years ago

v2.0.0 (2020-12-22)

Full Changelog

A new major release!

That's right, a new major release jam packed full of new features and with a side helping of breaking changes.

See the announcement post.

New docs site

Technically not part of this release but definitely worth a visit to check out the new guides!

Visit the docs!

Event driven validation

Validation is now tailored to specific events (mount, blur, submit, etc).

This provides extra flexibility to tweak validation for specific scenarios - such as triggering async validation only on submit (see example below).

useField({
  validate: useCallback(({ trigger, value  }) {
    // Validation for all events
    if (!value) {
      throw Error("Value is required.");
    }

    // Validation for blur only events
    if (trigger == "blur" && value.length < 4) {
      throw Error("Value must be at least 4 characters.");
    }

    // Async validation only on submit
    if (trigger == "submit") {
      return serverSideValidation(value).then((isValid) => {
        if (!isValid) {
          throw Error("Server side validation failed");
        }
      });
    }
  }, [])
})

More info on event driven validation can be found here.

useSubmit hook

There's a new useSubmit hook which:

  • triggers the new submit validation event
  • aggregates field values into a single object
  • tracks state of async submission validation
  • guards submission logic until validation succeeds
const { isValidating, hasSubmitted, handleSubmit } = useSubmit(() => {
  console.log('This is only called if submission validation succeeds!');
});

handleSubmit(); // Trigger submission

More info on submission can be found here.

Breaking changes

  • initialValue argument on the useField hook is now required (more info)
  • validate argument on the useField hook now receives only a single argument (more info)
  • removed deprecated properties touched and initalTouched on the useField hook (more info)
  • removed initialValid and initialError arguments on the useField hook in favor of validation events (more info)
  • removed validateOnBlur, validateOnChange, and validateOnUpdate arguments on the useField hook in favor of validation events (more info)
  • removed support for returning validation errors as strings without throwing (more info)

Don't miss a new fielder release

NewReleases is sending notifications on new releases.