Skip to content

Commit 6cad9c3

Browse files
author
undefined
committed
feat: udpate formmodel
1 parent a3c8930 commit 6cad9c3

File tree

6 files changed

+101
-102
lines changed

6 files changed

+101
-102
lines changed

components/_util/vnode.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { filterEmpty } from './props-util';
22
import { cloneVNode } from 'vue';
33

4-
export function cloneElement(n, nodeProps = {}, override = true) {
5-
let ele = n;
6-
if (Array.isArray(n)) {
7-
ele = filterEmpty(n)[0];
4+
export function cloneElement(vnode, nodeProps = {}, override = true) {
5+
let ele = vnode;
6+
if (Array.isArray(vnode)) {
7+
ele = filterEmpty(vnode)[0];
88
}
99
if (!ele) {
1010
return null;
@@ -15,3 +15,7 @@ export function cloneElement(n, nodeProps = {}, override = true) {
1515
node.props = override ? { ...node.props, ...nodeProps } : node.props;
1616
return node;
1717
}
18+
19+
export function cloneVNodes(vnodes, nodeProps = {}, override = true) {
20+
return vnodes.map(vnode => cloneElement(vnode, nodeProps, override));
21+
}

components/form-model/Form.jsx

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { inject, provide } from 'vue';
12
import PropTypes from '../_util/vue-types';
23
import classNames from 'classnames';
34
import { ColProps } from '../grid/Col';
45
import isRegExp from 'lodash/isRegExp';
56
import warning from '../_util/warning';
67
import FormItem from './FormItem';
7-
import { initDefaultProps, getListeners } from '../_util/props-util';
8+
import { initDefaultProps, getListeners, getSlot } from '../_util/props-util';
89
import { ConfigConsumerProps } from '../config-provider';
910

1011
export const FormProps = {
@@ -48,6 +49,7 @@ export const ValidationRule = {
4849

4950
const Form = {
5051
name: 'AFormModel',
52+
inheritAttrs: false,
5153
props: initDefaultProps(FormProps, {
5254
layout: 'horizontal',
5355
hideRequiredMark: false,
@@ -56,15 +58,14 @@ const Form = {
5658
Item: FormItem,
5759
created() {
5860
this.fields = [];
61+
this.form = undefined;
62+
provide('FormContext', this);
5963
},
60-
provide() {
64+
setup() {
6165
return {
62-
FormContext: this,
66+
configProvider: inject('configProvider', ConfigConsumerProps),
6367
};
6468
},
65-
inject: {
66-
configProvider: { default: () => ConfigConsumerProps },
67-
},
6869
watch: {
6970
rules() {
7071
if (this.validateOnRuleChange) {
@@ -164,19 +165,20 @@ const Form = {
164165
},
165166

166167
render() {
167-
const { prefixCls: customizePrefixCls, hideRequiredMark, layout, onSubmit, $slots } = this;
168+
const { prefixCls: customizePrefixCls, hideRequiredMark, layout, onSubmit } = this;
168169
const getPrefixCls = this.configProvider.getPrefixCls;
169170
const prefixCls = getPrefixCls('form', customizePrefixCls);
171+
const { class: className, onSubmit: originSubmit, ...restProps } = this.$attrs;
170172

171-
const formClassName = classNames(prefixCls, {
173+
const formClassName = classNames(prefixCls, className, {
172174
[`${prefixCls}-horizontal`]: layout === 'horizontal',
173175
[`${prefixCls}-vertical`]: layout === 'vertical',
174176
[`${prefixCls}-inline`]: layout === 'inline',
175177
[`${prefixCls}-hide-required-mark`]: hideRequiredMark,
176178
});
177179
return (
178-
<form onSubmit={onSubmit} class={formClassName}>
179-
{$slots.default}
180+
<form onSubmit={onSubmit} class={formClassName} {...restProps}>
181+
{getSlot(this)}
180182
</form>
181183
);
182184
},

components/form-model/FormItem.jsx

+37-37
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import { inject } from 'vue';
12
import AsyncValidator from 'async-validator';
23
import cloneDeep from 'lodash/cloneDeep';
34
import PropTypes from '../_util/vue-types';
45
import { ColProps } from '../grid/Col';
56
import {
67
initDefaultProps,
7-
getComponentFromProp,
8+
getComponent,
89
getOptionProps,
910
getEvents,
10-
filterEmpty,
1111
isValidElement,
12+
getSlot,
1213
} from '../_util/props-util';
1314
import BaseMixin from '../_util/BaseMixin';
1415
import { ConfigConsumerProps } from '../config-provider';
@@ -63,15 +64,18 @@ export const FormItemProps = {
6364

6465
export default {
6566
name: 'AFormModelItem',
66-
__ANT_NEW_FORM_ITEM: true,
6767
mixins: [BaseMixin],
68+
inheritAttrs: false,
69+
__ANT_NEW_FORM_ITEM: true,
6870
props: initDefaultProps(FormItemProps, {
6971
hasFeedback: false,
7072
autoLink: true,
7173
}),
72-
inject: {
73-
configProvider: { default: () => ConfigConsumerProps },
74-
FormContext: { default: () => ({}) },
74+
setup() {
75+
return {
76+
configProvider: inject('configProvider', ConfigConsumerProps),
77+
FormContext: inject('FormContext', {}),
78+
};
7579
},
7680
data() {
7781
return {
@@ -216,43 +220,39 @@ export default {
216220
},
217221
},
218222
render() {
219-
const { $slots, $scopedSlots } = this;
220-
const props = getOptionProps(this);
221-
const label = getComponentFromProp(this, 'label');
222-
const extra = getComponentFromProp(this, 'extra');
223-
const help = getComponentFromProp(this, 'help');
223+
const { autoLink, ...props } = getOptionProps(this);
224+
const label = getComponent(this, 'label');
225+
const extra = getComponent(this, 'extra');
226+
const help = getComponent(this, 'help');
224227
const formProps = {
225-
props: {
226-
...props,
227-
label,
228-
extra,
229-
validateStatus: this.validateState,
230-
help: this.validateMessage || help,
231-
required: this.isRequired || props.required,
232-
},
228+
...this.$attrs,
229+
...props,
230+
label,
231+
extra,
232+
validateStatus: this.validateState,
233+
help: this.validateMessage || help,
234+
required: this.isRequired || props.required,
233235
};
234-
const children = filterEmpty($scopedSlots.default ? $scopedSlots.default() : $slots.default);
236+
const children = getSlot(this);
235237
let firstChildren = children[0];
236-
if (this.prop && this.autoLink && isValidElement(firstChildren)) {
238+
if (this.prop && autoLink && isValidElement(firstChildren)) {
237239
const originalEvents = getEvents(firstChildren);
238-
const originalBlur = originalEvents.blur;
239-
const originalChange = originalEvents.change;
240+
const originalBlur = originalEvents.onBlur;
241+
const originalChange = originalEvents.onChange;
240242
firstChildren = cloneElement(firstChildren, {
241-
on: {
242-
blur: (...args) => {
243-
originalBlur && originalBlur(...args);
244-
this.onFieldBlur();
245-
},
246-
change: (...args) => {
247-
if (Array.isArray(originalChange)) {
248-
for (let i = 0, l = originalChange.length; i < l; i++) {
249-
originalChange[i](...args);
250-
}
251-
} else if (originalChange) {
252-
originalChange(...args);
243+
onBlur: (...args) => {
244+
originalBlur && originalBlur(...args);
245+
this.onFieldBlur();
246+
},
247+
onChange: (...args) => {
248+
if (Array.isArray(originalChange)) {
249+
for (let i = 0, l = originalChange.length; i < l; i++) {
250+
originalChange[i](...args);
253251
}
254-
this.onFieldChange();
255-
},
252+
} else if (originalChange) {
253+
originalChange(...args);
254+
}
255+
this.onFieldChange();
256256
},
257257
});
258258
}

components/form-model/index.jsx

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import Vue from 'vue';
21
import Form from './Form';
3-
import ref from 'vue-ref';
4-
import FormDecoratorDirective from '../_util/FormDecoratorDirective';
5-
import Base from '../base';
6-
7-
Vue.use(ref, { name: 'ant-ref' });
8-
Vue.use(FormDecoratorDirective);
92

103
export { FormProps, ValidationRule } from './Form';
114
export { FormItemProps } from './FormItem';
125

136
/* istanbul ignore next */
14-
Form.install = function(Vue) {
15-
Vue.use(Base);
16-
Vue.component(Form.name, Form);
17-
Vue.component(Form.Item.name, Form.Item);
7+
Form.install = function(app) {
8+
app.component(Form.name, Form);
9+
app.component(Form.Item.name, Form.Item);
1810
};
1911

2012
export default Form;

0 commit comments

Comments
 (0)