Skip to content

Commit 29b9c51

Browse files
committed
feat: vc-form
1 parent 81048b9 commit 29b9c51

File tree

4 files changed

+72
-29
lines changed

4 files changed

+72
-29
lines changed

components/vc-form/src/createBaseForm.jsx

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
flattenArray,
1919
} from './utils'
2020

21-
const DEFAULT_TRIGGER = 'onChange'
21+
const DEFAULT_TRIGGER = 'change'
2222

2323
function createBaseForm (option = {}, mixins = []) {
2424
const {
@@ -94,7 +94,7 @@ function createBaseForm (option = {}, mixins = []) {
9494
const valuesAllSet = {}
9595
valuesAll[name] = value
9696
Object.keys(valuesAll).forEach(key => set(valuesAllSet, key, valuesAll[key]))
97-
onValuesChange(this.props, set({}, name, value), valuesAllSet)
97+
onValuesChange(this.$props, set({}, name, value), valuesAllSet)
9898
}
9999
const field = this.fieldsStore.getField(name)
100100
return ({ name, field: { ...field, value, touched: true }, fieldMeta })
@@ -148,7 +148,7 @@ function createBaseForm (option = {}, mixins = []) {
148148
},
149149

150150
getFieldDecorator (name, fieldOption) {
151-
const { ref, ...restProps } = this.getFieldProps(name, fieldOption)
151+
const { directives, props } = this.getFieldProps(name, fieldOption)
152152
return (fieldElem) => {
153153
const fieldMeta = this.fieldsStore.getFieldMeta(name)
154154
const originalProps = fieldElem.props
@@ -173,10 +173,10 @@ function createBaseForm (option = {}, mixins = []) {
173173
fieldMeta.ref = fieldElem.ref
174174
return cloneElement(fieldElem, {
175175
props: {
176-
...restProps,
176+
...props,
177177
...this.fieldsStore.getFieldValuePropValue(fieldMeta),
178178
},
179-
ref,
179+
directives,
180180
})
181181
}
182182
},
@@ -220,25 +220,10 @@ function createBaseForm (option = {}, mixins = []) {
220220

221221
const inputProps = {
222222
...this.fieldsStore.getFieldValuePropValue(fieldOption),
223-
ref: name,
223+
// ref: name,
224+
on: {},
224225
}
225226
const saveRef = this.getCacheBind(name, `${name}__ref`, this.saveRef)
226-
this.$nextTick(() => {
227-
if (this.instances[name] !== this.$refs[name]) {
228-
this.$refs[name].destroyed = () => {
229-
this.$refs[name].destroyed()
230-
// after destroy, delete data
231-
this.clearedFieldMetaCache[name] = {
232-
field: this.fieldsStore.getField(name),
233-
meta: this.fieldsStore.getFieldMeta(name),
234-
}
235-
this.fieldsStore.clearField(name)
236-
delete this.instances[name]
237-
delete this.cachedBind[name]
238-
}
239-
}
240-
saveRef(name, `${name}__ref`, this.$refs[name])
241-
})
242227
if (fieldNameProp) {
243228
inputProps[fieldNameProp] = name
244229
}
@@ -252,7 +237,7 @@ function createBaseForm (option = {}, mixins = []) {
252237

253238
// make sure that the value will be collect
254239
if (trigger && validateTriggers.indexOf(trigger) === -1) {
255-
inputProps[trigger] = this.getCacheBind(name, trigger, this.onCollect)
240+
inputProps.on[trigger] = this.getCacheBind(name, trigger, this.onCollect)
256241
}
257242

258243
const meta = {
@@ -269,7 +254,14 @@ function createBaseForm (option = {}, mixins = []) {
269254
inputProps[fieldDataProp] = this.fieldsStore.getField(name)
270255
}
271256

272-
return inputProps
257+
return {
258+
props: inputProps,
259+
directives: [
260+
{ name: 'ant-form-item-ref-cal', value: (component) => {
261+
saveRef(component)
262+
} },
263+
],
264+
}
273265
},
274266

275267
getFieldInstance (name) {
@@ -289,9 +281,12 @@ function createBaseForm (option = {}, mixins = []) {
289281
if (onFieldsChange) {
290282
const changedFields = Object.keys(fields)
291283
.reduce((acc, name) => set(acc, name, this.fieldsStore.getField(name)), {})
292-
onFieldsChange(this.props, changedFields, this.fieldsStore.getNestedAllFields())
284+
onFieldsChange(this.$props, changedFields, this.fieldsStore.getNestedAllFields())
293285
}
294-
this.forceUpdate(callback)
286+
this.$forceUpdate()
287+
this.$nextTick(() => {
288+
callback && callback()
289+
})
295290
},
296291

297292
resetFields (ns) {
@@ -330,11 +325,22 @@ function createBaseForm (option = {}, mixins = []) {
330325
this.setFields(newFields, callback)
331326
if (onValuesChange) {
332327
const allValues = this.fieldsStore.getAllValues()
333-
onValuesChange(this.props, changedValues, allValues)
328+
onValuesChange(this.$props, changedValues, allValues)
334329
}
335330
},
336331

337332
saveRef (name, _, component) {
333+
if (!component) {
334+
// after destroy, delete data
335+
this.clearedFieldMetaCache[name] = {
336+
field: this.fieldsStore.getField(name),
337+
meta: this.fieldsStore.getFieldMeta(name),
338+
}
339+
this.fieldsStore.clearField(name)
340+
delete this.instances[name]
341+
delete this.cachedBind[name]
342+
return
343+
}
338344
this.recoverClearedField(name)
339345
const fieldMeta = this.fieldsStore.getFieldMeta(name)
340346
if (fieldMeta) {

components/vc-form/src/index.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,15 @@
22
import createForm from './createForm'
33
import createFormField from './createFormField'
44
import formShape from './propTypes'
5+
import Vue from 'vue'
6+
7+
Vue.directive('ant-form-item-ref-cal', {
8+
inserted: function (el, binding, vnode) {
9+
binding.value(vnode)
10+
},
11+
unbind: function (el, binding, vnode) {
12+
binding.value()
13+
},
14+
})
515

616
export { createForm, createFormField, formShape }

package-lock.json

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"babel-plugin-istanbul": "^4.1.1",
6262
"babel-plugin-syntax-dynamic-import": "^6.18.0",
6363
"babel-plugin-syntax-jsx": "^6.18.0",
64+
"babel-plugin-transform-class-properties": "^6.24.1",
6465
"babel-plugin-transform-decorators": "^6.24.1",
6566
"babel-plugin-transform-decorators-legacy": "^1.3.4",
6667
"babel-plugin-transform-es3-member-expression-literals": "^6.22.0",
@@ -81,8 +82,8 @@
8182
"css-loader": "^0.28.7",
8283
"deep-assign": "^2.0.0",
8384
"eslint": "^4.7.2",
84-
"eslint-plugin-vue": "^3.13.0",
8585
"eslint-plugin-html": "^3.2.2",
86+
"eslint-plugin-vue": "^3.13.0",
8687
"eslint-plugin-vue-libs": "^1.2.1",
8788
"extract-text-webpack-plugin": "^3.0.2",
8889
"fetch-jsonp": "^1.1.3",

0 commit comments

Comments
 (0)