Skip to content

Commit d203aee

Browse files
committed
0.0.2-beta
update builds
1 parent d5e8cc8 commit d203aee

File tree

21 files changed

+145
-141
lines changed

21 files changed

+145
-141
lines changed

es/components/Field.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ const shallowequal = require("shallowequal");
1414
const tools_1 = require("../tools");
1515
const Form_1 = require("./Form");
1616
const Validation_1 = require("./Validation");
17-
_a = React.createContext(), exports.Provider = _a.Provider, exports.Consumer = _a.Consumer;
17+
const defaultProps = {
18+
validationErrors: [],
19+
validationScope: [],
20+
rest: {},
21+
formState: {
22+
model: {},
23+
},
24+
};
25+
_a = React.createContext(defaultProps), exports.Provider = _a.Provider, exports.Consumer = _a.Consumer;
1826
class FieldClass extends React.Component {
1927
constructor() {
2028
super(...arguments);
@@ -40,34 +48,38 @@ class FieldClass extends React.Component {
4048
});
4149
};
4250
this.update = (nextValue) => {
43-
const { formState: { handleChange }, name, value, isChanged, isVisited, } = this.props;
51+
const { formState: { handleChange }, name, value, isChanged, isVisited, onChange, } = this.props;
4452
const updValue = Object.assign({ value,
4553
isChanged,
4654
isVisited }, (nextValue || {}));
4755
handleChange(name, updValue);
48-
if (this.props.onChange) {
49-
this.props.onChange(this.props.name, updValue);
56+
if (onChange) {
57+
onChange(name, updValue);
5058
}
5159
};
5260
}
5361
render() {
54-
const { children } = this.props;
55-
const props = Object.assign({}, this.props, { onChange: this.handleChange, onClick: this.onClick });
56-
const rChildren = children
57-
&& typeof children === "function"
58-
? children(props)
59-
: children;
60-
return (React.createElement(exports.Provider, { value: props }, rChildren));
62+
const { value, children } = this.props;
63+
const context = Object.assign({}, this.props, { value: value === undefined ? "" : value, onChange: this.handleChange, onClick: this.onClick });
64+
return (children && typeof children === "function"
65+
? children(context)
66+
: React.createElement(exports.Provider, { value: context }, children));
6167
}
6268
componentDidMount() {
6369
this.update();
6470
}
71+
componentDidUpdate(prevProps) {
72+
if (prevProps.value === undefined) {
73+
this.update();
74+
}
75+
}
6576
shouldComponentUpdate(nextProps) {
66-
const _a = nextProps, { validationErrors: nextErrors, validationScope: nextScope, formState: _, children: __ } = _a, nextRest = __rest(_a, ["validationErrors", "validationScope", "formState", "children"]);
67-
const _b = this.props, { validationErrors, validationScope, formState, children } = _b, rest = __rest(_b, ["validationErrors", "validationScope", "formState", "children"]);
68-
if (!tools_1.isArrayEqual((validationErrors || []).map(error => error.message), (nextErrors || []).map(error => error.message))
69-
|| !tools_1.isArrayEqual((validationScope || []).map(error => error.message), (nextScope || []).map(error => error.message))
70-
|| !shallowequal(nextRest, rest)) {
77+
const { name: nextName, value: nextValue, isVisited: nextIsVisited, isChanged: nextIsChanged, isValid: nextIsValid, validationErrors: nextErrors, validationScope: nextScope, rest: nextRest, } = nextProps;
78+
const { name, value, isVisited, isChanged, isValid, validationErrors, validationScope, rest, } = this.props;
79+
if (!tools_1.isArrayEqual(validationErrors.map(error => error.message), nextErrors.map(error => error.message))
80+
|| !tools_1.isArrayEqual(validationScope.map(error => error.message), nextScope.map(error => error.message))
81+
|| !shallowequal(nextRest, rest)
82+
|| !shallowequal({ nextName, nextValue, nextIsChanged, nextIsValid, nextIsVisited }, { name, value, isVisited, isChanged, isValid })) {
7183
return true;
7284
}
7385
return false;
@@ -78,20 +90,21 @@ class FieldClass extends React.Component {
7890
}
7991
}
8092
}
93+
FieldClass.defaultProps = defaultProps;
8194
exports.FieldClass = FieldClass;
8295
class Field extends React.Component {
8396
render() {
8497
return (React.createElement(Form_1.Consumer, null, (formState) => (React.createElement(Validation_1.Consumer, null, validation => {
85-
const props = this.props;
86-
const modelValue = formState.model[props.name];
87-
const value = modelValue === undefined ? "" : modelValue.value;
98+
const _a = this.props, { name, children, onClick, onChange } = _a, rest = __rest(_a, ["name", "children", "onClick", "onChange"]);
99+
const modelValue = formState.model[name];
100+
const value = modelValue === undefined ? undefined : modelValue.value;
88101
const isChanged = modelValue === undefined ? false : modelValue.isChanged;
89102
const isVisited = modelValue === undefined ? false : modelValue.isVisited;
90103
const isValid = (validation.errors[this.props.name] === undefined
91104
|| validation.errors[this.props.name].length === 0)
92105
&& (validation.scope === undefined || validation.scope.length === 0);
93106
const _Field = FieldClass;
94-
return (React.createElement(_Field, Object.assign({}, props, { value: value, validationErrors: validation.errors[this.props.name], validationScope: validation.scope, formState: formState, isChanged: isChanged, isVisited: isVisited, isValid: isValid })));
107+
return (React.createElement(_Field, { name: name, value: value, validationErrors: validation.errors[this.props.name], validationScope: validation.scope, formState: formState, isChanged: isChanged, isVisited: isVisited, isValid: isValid, onClick: onClick, onChange: onChange, children: children, rest: rest }));
95108
}))));
96109
}
97110
}

