ant-design-vue/components/vc-form/demo/normalize.js

162 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-05-04 08:02:31 +00:00
/* eslint react/no-multi-comp:0, no-console:0 */
2019-01-12 03:33:27 +00:00
import { createForm } from '../index';
import { regionStyle, errorStyle } from './styles';
2018-05-04 08:02:31 +00:00
const CustomInput = {
props: {
form: Object,
},
2019-01-12 03:33:27 +00:00
data() {
2018-05-04 08:02:31 +00:00
return {
data: [],
2019-01-12 03:33:27 +00:00
};
2018-05-04 08:02:31 +00:00
},
methods: {
2019-01-12 03:33:27 +00:00
checkUpper(rule, value = '', callback) {
2018-05-04 08:02:31 +00:00
if (value !== value.toUpperCase()) {
2019-01-12 03:33:27 +00:00
callback(new Error('need to be upper!'));
2018-05-04 08:02:31 +00:00
} else {
2019-01-12 03:33:27 +00:00
callback();
2018-05-04 08:02:31 +00:00
}
},
2019-01-12 03:33:27 +00:00
toUpper(v, prev) {
2018-05-04 08:02:31 +00:00
if (v === prev) {
2019-01-12 03:33:27 +00:00
return v;
2018-05-04 08:02:31 +00:00
}
2019-01-12 03:33:27 +00:00
return v.toUpperCase();
2018-05-04 08:02:31 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldProps, getFieldError } = this.form;
const errors = getFieldError('upper');
return (
<div style={regionStyle}>
<div>upper normalize</div>
<div>
<input
{...getFieldProps('upper', {
normalize: this.toUpper,
rules: [
{
validator: this.checkUpper,
},
],
})}
/>
</div>
<div style={errorStyle}>{errors ? errors.join(',') : null}</div>
2018-05-04 08:02:31 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 08:02:31 +00:00
const MaxMin = {
props: {
form: Object,
},
methods: {
2019-01-12 03:33:27 +00:00
normalizeMin(value, prevValue, allValues) {
console.log('normalizeMin', allValues.min, allValues.max);
const previousAllValues = this.form.getFieldsValue();
2018-05-04 08:02:31 +00:00
if (allValues.max !== previousAllValues.max) {
// max changed
if (value === '' || Number(allValues.max) < Number(value)) {
2019-01-12 03:33:27 +00:00
return allValues.max;
2018-05-04 08:02:31 +00:00
}
}
2019-01-12 03:33:27 +00:00
return value;
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
normalizeMax(value, prevValue, allValues) {
console.log('normalizeMax', allValues.min, allValues.max);
const previousAllValues = this.form.getFieldsValue();
2018-05-04 08:02:31 +00:00
if (allValues.min !== previousAllValues.min) {
// min changed
if (value === '' || Number(allValues.min) > Number(value)) {
2019-01-12 03:33:27 +00:00
return allValues.min;
2018-05-04 08:02:31 +00:00
}
}
2019-01-12 03:33:27 +00:00
return value;
2018-05-04 08:02:31 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldProps } = this.form;
return (
<div style={regionStyle}>
<div>
min:{' '}
<select
{...getFieldProps('min', {
normalize: this.normalizeMin,
initialValue: '',
})}
>
<option value="">empty</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div>
max:{' '}
<select
{...getFieldProps('max', {
initialValue: '',
normalize: this.normalizeMax,
})}
>
<option value="">empty</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
2018-05-04 08:02:31 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 08:02:31 +00:00
const Form = {
// props: {
// form: Object,
// },
methods: {
2019-01-12 03:33:27 +00:00
onSubmit(e) {
e.preventDefault();
2018-05-04 08:02:31 +00:00
this.form.validateFields((error, values) => {
if (!error) {
2019-01-12 03:33:27 +00:00
console.log('ok', values);
2018-05-04 08:02:31 +00:00
} else {
2019-01-12 03:33:27 +00:00
console.log('error', error, values);
2018-05-04 08:02:31 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-05-04 08:02:31 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { form } = this;
return (
<div style={{ margin: '20px' }}>
<h2>normalize</h2>
<form onSubmit={this.onSubmit}>
<CustomInput form={form} />
2018-05-04 08:02:31 +00:00
2019-01-12 03:33:27 +00:00
<MaxMin form={form} />
2018-05-04 08:02:31 +00:00
2019-01-12 03:33:27 +00:00
<div style={regionStyle}>
<button>submit</button>
</div>
</form>
</div>
);
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 08:02:31 +00:00
2019-01-12 03:33:27 +00:00
export default createForm()(Form);