diff --git a/components/vc-form/demo/parallel-form.js b/components/vc-form/demo/parallel-form.js new file mode 100644 index 000000000..d4403d835 --- /dev/null +++ b/components/vc-form/demo/parallel-form.js @@ -0,0 +1,85 @@ +/* eslint react/no-multi-comp:0, no-console:0 */ + +import { createForm } from '../index' +import { regionStyle } from './styles' +import { Switch } from 'antd' + +const TopForm = { + props: { + form: Object, + }, + render () { + const { getFieldProps } = this.form + return (
+
has email?
+
+ +
+
) + }, +} + +const BottomForm = { + props: { + form: Object, + on: Boolean, + }, + render () { + const { form } = this + const on = form.getFieldValue('on') + const style = { + ...regionStyle, + display: on ? 'block' : 'none', + } + return (
+
email:
+
+ +
+
) + }, +} + +const Form = { + props: { + form: Object, + }, + methods: { + onSubmit (e) { + e.preventDefault() + console.log(this.form.getFieldsValue()) + }, + }, + + render () { + const { form } = this + return (
+ + +
+ +
+
) + }, +} + +const NewForm = createForm()(Form) + +export default { + render () { + return (
+

parallel form

+ +
) + }, +} diff --git a/components/vc-form/demo/start-end-date.js b/components/vc-form/demo/start-end-date.js new file mode 100644 index 000000000..80993ad73 --- /dev/null +++ b/components/vc-form/demo/start-end-date.js @@ -0,0 +1,91 @@ +/* eslint no-console:0 */ + +import { DatePicker } from 'antd' +import createDOMForm from '../src/createDOMForm' +import { regionStyle, errorStyle } from './styles' + +const Form = { + props: { + form: Object, + }, + methods: { + onSubmit (e) { + console.log('submit') + e.preventDefault() + this.form.validateFieldsAndScroll((error, values) => { + if (!error) { + console.log('ok', values) + } else { + console.log('error', error, values) + } + }) + }, + + reset (e) { + e.preventDefault() + this.form.resetFields() + }, + + checkStart (rule, value, callback) { + const { validateFields } = this.form + validateFields(['end'], { + force: true, + }) + callback() + }, + + checkEnd (rule, value, callback) { + const end = value + const { getFieldValue } = this.form + const start = getFieldValue('start') + if (!end || !start) { + callback('please select both start and end time') + } else if (end.valueOf() < start.valueOf()) { + callback('start time should be less than end time') + } else { + callback() + } + }, + }, + + render () { + const { form } = this + const { getFieldProps, getFieldError } = form + return (
+

startTime and endTime validation

+
+
+
start:
+
+ +
+
+ +
+
end:
+
+ +
+
+ +
+ {getFieldError('end') ? getFieldError('end').join(',') : ''} +
+ +
+ +   + +
+
+
) + }, +} + +export default createDOMForm()(Form) diff --git a/components/vc-form/demo/suggest.js b/components/vc-form/demo/suggest.js new file mode 100644 index 000000000..9d50e76fc --- /dev/null +++ b/components/vc-form/demo/suggest.js @@ -0,0 +1,120 @@ +/* eslint react/no-multi-comp:0, no-console:0 */ + +import { createForm } from '../index' +import { Select } from 'antd' +import { regionStyle, errorStyle } from './styles' +import { mergeProps } from '../../_util/props-util' +const emailTpl = ['@gmail.com', '@outlook.com', '@qq.com'] +const { Option } = Select +const CustomInput = { + props: { + form: Object, + }, + data () { + return { + data: [], + } + }, + methods: { + onChange (v) { + if (v.indexOf('@') === -1) { + this.data = emailTpl.map(m => v + m) + } else if (this.data.length) { + this.data = [] + } + }, + }, + + render () { + const { getFieldProps, getFieldError, isFieldValidating } = this.form + const errors = getFieldError('select') + return (
+
custom select sync validate
+
+ +
+
+ {(errors) ? errors.join(',') + : + 1 + } +
+
+ {isFieldValidating('select') ? 'validating' : + 1 + } +
+
) + }, +} + +const Form = { + props: { + form: Object, + }, + methods: { + onSubmit (e) { + e.preventDefault() + this.form.validateFields((error, values) => { + if (!error) { + console.log('ok', values) + } else { + console.log('error', error, values) + } + }) + }, + }, + + render () { + const { form } = this + return (
+

suggest

+
+ + +
+ +
+ +
) + }, +} + +export default createForm()(Form) + diff --git a/components/vc-form/demo/validateFirst.js b/components/vc-form/demo/validateFirst.js new file mode 100644 index 000000000..184e5ef90 --- /dev/null +++ b/components/vc-form/demo/validateFirst.js @@ -0,0 +1,132 @@ +/* eslint react/no-multi-comp:0, no-console:0 */ + +import { createForm } from '../index' +import { regionStyle, errorStyle } from './styles' + +const Email = { + props: { + form: Object, + }, + render () { + const { getFieldProps, getFieldError, isFieldValidating } = this.form + const errors = getFieldError('email') + return (
+
email sync validate
+
+ +
+
+ {errors ? errors.join(',') : null} +
+
+ {isFieldValidating('email') ? 'validating' : null} +
+
) + }, + +} + +const User = { + props: { + form: Object, + }, + methods: { + userExists (rule, value, callback) { + setTimeout(() => { + if (value === '1') { + callback([new Error('are you kidding?')]) + } else if (value === 'yiminghe') { + callback([new Error('forbid yiminghe')]) + } else { + callback() + } + }, 300) + }, + }, + + render () { + const { getFieldProps, getFieldError, isFieldValidating } = this.form + const errors = getFieldError('user') + return (
+
user async validate
+
+ +
+
+ {(errors) ? errors.join(',') : null} +
+
+ {isFieldValidating('user') ? 'validating' : null} +
+
) + }, +} + +const Form = { + methods: { + onSubmit (e) { + console.log('submit') + e.preventDefault() + this.form.validateFields({ + // firstFields: false, + }, (error, values) => { + if (!error) { + console.log('ok', values) + } else { + console.log('error', error, values) + } + }) + }, + + reset (e) { + e.preventDefault() + this.form.resetFields() + }, + }, + + render () { + const { form } = this + return (
+

validateFirst

+
+ + + + +
+ +   + +
+ +
) + }, +} + +export default createForm()(Form) + diff --git a/components/vc-form/src/createBaseForm.jsx b/components/vc-form/src/createBaseForm.jsx index dd482bf80..d5016a94d 100644 --- a/components/vc-form/src/createBaseForm.jsx +++ b/components/vc-form/src/createBaseForm.jsx @@ -96,7 +96,7 @@ function createBaseForm (option = {}, mixins = []) { const valuesAllSet = {} valuesAll[name] = value Object.keys(valuesAll).forEach(key => set(valuesAllSet, key, valuesAll[key])) - onValuesChange(this.$props, set({}, name, value), valuesAllSet) + onValuesChange(this, set({}, name, value), valuesAllSet) } const field = this.fieldsStore.getField(name) return ({ name, field: { ...field, value, touched: true }, fieldMeta }) @@ -300,7 +300,7 @@ function createBaseForm (option = {}, mixins = []) { if (onFieldsChange) { const changedFields = Object.keys(fields) .reduce((acc, name) => set(acc, name, this.fieldsStore.getField(name)), {}) - onFieldsChange(this.$props, changedFields, this.fieldsStore.getNestedAllFields()) + onFieldsChange(this, changedFields, this.fieldsStore.getNestedAllFields()) } this.$forceUpdate() this.$nextTick(() => { @@ -344,7 +344,7 @@ function createBaseForm (option = {}, mixins = []) { this.setFields(newFields, callback) if (onValuesChange) { const allValues = this.fieldsStore.getAllValues() - onValuesChange(this.$props, changedValues, allValues) + onValuesChange(this, changedValues, allValues) } },