Skip to content

Commit ae5a01e

Browse files
committed
为<Input /> 添加onlyLetterAndNumber属性
1 parent 766e30d commit ae5a01e

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/components/form/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ function getFormItemComponent(that) {
570570
delete otherItemProps.trigger;
571571
delete otherItemProps.aliasLabel;
572572
delete otherItemProps.arrayItemIndexs;
573+
delete otherItemProps.onlyLetterAndNumber;
573574
}
574575
render() {
575576
const { validateStatus, canBeRendered } = this.state;

src/components/input/index.jsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,31 @@ AInput.displayName = 'OriginalAntdComponent';
1111
export default class Input extends BasicComponent {
1212
currentAntdComponent = AInput;
1313
localeKey = 'afcInput';
14+
getOnlyLetterAndNumberRule() {
15+
const locale = this.locale;
16+
return [
17+
{
18+
validator(rule, value, callback, source, options) {
19+
var errors = [];
20+
//数组和字母结合
21+
var pass = new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]*$').test(
22+
value
23+
);
24+
if (!pass && value !== '') {
25+
errors.push({
26+
message: locale.afcPassword.formatErrorMsg,
27+
});
28+
}
29+
callback(errors);
30+
},
31+
},
32+
];
33+
}
1434
getSepcialRuleByType(type) {
1535
//继承父类可能有的rules
1636
const rules = super.getSepcialRuleByType();
1737
const locale = this.locale;
38+
const { onlyLetterAndNumber } = this.props;
1839
switch (type) {
1940
case 'email':
2041
rules.push({
@@ -30,7 +51,11 @@ export default class Input extends BasicComponent {
3051
break;
3152
default:
3253
}
33-
return rules;
54+
if (onlyLetterAndNumber) {
55+
return [...rules, ...this.getOnlyLetterAndNumberRule()];
56+
} else {
57+
return rules;
58+
}
3459
}
3560
}
3661
Input.TextArea = class InputTextArea extends BasicComponent {

src/components/password/index.jsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,6 @@ export default class Password extends BasicComponent {
2626
];
2727
return rules;
2828
}
29-
getOnlyLetterAndNumberRule() {
30-
const locale = this.locale;
31-
return [
32-
{
33-
validator(rule, value, callback, source, options) {
34-
var errors = [];
35-
//数组和字母结合
36-
var pass = new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]*$').test(
37-
value
38-
);
39-
if (!pass && value !== '') {
40-
errors.push({
41-
message: locale.afcPassword.formatErrorMsg,
42-
});
43-
}
44-
callback(errors);
45-
},
46-
},
47-
];
48-
}
4929
onPasswordChange = e => {
5030
const { onChange } = this.props;
5131
this.setState({
@@ -59,7 +39,7 @@ export default class Password extends BasicComponent {
5939
rePassword,
6040
type = 'password',
6141
checkPassword,
62-
rules = [],
42+
onlyLetterAndNumber = true,
6343
...other
6444
} = this.props;
6545
const locale = this.locale;
@@ -68,9 +48,9 @@ export default class Password extends BasicComponent {
6848
<Input
6949
autoComplete="new-password"
7050
type={type}
51+
onlyLetterAndNumber={onlyLetterAndNumber}
7152
{...other}
7253
onChange={this.onPasswordChange}
73-
rules={[...rules, ...this.getOnlyLetterAndNumberRule()]}
7454
/>
7555
{(rePassword || checkPassword) && (
7656
//rePassword视为了兼容之前的formBuilder的api

0 commit comments

Comments
 (0)