es/components/Form.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ exports.defaultConfiguration = {
1616
submitting: {
1717
preventDefault: true,
1818
},
19-
validation: {},
2019
};
21-
_a = React.createContext(), exports.Provider = _a.Provider, exports.Consumer = _a.Consumer;
2220
const EmptyModel = {};
21+
_a = React.createContext({
22+
model: EmptyModel,
23+
configure: exports.defaultConfiguration,
24+
handleReset: () => ({}),
25+
handleChange: () => ({}),
26+
}), exports.Provider = _a.Provider, exports.Consumer = _a.Consumer;
2327
class Form extends React.Component {
2428
constructor(props) {
2529
super(props);
@@ -44,22 +48,18 @@ class Form extends React.Component {
4448
onReset();
4549
}
4650
if (!this.props.values) {
47-
this.setState(({ model }) => {
48-
const nextModel = form_1.resetModel(model);
49-
return {
50-
model: nextModel,
51-
};
52-
});
51+
this.setState(({ model }) => ({
52+
model: form_1.resetModel(model),
53+
}));
5354
}
5455
};
5556
this.handleChange = (field, value) => {
56-
this.setState((prev, props) => {
57+
this.setState(prev => {
5758
if (prev.model[field] && shallowequal(prev.model[field], value)) {
5859
return null;
5960
}
60-
const nextModel = Object.assign({}, prev.model, { [field]: Object.assign({}, value) });
6161
return {
62-
model: nextModel,
62+
model: Object.assign({}, prev.model, { [field]: Object.assign({}, value) }),
6363
};
6464
});
6565
};
@@ -73,13 +73,11 @@ class Form extends React.Component {
7373
}
7474
static getDerivedStateFromProps(props, state) {
7575
const { values, configure } = props;
76-
let nextState = null;
7776
const model = props.isReset ? form_1.resetModel(state.model) : state.model;
78-
nextState = {
77+
return {
7978
model: values ? form_1.updateModel(values, model) : model,
80-
configure: configure || exports.defaultConfiguration,
79+
configure,
8180
};
82-
return nextState;
8381
}
8482
shouldComponentUpdate(nextProps, nextState) {
8583
const _a = this.state, { model } = _a, rest = __rest(_a, ["model"]);

es/components/Validation.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,27 @@ class Validation extends React.Component {
3232
let scope = NoScopeErrors;
3333
let isValid = true;
3434
const model = form_1.getValuesFromModel(form.model);
35-
if (validator && model) {
35+
if (!model) {
36+
return { errors, scope, isValid };
37+
}
38+
if (validator) {
3639
if (validator.validateSync) {
3740
try {
3841
validator.validateSync(model, Object.assign({ abortEarly: false, context: {
3942
state: this.state,
4043
props: this.props,
41-
} }, form.configure.validation));
44+
} }, this.props.configure));
4245
}
43-
catch (_errors) {
44-
const __errors = _errors;
45-
if (__errors.path === undefined) {
46-
__errors.inner.forEach(error => {
46+
catch (validationErrors) {
47+
const _errors = validationErrors;
48+
if (_errors.path === undefined) {
49+
_errors.inner.forEach(error => {
4750
errors = Object.assign({}, errors, { [error.path]: [...(errors[error.path] || []),
4851
...error.errors.map(message => ({ message }))] });
4952
});
5053
}
5154
else {
52-
this.context = __errors.errors;
55+
this.context = _errors.errors;
5356
}
5457
isValid = false;
5558
}
@@ -68,7 +71,7 @@ class Validation extends React.Component {
6871
}
6972
}
7073
}
71-
if (scopeValidator && model) {
74+
if (scopeValidator) {
7275
const preScope = scopeValidator.validate(model, {
7376
state: this.state,
7477
props: this.props,
@@ -92,5 +95,9 @@ class Validation extends React.Component {
9295
return (React.createElement(Form_1.Consumer, null, context => React.createElement(exports.Provider, { value: this.validate(context) }, this.props.children)));
9396
}
9497
}
98+
Validation.defaultProps = {
99+
isValid: true,
100+
configure: {},
101+
};
95102
exports.Validation = Validation;
96103
var _a;

es/helpers/formFactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
const Field_1 = require("../components/Field");
44
const Form_1 = require("../components/Form");
5-
function createFormFactory(defaultValues) {
5+
function createFormFactory() {
66
return {
77
Form: Form_1.Form,
88
Field: Field_1.Field,

es/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ exports.createFormValidator = FormValidator_1.createFormValidator;
2020
exports.FormValidator = FormValidator_1.FormValidator;
2121
exports.createRawFormValidator = FormValidator_1.createRawFormValidator;
2222
var tools_1 = require("./tools");
23-
exports.concat = tools_1.concat;
24-
exports.reduce = tools_1.reduce;
2523
exports.isArrayEqual = tools_1.isArrayEqual;
2624
var ArrayValidator_1 = require("./ArrayValidator");
2725
exports.ArrayValidator = ArrayValidator_1.ArrayValidator;

es/tools.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
function concat(...units) {
4-
return ((...args) => [].concat(...units.map(unit => unit(...args))));
5-
}
6-
exports.concat = concat;
7-
function reduce(...units) {
8-
return ((...args) => units.reduce((prev, unit) => (Object.assign({}, prev, unit(...args))), {}));
9-
}
10-
exports.reduce = reduce;
113
function isArrayEqual(array0, array1) {
124
if (array0 !== array1
135
&& (array0 === undefined || array1 === undefined)) {

lib/components/Field.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@ export interface IFieldClassProps<TName extends keyof TModel, TValue extends TMo
2121
children?: ((state: IFieldClassProps<TName, TValue, TModel>) => React.ReactNode) | React.ReactNode;
2222
onClick?: () => any;
2323
onChange?: (field: string, value: IFieldState<TValue>) => any;
24-
[key: string]: any;
24+
rest: {
25+
[key: string]: any;
26+
};
2527
}
2628
export declare const Provider: React.ComponentClass<{
2729
value: IFieldClassProps<string, any, any>;
2830
}>, Consumer: React.ComponentClass<{
2931
children?: (context: IFieldClassProps<string, any, any>) => React.ReactNode;
3032
}>;
3133
export declare class FieldClass<T> extends React.Component<IClassProps<T>> {
32-
private inputValue;
33-
render(): JSX.Element;
34+
static defaultProps: Partial<IFieldClassProps<string, any, any>>;
35+
render(): {};
3436
componentDidMount(): void;
37+
componentDidUpdate(prevProps: IClassProps<T>): void;
3538
shouldComponentUpdate(nextProps: IClassProps<T>): boolean;
3639
private setVisited();
3740
private onClick;
@@ -40,7 +43,6 @@ export declare class FieldClass<T> extends React.Component<IClassProps<T>> {
4043
}
4144
export interface IFieldProps<TName extends keyof TModel, TValue extends TModel[TName], TModel> {
4245
name: TName;
43-
value?: TValue;
4446
children?: ((state: IClassProps<TModel>) => React.ReactNode) | React.ReactNode;
4547
onClick?: () => any;
4648
onChange?: (field: string, value: IFieldState<TValue>) => any;

lib/components/Field.js

Lines changed: 33 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)