4.1.1 • Published 8 months ago
formik-alfalab-core-components v4.1.1
formik-alfalab-core-components
Bindings for formik and @alfalab/core-components.
Usage
Just use the components from formik-alfalab-core-components as a drop-in replacement of the components
from @alfalab/core-components, but with one required prop name. Available components are:
- AmountInput
- Checkbox
- Input
- InputAutocomplete
- MaskedInput
- PhoneInput
- RadioGroup
- Select
- Switch
- Textarea
Consider the example:
import React from 'react';
import { Formik, Form } from 'formik';
import { Input } from 'formik-alfalab-core-components/Input';
export const MyForm: FC = () => {
return (
<Formik
initialValues={{ name: '' }}
onSubmit={(values) => { console.log(values) }}
>
<Form>
<Input name="name" />
<button type="submit">Submit</button>
</Form>
</Formik>
);
};Exports
Each component can be imported from the subdirectory:
import { Input } from 'formik-alfalab-core-components/Input';This library also exposes some utility hooks which are used under the hood by components:
useFieldBlurState— useful for handlingblurevent inside formik context;useFieldOkState— provideserrorandsuccessprops using formik state;useInputFieldState— providesprops: InputPropsusing formik state;useSelectFieldState— providesprops: BaseSelectPropsusing formik state.
These hooks are available under formik-alfalab-core-components/hooks and can be used
to bind your custom components to formik.
How does it work?
Under the hood it connects to the Formik's state via useField hook and name prop.
Then the given state is passed to original component from @alfalab/core-components.