Skip to content

Commit a1a76bc

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

File tree

2 files changed

+46
-52
lines changed

2 files changed

+46
-52
lines changed

API.md renamed to HOW_TO_USE.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
API
2-
---
1+
# How To Use
32

43
### Base (Common props)
54

@@ -25,6 +24,7 @@ interface IState extends IStateFieldBase {
2524

2625
interface IProps extends IPropsFieldBase {
2726
// your props
27+
onChange: (value: string) => void;
2828
}
2929

3030
class MyComponentField extends FieldCoreBase<IProps, IState> {
@@ -64,4 +64,47 @@ class MyComponentField extends FieldCoreBase<IProps, IState> {
6464
);
6565
}
6666
}
67+
```
68+
69+
### How extends default config
70+
71+
```ts
72+
// your config/index.ts
73+
import * as coreConfig from '@react-form-fields/core/config';
74+
75+
declare module '@react-form-fields/core/config' {
76+
interface IConfig {
77+
newProps?: string;
78+
}
79+
}
80+
81+
const defaultConfig: coreConfig.IConfig = {
82+
newProps: 'teste'
83+
};
84+
85+
export function getConfig(): coreConfig.IConfig {
86+
return {
87+
...defaultConfig,
88+
...(coreConfig.getConfig() || {})
89+
};
90+
}
91+
92+
export function setConfig(config: coreConfig.IConfig) {
93+
coreConfig.setConfig(config);
94+
}
95+
96+
// your config/builder.ts
97+
import { IConfig } from '@react-form-fields/core/config';
98+
import CoreConfigBuilder from '@react-form-fields/core/config/builder';
99+
100+
export default class ConfigBuilder extends CoreConfigBuilder {
101+
public setNewProps(newProps: string) {
102+
this.config = {
103+
...this.config,
104+
newProps: newProps
105+
};
106+
107+
return this; // <-- always return this
108+
}
109+
}
67110
```

README.md

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
React Form Fields: Core
33
------------------------------
44

5-
See [API.md](https://github.com/react-form-fields/core/blob/master/API.md) for details
65

76
## Requirements
87

@@ -21,55 +20,7 @@ yarn add @react-form-fields/core
2120

2221
### How to Create a Form Field
2322

24-
```tsx
25-
import FieldCoreBase, { IPropsFieldBase, IStateFieldBase } from '@react-form-fields/core/components/FieldCoreBase';
26-
27-
interface IState extends IStateFieldBase {
28-
//your state props
29-
}
30-
31-
interface IProps extends IPropsFieldBase {
32-
// your props
33-
}
34-
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-
}
42-
43-
onChange = event => {
44-
const value = this.mask.clean(event.target ? event.target.value : event);
45-
46-
this.setState({ touched: true }); //<-- important to show the error
47-
this.props.onChange(value);
48-
}
49-
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-
}
72-
```
23+
See [HOW_TO_USE.md](https://github.com/react-form-fields/core/blob/master/HOW_TO_USE.md) for details
7324

7425
### Common Masks
7526

0 commit comments

Comments
 (0)