Module: "useNormalizers"

May 22, 2020 · View on GitHub

react-uniformed"useNormalizers"

Module: "useNormalizers"

Index

Type aliases

Functions

Type aliases

UseNormalizersOption

Ƭ UseNormalizersOption: Readonly‹object›

Functions

useNormalizers

useNormalizers(...normalizers: NormalizerHandler | object[]): NormalizerHandler

Creates a single normalizer function from the specified list of normalizers. note: order matters when passing normalizers. This means that the results or value of the first normalizer is passed to the next normalizer.

example

const normalizer = useNormalizers(
   // apply to all fields
   normalizeNestedObjects(),
   {
     // apply to only fields ending in name (eg: firstName, lastName)
     name: /name$/i,
     normalizer: ({value}) => !value ? value : value.toUpperCase(),
   },
   {
     // apply to only the date field
     name: "date",
     normalizer: ({value}) => !value ? value : value.replace(/-/g, "/"),
   },
   {
     // apply to username or slug field
     name: ["username", /^slug$/],
     normalizer: ({value}) => !value ? value : value.toLowerCase(),
   }
)

example Usage with useForm and useFields

const normalizer = useNormalizers({
  name: /name$/i,
  normalizer: ({value}) => !value ? value : value.toUpperCase()
});

const {values} = useFields(
  {}, // initialValues must come first
  normalizer
);

const {values} = useForm({ normalizer });

Parameters:

NameTypeDescription
...normalizersNormalizerHandler | object[]if you pass a normalizer handler then it will apply to all fields. You can specify a specific list of fields by passing in a UseNormalizersOption

Returns: NormalizerHandler

returns a normalizer handler