ant-design-vue/components/vc-form/demo/start-end-date.js

88 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-05-04 11:11:42 +00:00
/* eslint no-console:0 */
2019-01-12 03:33:27 +00:00
import { DatePicker } from 'ant-design-vue';
import createDOMForm from '../src/createDOMForm';
import { regionStyle, errorStyle } from './styles';
2018-05-04 11:11:42 +00:00
const Form = {
props: {
form: Object,
},
methods: {
2019-01-12 03:33:27 +00:00
onSubmit(e) {
console.log('submit');
e.preventDefault();
this.form.validateFieldsAndScroll((error, values) => {
if (!error) {
console.log('ok', values);
} else {
console.log('error', error, values);
}
});
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
reset(e) {
e.preventDefault();
this.form.resetFields();
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
checkStart(rule, value, callback) {
const { validateFields } = this.form;
validateFields(['end'], { force: true });
callback();
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
checkEnd(rule, value, callback) {
const end = value;
const { getFieldValue } = this.form;
const start = getFieldValue('start');
2018-05-04 11:11:42 +00:00
if (!end || !start) {
2019-01-12 03:33:27 +00:00
callback('please select both start and end time');
2018-05-04 11:11:42 +00:00
} else if (end.valueOf() < start.valueOf()) {
2019-01-12 03:33:27 +00:00
callback('start time should be less than end time');
2018-05-04 11:11:42 +00:00
} else {
2019-01-12 03:33:27 +00:00
callback();
2018-05-04 11:11:42 +00:00
}
},
},
2019-01-12 03:33:27 +00:00
render() {
const { form } = this;
const { getFieldProps, getFieldError } = form;
return (
2019-01-12 03:33:27 +00:00
<div
style={{
margin: 20,
}}
>
<h2>startTime and endTime validation</h2>
<form onSubmit={this.onSubmit}>
<div style={regionStyle}>
2019-01-12 03:33:27 +00:00
<div>start:</div>
<div>
2019-01-12 03:33:27 +00:00
<DatePicker {...getFieldProps('start', { rules: [this.checkStart] })} />
</div>
2018-05-04 11:11:42 +00:00
</div>
<div style={regionStyle}>
2019-01-12 03:33:27 +00:00
<div>end:</div>
<div>
2019-01-12 03:33:27 +00:00
<DatePicker {...getFieldProps('end', { rules: [this.checkEnd] })} />
</div>
2018-05-04 11:11:42 +00:00
</div>
2019-01-12 03:33:27 +00:00
<div style={errorStyle}>{getFieldError('end') ? getFieldError('end').join(',') : ''}</div>
2018-05-04 11:11:42 +00:00
<div style={regionStyle}>
<button onClick={this.reset}>reset</button>
&nbsp;
2019-01-12 03:33:27 +00:00
<input type="submit" value="submit" />
</div>
</form>
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 11:11:42 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 11:11:42 +00:00
2019-01-12 03:33:27 +00:00
export default createDOMForm()(Form);