forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhance.jsx
116 lines (100 loc) · 3.31 KB
/
enhance.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function getCfgFromProps(props, type) {
if (type in props) {
return props[type];
}
return undefined;
}
function getRule(ruleName, props) {
return {
[ruleName]: props[ruleName],
message: getCfgFromProps(props, `${ruleName}Message`),
trigger: getCfgFromProps(props, `${ruleName}Trigger`),
};
}
function getValueName(props, displayName) {
if (props.valueName) {
return props.valueName;
}
if (typeof displayName === 'string') {
// Next Components are all wrappered by configProvider
const componentName = displayName.replace(/Config\(/g, '').replace(/\)/g, '');
if (['Switch', 'Checkbox', 'Radio'].indexOf(componentName) !== -1) {
return 'checked';
}
}
return 'value';
}
export function getRules(props, labelForErrorMessage) {
const result = [];
// required
if (props.required) {
result.push(getRule('required', props));
}
const maxLength = Number(props.maxLength);
const minLength = Number(props.minLength);
if (minLength || maxLength) {
result.push({
minLength,
maxLength,
// minLengthMessage maxLengthMessage had been deprected, please use minmaxLength. TODO: removed in 2.0
message:
getCfgFromProps(props, 'minmaxLengthMessage') ||
getCfgFromProps(props, 'minLengthMessage') ||
getCfgFromProps(props, 'maxLengthMessage'),
trigger:
getCfgFromProps(props, 'minmaxLengthTrigger') ||
getCfgFromProps(props, 'minLengthTrigger') ||
getCfgFromProps(props, 'maxLengthTrigger'),
});
}
// length
if (props.length) {
result.push(getRule('length', props));
}
// pattern
if (props.pattern) {
result.push(getRule('pattern', props));
}
// format
if (['number', 'tel', 'url', 'email'].indexOf(props.format) > -1) {
result.push(getRule('format', props));
}
const max = Number(props.max);
const min = Number(props.min);
// max min
if (max || min) {
result.push({
min,
max,
// minMessage maxMessage had been deprected, please use minmaxLength. TODO: removed in 2.0
message:
getCfgFromProps(props, 'minmaxMessage') ||
getCfgFromProps(props, 'minMessage') ||
getCfgFromProps(props, 'maxMessage'),
trigger:
getCfgFromProps(props, 'minmaxTrigger') ||
getCfgFromProps(props, 'minTrigger') ||
getCfgFromProps(props, 'maxTrigger'),
});
}
if (props.validator && typeof props.validator === 'function') {
result.push({
validator: props.validator,
trigger: getCfgFromProps(props, 'validatorTrigger'),
});
}
if (labelForErrorMessage) {
result.forEach(r => {
r.aliasName = labelForErrorMessage;
});
}
return result;
}
export function getFieldInitCfg(props, displayName, labelForErrorMessage) {
return {
valueName: getValueName(props, displayName),
trigger: props.trigger ? props.trigger : 'onChange',
autoValidate: props.autoValidate,
rules: getRules(props, labelForErrorMessage),
};
}