Skip to content

Commit d40cfff

Browse files
committed
update readme
Signed-off-by: danieloprado <[email protected]>
1 parent 4d6e2c6 commit d40cfff

File tree

2 files changed

+65
-141
lines changed

2 files changed

+65
-141
lines changed

API.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,25 @@ Component Base to create a form field.
1616

1717
### How to Create a Form Field
1818

19-
```jsx
20-
import { FieldCoreBase } from '@react-form-fields/core';
19+
```tsx
20+
import FieldCoreBase, { IPropsFieldBase, IStateFieldBase } from '@react-form-fields/core/components/FieldCoreBase';
21+
22+
interface IState extends IStateFieldBase {
23+
//your state props
24+
}
25+
26+
interface IProps extends IPropsFieldBase {
27+
// your props
28+
}
29+
30+
class MyComponentField extends FieldCoreBase<IProps, IState> {
31+
//If you need getDerivedStateFromProps dont forget to call super
32+
static getDerivedStateFromProps(props: IProps, currentState: IState): IState {
33+
const state = super.getDerivedStateFromProps(props, currentState);
34+
// your logic....
35+
return state;
36+
}
2137

22-
class MyComponentField extends FieldCoreBase {
2338
onChange = event => {
2439
const value = this.mask.clean(event.target ? event.target.value : event);
2540

README.md

Lines changed: 47 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
![logo](https://avatars2.githubusercontent.com/u/40718737?s=50&v=4)
12
React Form Fields: Core
23
------------------------------
34

@@ -13,153 +14,61 @@ See [API.md](https://github.com/react-form-fields/core/blob/master/API.md) for d
1314
yarn add @react-form-fields/core
1415
```
1516

16-
## Usage
17+
## Implementations
1718

18-
### Individual field
19+
* [Material UI](https://github.com/react-form-fields/material-ui)
20+
* [NativeBase](https://github.com/react-form-fields/native-base)
1921

20-
```jsx
21-
// import
22-
import FieldText from '@react-form-fields/material-ui/components/Text';
22+
### How to Create a Form Field
2323

24-
// render()
25-
<FieldText
26-
ref={ref => this.field = ref}
27-
label='Email'
28-
type='email'
29-
disabled={disabled}
30-
value={email}
31-
validation='required|email'
32-
onChange={v => this.setState({ email: v }))}
33-
/>
24+
```tsx
25+
import FieldCoreBase, { IPropsFieldBase, IStateFieldBase } from '@react-form-fields/core/components/FieldCoreBase';
3426

35-
// onSubmit()
36-
if(this.field.isValid()) {
37-
console.log('submit');
38-
}
39-
```
40-
41-
### Complete Form
42-
43-
```jsx
44-
// import
45-
import ValidationContext from '@react-form-fields/core/components/ValidationContext';
46-
import FieldText from '@react-form-fields/material-ui/components/Text';
47-
48-
// render()
49-
<ValidationContext ref={ref=> this.validation = ref}>
50-
<FieldText
51-
ref={ref => this.field = ref}
52-
label='Email'
53-
type='email'
54-
value={email}
55-
validation='required|email'
56-
onChange={v => this.setState({ email: v }))}
57-
/>
58-
59-
<FieldText
60-
label='Senha'
61-
type='password'
62-
value={password}
63-
validation='required'
64-
onChange={v => this.setState({ password: v }))}
65-
/>
66-
</ValidationContext>
67-
68-
// onSubmit()
69-
if(this.validation.isValid()) {
70-
console.log('all fields are valid');
71-
}
72-
```
27+
interface IState extends IStateFieldBase {
28+
//your state props
29+
}
7330

74-
### Config
75-
76-
Global Setup example:
77-
78-
```js
79-
import { setConfig } from '@react-form-fields/core';
80-
import commonMasks from '@react-form-fields/core/mask/common/pt-br';
81-
import validationMessage from '@react-form-fields/core/validator/custom-languages/pt-br';
82-
83-
setConfig({
84-
masks: commonMasks,
85-
validation: validationMessage
86-
});
87-
```
31+
interface IProps extends IPropsFieldBase {
32+
// your props
33+
}
8834

89-
## Validation Rules and Config
90-
91-
See [validatorjs](https://github.com/skaterdav85/validatorjs)
92-
93-
Validation and Validation Context
94-
95-
```jsx
96-
<FieldDate
97-
name='begin'
98-
value={model.beginDate}
99-
validation='date'
100-
onChange={(v => this.setState({ model: { ...model, beginDate: v } }))}
101-
/>
35+
class MyComponentField extends FieldCoreBase<IProps, IState> {
36+
//If you need getDerivedStateFromProps dont forget to call super
37+
static getDerivedStateFromProps(props: IProps, currentState: IState): IState {
38+
const state = super.getDerivedStateFromProps(props, currentState);
39+
// your logic....
40+
return state;
41+
}
10242

103-
<FieldDate
104-
name='end'
105-
value={model.endDate}
106-
validation='date|after_or_equal:begin date' //after_or_equal needs a value from other prop (ex: 'begin date')
107-
validationContext={{ 'begin date': model.beginDate }} // build the dependency object as you needed
108-
onChange={(v => this.setState({ model: { ...model, endDate: v } }))}
109-
/>
110-
```
43+
onChange = event => {
44+
const value = this.mask.clean(event.target ? event.target.value : event);
11145

112-
Custom Message
113-
114-
```jsx
115-
<FieldDate
116-
label='Begin Date'
117-
name='begin'
118-
value={model.beginDate}
119-
validation='date'
120-
onChange={(v => this.setState({ model: { ...model, beginDate: v } }))}
121-
>
122-
<CustomMessage rules='date'>This not a date!</CustomMessage>
123-
</FieldDate>
124-
```
46+
this.setState({ touched: true }); //<-- important to show the error
47+
this.props.onChange(value);
48+
}
12549

126-
## Mask
127-
128-
Register your custom masks:
129-
130-
```js
131-
// register
132-
import { register } from '@react-form-fields/core/mask';
133-
134-
// -optional
135-
import commonMasks from '@react-form-fields/core/mask/common/pt-br';
136-
137-
register([
138-
...commonMasks, // -optional
139-
name: 'my-new-mask',
140-
apply: value => {
141-
if (!value) return value;
142-
143-
const regexp = value.length > 10 ?
144-
/^(\d{0,2})(\d{0,5})(\d{0,4}).*/ :
145-
/^(\d{0,2})(\d{0,4})(\d{0,4}).*/;
146-
147-
const result = value.length > 2 ?
148-
'($1) $2-$3' : '($1$2$3';
149-
150-
return value.replace(regexp, result).replace(/-$/, '');
151-
},
152-
clean: value => value.replace(/\D/gi, '').substr(0, 11)
153-
])
154-
155-
// usage
156-
<FieldText
157-
label='Phone'
158-
type='text'
159-
mask='my-new-mask'
160-
value={phone}
161-
onChange={v => this.setState({ phone: v }))}
162-
/>
50+
render() {
51+
const { label, name } = this.props;
52+
53+
return (
54+
<Fragment>
55+
{/* import: register the field in the validation context */}
56+
<ValidationContextRegister field={this} />
57+
58+
{/* isRequired: check if validation prop contains the required rule */}
59+
<label>{label} {this.isRequired ? '*' : ''}</label>
60+
<input
61+
name={name}
62+
value={this.getMaskedValue}
63+
onChange={this.onChange}
64+
/>
65+
66+
{/* errorMessage will be null if submitted and touched are false */}
67+
{this.errorMessage ? <p class="error">{this.errorMessage}</p> : null}
68+
</Fragment>
69+
);
70+
}
71+
}
16372
```
16473

16574
### Common Masks

0 commit comments

Comments
 (0)