Skip to content

Commit a7dcc56

Browse files
committed
New way to register a component
Signed-off-by: danieloprado <[email protected]>
1 parent f50e047 commit a7dcc56

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

API.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ class MyComponentField extends FieldCoreBase {
2828
}
2929

3030
render() {
31-
const { value, label, name } = this.props;
31+
const { label, name } = this.props;
3232

3333
return (
3434
<Fragment>
35-
{super.render() /*<-- important*/}
35+
{/* import: register the field in the validation context */}
36+
<ValidationContextRegister field={this} />
3637

3738
{/* isRequired: check if validation prop contains the required rule */}
3839
<label>{label} {this.isRequired ? '*' : ''}</label>
3940
<input
4041
name={name}
41-
value={this.mask.apply(value)}
42+
value={this.getMaskedValue}
4243
onChange={this.onChange}
4344
/>
4445

45-
{/* errorMessage will null if submitted and touched are false */}
46+
{/* errorMessage will be null if submitted and touched are false */}
4647
{this.errorMessage ? <p class="error">{this.errorMessage}</p> : null}
4748
</Fragment>
4849
);

components/FieldCoreBase.tsx

+8-25
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ export interface IPropsFieldBase {
2323
children?: React.ReactNode;
2424
}
2525

26-
export default class FieldCoreBase<
26+
export default abstract class FieldCoreBase<
2727
P extends IPropsFieldBase = IPropsFieldBase,
2828
S extends IStateFieldBase = IStateFieldBase
2929
> extends React.PureComponent<P, S> {
30-
31-
protected validationContext: IFieldValidationContext;
30+
static contextType = FieldValidation;
31+
context: IFieldValidationContext;
3232

3333
constructor(props: any) {
3434
super(props);
@@ -58,6 +58,10 @@ export default class FieldCoreBase<
5858
return mask;
5959
}
6060

61+
get getMaskedValue() {
62+
return this.mask.apply(this.props.value);
63+
}
64+
6165
static getDerivedStateFromProps({ name, value, validation, validationContext, children, submitted }: IPropsFieldBase, currentState: IStateFieldBase): IStateFieldBase {
6266
const customMessages = React.Children
6367
.toArray(children)
@@ -81,7 +85,7 @@ export default class FieldCoreBase<
8185
}
8286

8387
public componentWillUnmount() {
84-
this.validationContext && this.validationContext.unregister(this);
88+
this.context && this.context.unregister(this);
8589
}
8690

8791
public setFormSubmitted = (submitted: boolean) => {
@@ -95,25 +99,4 @@ export default class FieldCoreBase<
9599
public isValid = () => {
96100
return !this.state.errorMessage && !this.props.errorMessage;
97101
}
98-
99-
public setContext = (newContext: IFieldValidationContext): React.ReactNode => {
100-
if (newContext === this.validationContext) return null;
101-
102-
this.validationContext && this.validationContext.unregister(this);
103-
104-
if (newContext) {
105-
this.validationContext = newContext;
106-
this.validationContext.register(this);
107-
}
108-
109-
return null;
110-
}
111-
112-
public render() {
113-
return (
114-
<FieldValidation.Consumer>
115-
{this.setContext}
116-
</FieldValidation.Consumer>
117-
);
118-
}
119102
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as React from 'react';
2+
3+
import { FieldValidation, IFieldValidationContext } from '../validator/context';
4+
import FieldCoreBase from './FieldCoreBase';
5+
6+
interface IProps {
7+
field: FieldCoreBase;
8+
}
9+
10+
export default class ValidationContextRegister extends React.PureComponent<IProps, {}> {
11+
private validationContext: IFieldValidationContext;
12+
13+
private setContext = (newContext: IFieldValidationContext): React.ReactNode => {
14+
if (newContext === this.validationContext) return null;
15+
16+
this.validationContext && this.validationContext.unregister(this.props.field);
17+
18+
if (newContext) {
19+
this.validationContext = newContext;
20+
this.validationContext.register(this.props.field);
21+
}
22+
23+
return null;
24+
}
25+
26+
public componentWillMount() {
27+
this.validationContext && this.validationContext.unregister(this.props.field);
28+
}
29+
30+
public render(): React.ReactNode {
31+
return (
32+
<FieldValidation.Consumer>
33+
{this.setContext}
34+
</FieldValidation.Consumer>
35+
)
36+
}
37+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"validation",
1010
"material"
1111
],
12-
"version": "1.1.0",
12+
"version": "1.3.0",
1313
"main": "./index.js",
1414
"types": "./index.d.ts",
1515
"license": "MIT",

0 commit comments

Comments
 (0)