|
1 | 1 | # How To Use |
2 | 2 |
|
3 | | -### Base (Common props) |
| 3 | +See [API.md](https://github.com/react-form-fields/core/blob/master/API.md) to see all configs available |
4 | 4 |
|
5 | | -Component Base to create a form field. |
| 5 | +* [How to Create a Form Field](#how-to-create-a-form-field) |
| 6 | +* [How to a custom validation rules](#how-to-a-custom-validation-rules) |
| 7 | +* [How extends default config](#how-extends-default-config) |
| 8 | +* [How to create or extends a language config](#how-to-create-or-extends-a-language-config) |
6 | 9 |
|
7 | | -| Props | Required | Type | Description | |
8 | | -|--------------------------|----------|------------------------|------------------------------------------------------------------------| |
9 | | -| name | false | string | | |
10 | | -| value | false | any | | |
11 | | -| submitted | false | boolean | flag to set if form was submited (also can be set by setFormSubmitted) | |
12 | | -| validation | false | string | rules of validation | |
13 | | -| validationContext | false | object { prop: value } | extra fields for validation bind (ex. required_if) | |
14 | | -| validationAttributeNames | false | object { prop: value } | see: https://github.com/skaterdav85/validatorjs#custom-attribute-names | |
15 | | -| errorMessage | false | string | custom error message from external validation | |
16 | | - |
17 | | -### How to Create a Form Field |
| 10 | +## How to Create a Form Field |
18 | 11 |
|
19 | 12 | ```tsx |
20 | | -import FieldCoreBase, { IPropsFieldBase, IStateFieldBase } from '@react-form-fields/core/components/FieldCoreBase'; |
21 | | - |
22 | | -interface IState extends IStateFieldBase { //<-- extends |
23 | | - //your state props |
| 13 | +import useMask from '@react-form-fields/core/hooks/useMask'; |
| 14 | +import useValidation from '@react-form-fields/core/hooks/useValidation'; |
| 15 | +import { IPropsFieldBase } from '@react-form-fields/core/interfaces/props'; |
| 16 | +import React, { useCallback } from 'react'; |
| 17 | + |
| 18 | +interface IProps extends IPropsFieldBase { // <~extends your interface props from IPropsFieldBase |
| 19 | + placeholder: string; |
| 20 | + value: string; |
| 21 | + onChange(value: string): void; |
24 | 22 | } |
25 | 23 |
|
26 | | -interface IProps extends IPropsFieldBase { //<-- extends |
27 | | - // your props |
28 | | - onChange: (value: string) => void; |
29 | | -} |
| 24 | +const Field = React.memo((props: IProps) => { |
| 25 | + const { onChange, placeholder } = props; |
| 26 | + |
| 27 | + const { errorMessage, showError, setDirty } = useValidation(props); // <~ Register your field and get validation |
| 28 | + const { maskedValue, maskClean } = useMask(props); // <~ (optional) get the mask info, don't worry if no mask was pass, there is a default |
| 29 | + |
| 30 | + const onChangeHandler = useCallback((e: React.ChangeEvent<HTMLInputElement>) => { |
| 31 | + setDirty(true); // <~ remember to check your field as dirty |
| 32 | + onChange(maskClean(e.currentTarget.value) as string); // <~ (optional) always pass the "clean" value |
| 33 | + }, [onChange, setDirty, maskClean]); |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className='input-container'> |
| 37 | + <input |
| 38 | + type='text' |
| 39 | + placeholder={placeholder} |
| 40 | + value={maskedValue} // <~ (optional) use the masked value |
| 41 | + onChange={onChangeHandler} |
| 42 | + /> |
| 43 | + |
| 44 | + {showError && errorMessage && // <~ use showError to know when the error message should be display |
| 45 | + <p className='error'>{errorMessage}</p> |
| 46 | + } |
| 47 | + </div> |
| 48 | + ) |
| 49 | +}); |
| 50 | + |
| 51 | +export default Field; |
| 52 | +``` |
30 | 53 |
|
31 | | -class MyComponentField extends FieldCoreBase<IProps, IState> { |
32 | | - //[optional] If you need getDerivedStateFromProps dont forget to call super |
33 | | - static getDerivedStateFromProps(props: IProps, currentState: IState): IState { |
34 | | - const state = super.getDerivedStateFromProps(props, currentState); |
35 | | - // your logic.... |
36 | | - return state; |
37 | | - } |
| 54 | +## How to a custom validation rules |
38 | 55 |
|
39 | | - onChange = event => { |
40 | | - const value = this.mask.clean(event.target ? event.target.value : event); |
| 56 | +See [validatorjs](https://github.com/skaterdav85/validatorjs#register-custom-validation-rules) |
41 | 57 |
|
42 | | - this.setState({ touched: true }); //<-- important to show the error |
43 | | - this.props.onChange(value); |
44 | | - } |
| 58 | +## How extends default config |
45 | 59 |
|
46 | | - render() { |
47 | | - const { label, name } = this.props; |
48 | | - |
49 | | - return ( |
50 | | - <Fragment> |
51 | | - {/* import: register the field in the validation context */} |
52 | | - <ValidationContextRegister field={this} /> |
53 | | - |
54 | | - {/* isRequired: check if validation prop contains the required rule */} |
55 | | - <label>{label} {this.isRequired ? '*' : ''}</label> |
56 | | - <input |
57 | | - name={name} |
58 | | - value={this.getMaskedValue} |
59 | | - onChange={this.onChange} |
60 | | - /> |
61 | | - |
62 | | - {/* errorMessage will be null if submitted and touched are false */} |
63 | | - {this.errorMessage ? <p class="error">{this.errorMessage}</p> : null} |
64 | | - </Fragment> |
65 | | - ); |
66 | | - } |
67 | | -} |
68 | | -``` |
| 60 | +We recommend you to always create these files to avoid imports from **@react-form-fields/core** |
| 61 | +directly, you can copy and paste initially. |
69 | 62 |
|
70 | | -### How extends default config |
| 63 | +Create config/context.tsx |
71 | 64 |
|
72 | | -```ts |
73 | | -// your config/index.ts |
74 | | -import * as coreConfig from '@react-form-fields/core/config'; |
| 65 | +```tsx |
| 66 | +import FieldValidationConfigContextCore, { IConfig as IConfigCore } from '@react-form-fields/core/config'; |
| 67 | +import ConfigBuilderClass from './builder'; |
75 | 68 |
|
76 | | -declare module '@react-form-fields/core/config' { |
77 | | - interface IConfig { |
78 | | - newProps?: string; |
79 | | - } |
| 69 | +export interface IConfig extends IConfigCore { |
| 70 | + myNewBrandConfigProp?: string; // <~ all configs are optional |
80 | 71 | } |
81 | 72 |
|
82 | | -const defaultConfig: coreConfig.IConfig = { |
83 | | - newProps: 'teste' |
84 | | -}; |
85 | | - |
86 | | -export function getConfig(): coreConfig.IConfig { |
87 | | - return { |
88 | | - ...defaultConfig, |
89 | | - ...(coreConfig.getConfig() || {}) |
90 | | - }; |
91 | | -} |
| 73 | +export const ConfigBuilder = ConfigBuilderClass; |
| 74 | +export default FieldValidationConfigContext = FieldValidationConfigContextCore; |
| 75 | +``` |
92 | 76 |
|
93 | | -export function setConfig(config: coreConfig.IConfig) { |
94 | | - coreConfig.setConfig(config); |
95 | | -} |
| 77 | +Create config/builder.tsx |
96 | 78 |
|
97 | | -// your config/builder.ts |
98 | | -import { IConfig } from '@react-form-fields/core/config'; |
| 79 | +```tsx |
99 | 80 | import CoreConfigBuilder from '@react-form-fields/core/config/builder'; |
| 81 | +import { IConfig } from './context'; |
100 | 82 |
|
101 | 83 | export default class ConfigBuilder extends CoreConfigBuilder { |
102 | | - public setNewProps(newProps: string) { |
| 84 | + public setMyNewBrandConfigProp(myNewBrandConfigProp: string) { |
103 | 85 | this.config = { |
104 | | - ...this.config, |
105 | | - newProps: newProps |
| 86 | + ...this.config, // <~ keeps already set's configs |
| 87 | + myNewBrandConfigProp |
106 | 88 | }; |
107 | 89 |
|
108 | | - return this; // <-- always return this |
| 90 | + return this; // <~ always return this |
| 91 | + } |
| 92 | + |
| 93 | + public clean() { // <~you can override the celan method to add you own default values |
| 94 | + return { |
| 95 | + ...super.clean(), |
| 96 | + myNewBrandConfigProp: 'my default value' |
| 97 | + } |
109 | 98 | } |
110 | 99 | } |
111 | | -``` |
| 100 | +``` |
| 101 | + |
| 102 | +## How to create or extends a language config |
| 103 | + |
| 104 | +Create you lang file in langs/your-lang.ts |
| 105 | + |
| 106 | +```ts |
| 107 | +import { IConfig } from '.../config/context'; |
| 108 | +import coreLangPTBR from '@react-form-fields/core/lang/pt-br'; // <~ import original lang config |
| 109 | +import * as locale from 'date-fns/locale/pt-BR'; |
| 110 | + |
| 111 | +const langPTBR: IConfig = { |
| 112 | + ...coreLangPTBR, // <~ extends the original lang config |
| 113 | + // add your configs |
| 114 | + myNewBrandConfigProp: 'default value for the lang', |
| 115 | + masks: [ |
| 116 | + ...coreLangPTBR.masks |
| 117 | + // your masks |
| 118 | + ], |
| 119 | + validations: { |
| 120 | + lang: 'you-new-lang', |
| 121 | + customMessages: { |
| 122 | + ...coreLangPTBR.validations.customMessages |
| 123 | + // override the default messages |
| 124 | + } |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +export default langPTBR; |
| 129 | +``` |
| 130 | + |
| 131 | +## How to |
0 commit comments