Skip to content

Commit ca6ce42

Browse files
authored
feat: V3 form (#4678)
* chore: update husky * perf: update formItem * perf: useInjectFormItemContext
1 parent 989d1d4 commit ca6ce42

File tree

38 files changed

+337
-133
lines changed

38 files changed

+337
-133
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ _site
6363
yarn.lock
6464
package-lock.json
6565
/coverage
66-
.husky
6766

6867
# 备份文件
6968
/components/test/*

.husky/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/pre-commit

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx --no-install pretty-quick --staged

components/auto-complete/__tests__/__snapshots__/demo.test.js.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`renders ./components/auto-complete/demo/basic.vue correctly 1`] = `
44
<div style="width: 200px;" class="ant-select ant-select-show-search ant-select-auto-complete ant-select-single ant-select-customize-input ant-select-show-search">
55
<!---->
66
<!---->
7-
<div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search" id="rc_select_TEST_OR_SSR" autocomplete="off" class="ant-input ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"></span>
7+
<div class="ant-select-selector"><span class="ant-select-selection-search"><input id="rc_select_TEST_OR_SSR" type="search" autocomplete="off" class="ant-input ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"></span>
88
<!----><span class="ant-select-selection-placeholder">input here</span>
99
</div>
1010
<!---->
@@ -17,7 +17,7 @@ exports[`renders ./components/auto-complete/demo/certain-category.vue correctly
1717
<div class="ant-select ant-select-lg certain-category-search ant-select-lg ant-select-show-search ant-select-auto-complete ant-select-single ant-select-customize-input ant-select-show-search" style="width: 100%;">
1818
<!---->
1919
<!---->
20-
<div class="ant-select-selector"><span class="ant-select-selection-search"><span class="ant-select-selection-search-input ant-input-affix-wrapper"><!----><input placeholder="input here" type="search" id="rc_select_TEST_OR_SSR" autocomplete="off" class="ant-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"><span class="ant-input-suffix"><!----><span role="img" aria-label="search" class="anticon anticon-search certain-category-icon"><svg focusable="false" class="" data-icon="search" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0011.6 0l43.6-43.5a8.2 8.2 0 000-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z"></path></svg></span></span></span></span>
20+
<div class="ant-select-selector"><span class="ant-select-selection-search"><span class="ant-select-selection-search-input ant-input-affix-wrapper"><!----><input placeholder="input here" id="rc_select_TEST_OR_SSR" type="search" autocomplete="off" class="ant-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"><span class="ant-input-suffix"><!----><span role="img" aria-label="search" class="anticon anticon-search certain-category-icon"><svg focusable="false" class="" data-icon="search" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0011.6 0l43.6-43.5a8.2 8.2 0 000-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z"></path></svg></span></span></span></span>
2121
<!----><span class="ant-select-selection-placeholder"><!----></span>
2222
</div>
2323
<!---->
@@ -30,7 +30,7 @@ exports[`renders ./components/auto-complete/demo/custom.vue correctly 1`] = `
3030
<div style="width: 200px;" class="ant-select ant-select-show-search ant-select-auto-complete ant-select-single ant-select-customize-input ant-select-show-search">
3131
<!---->
3232
<!---->
33-
<div class="ant-select-selector"><span class="ant-select-selection-search"><textarea placeholder="input here" class="ant-input ant-select-selection-search-input" style="height: 50px;" id="rc_select_TEST_OR_SSR" autocomplete="off" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"></textarea></span>
33+
<div class="ant-select-selector"><span class="ant-select-selection-search"><textarea placeholder="input here" id="rc_select_TEST_OR_SSR" class="ant-input ant-select-selection-search-input" style="height: 50px;" autocomplete="off" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"></textarea></span>
3434
<!----><span class="ant-select-selection-placeholder"><!----></span>
3535
</div>
3636
<!---->
@@ -42,7 +42,7 @@ exports[`renders ./components/auto-complete/demo/non-case-sensitive.vue correctl
4242
<div style="width: 200px;" class="ant-select ant-select-show-search ant-select-auto-complete ant-select-single ant-select-customize-input ant-select-show-search">
4343
<!---->
4444
<!---->
45-
<div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search" id="rc_select_TEST_OR_SSR" autocomplete="off" class="ant-input ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"></span>
45+
<div class="ant-select-selector"><span class="ant-select-selection-search"><input id="rc_select_TEST_OR_SSR" type="search" autocomplete="off" class="ant-input ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"></span>
4646
<!----><span class="ant-select-selection-placeholder">input here</span>
4747
</div>
4848
<!---->
@@ -54,7 +54,7 @@ exports[`renders ./components/auto-complete/demo/options.vue correctly 1`] = `
5454
<div style="width: 200px;" class="ant-select ant-select-show-search ant-select-auto-complete ant-select-single ant-select-customize-input ant-select-show-search">
5555
<!---->
5656
<!---->
57-
<div class="ant-select-selector"><span class="ant-select-selection-search"><input type="search" id="rc_select_TEST_OR_SSR" autocomplete="off" class="ant-input ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"></span>
57+
<div class="ant-select-selector"><span class="ant-select-selection-search"><input id="rc_select_TEST_OR_SSR" type="search" autocomplete="off" class="ant-input ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0"></span>
5858
<!----><span class="ant-select-selection-placeholder">input here</span>
5959
</div>
6060
<!---->
@@ -67,7 +67,7 @@ exports[`renders ./components/auto-complete/demo/uncertain-category.vue correctl
6767
<div class="ant-select ant-select-lg global-search ant-select-lg ant-select-show-search ant-select-auto-complete ant-select-single ant-select-customize-input ant-select-show-search" style="width: 100%;">
6868
<!---->
6969
<!---->
70-
<div class="ant-select-selector"><span class="ant-select-selection-search"><span class="ant-input-search ant-select-selection-search-input ant-input-search-enter-button ant-input-search-large ant-input-group-wrapper ant-input-group-wrapper-lg"><span class="ant-input-wrapper ant-input-group"><!----><input placeholder="input here" type="search" id="rc_select_TEST_OR_SSR" autocomplete="off" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0" class="ant-input ant-input-lg"><span class="ant-input-group-addon"><button class="ant-btn ant-btn-primary ant-btn-lg ant-input-search-button" type="button"><!----><span role="img" aria-label="search" class="anticon anticon-search"><svg focusable="false" class="" data-icon="search" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0011.6 0l43.6-43.5a8.2 8.2 0 000-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z"></path></svg></span></button></span></span></span></span>
70+
<div class="ant-select-selector"><span class="ant-select-selection-search"><span class="ant-input-search ant-select-selection-search-input ant-input-search-enter-button ant-input-search-large ant-input-group-wrapper ant-input-group-wrapper-lg"><span class="ant-input-wrapper ant-input-group"><!----><input placeholder="input here" id="rc_select_TEST_OR_SSR" type="search" autocomplete="off" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0" class="ant-input ant-input-lg"><span class="ant-input-group-addon"><button class="ant-btn ant-btn-primary ant-btn-lg ant-input-search-button" type="button"><!----><span role="img" aria-label="search" class="anticon anticon-search"><svg focusable="false" class="" data-icon="search" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0011.6 0l43.6-43.5a8.2 8.2 0 000-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z"></path></svg></span></button></span></span></span></span>
7171
<!----><span class="ant-select-selection-placeholder"><!----></span>
7272
</div>
7373
<!---->

components/cascader/index.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { defaultConfigProvider } from '../config-provider';
2727
import type { VueNode } from '../_util/type';
2828
import { tuple, withInstall } from '../_util/type';
2929
import type { RenderEmptyHandler } from '../config-provider/renderEmpty';
30+
import { useInjectFormItemContext } from '../form/FormItemContext';
3031

3132
export interface CascaderOptionType {
3233
value?: string | number;
@@ -220,12 +221,14 @@ const Cascader = defineComponent({
220221
inheritAttrs: false,
221222
props: cascaderProps,
222223
setup() {
224+
const formItemContext = useInjectFormItemContext();
223225
return {
224226
configProvider: inject('configProvider', defaultConfigProvider),
225227
localeData: inject('localeData', {} as any),
226228
cachedOptions: [],
227229
popupRef: undefined,
228230
input: undefined,
231+
formItemContext,
229232
};
230233
},
231234
data() {
@@ -323,6 +326,7 @@ const Cascader = defineComponent({
323326
inputFocused: false,
324327
});
325328
this.$emit('blur', e);
329+
this.formItemContext.onFieldBlur();
326330
},
327331

328332
handleInputClick(e: MouseEvent & { nativeEvent?: any }) {
@@ -354,6 +358,7 @@ const Cascader = defineComponent({
354358
}
355359
this.$emit('update:value', value);
356360
this.$emit('change', value, selectedOptions);
361+
this.formItemContext.onFieldChange();
357362
},
358363

359364
getLabel() {
@@ -474,7 +479,12 @@ const Cascader = defineComponent({
474479
...otherProps
475480
} = props as any;
476481
const { onEvents, extraAttrs } = splitAttrs(this.$attrs);
477-
const { class: className, style, ...restAttrs } = extraAttrs;
482+
const {
483+
class: className,
484+
style,
485+
id = this.formItemContext.id.value,
486+
...restAttrs
487+
} = extraAttrs;
478488
const getPrefixCls = this.configProvider.getPrefixCls;
479489
const renderEmpty = this.configProvider.renderEmpty;
480490
const prefixCls = getPrefixCls('cascader', customizePrefixCls);
@@ -570,6 +580,7 @@ const Cascader = defineComponent({
570580
const inputProps = {
571581
...restAttrs,
572582
...tempInputProps,
583+
id,
573584
prefixCls: inputPrefixCls,
574585
placeholder: value && value.length > 0 ? undefined : placeholder,
575586
value: inputValue,

components/checkbox/Checkbox.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { defaultConfigProvider } from '../config-provider';
88
import warning from '../_util/warning';
99
import type { RadioChangeEvent } from '../radio/interface';
1010
import type { EventHandler } from '../_util/EventInterface';
11+
import { useInjectFormItemContext } from '../form/FormItemContext';
1112
function noop() {}
1213

1314
export const checkboxProps = () => {
@@ -38,7 +39,9 @@ export default defineComponent({
3839
props: checkboxProps(),
3940
emits: ['change', 'update:checked'],
4041
setup() {
42+
const formItemContext = useInjectFormItemContext();
4143
return {
44+
formItemContext,
4245
configProvider: inject('configProvider', defaultConfigProvider),
4346
checkboxGroupContext: inject('checkboxGroupContext', undefined),
4447
};
@@ -96,7 +99,13 @@ export default defineComponent({
9699
const props = getOptionProps(this);
97100
const { checkboxGroupContext: checkboxGroup, $attrs } = this;
98101
const children = getSlot(this);
99-
const { indeterminate, prefixCls: customizePrefixCls, skipGroup, ...restProps } = props;
102+
const {
103+
indeterminate,
104+
prefixCls: customizePrefixCls,
105+
skipGroup,
106+
id = this.formItemContext.id.value,
107+
...restProps
108+
} = props;
100109
const getPrefixCls = this.configProvider.getPrefixCls;
101110
const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
102111
const {
@@ -109,12 +118,14 @@ export default defineComponent({
109118
} = $attrs;
110119
const checkboxProps: any = {
111120
...restProps,
121+
id,
112122
prefixCls,
113123
...restAttrs,
114124
};
115125
if (checkboxGroup && !skipGroup) {
116126
checkboxProps.onChange = (...args) => {
117127
this.$emit('change', ...args);
128+
this.formItemContext.onFieldChange();
118129
checkboxGroup.toggleOption({ label: children, value: props.value });
119130
};
120131
checkboxProps.name = checkboxGroup.name;

components/checkbox/Group.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Checkbox from './Checkbox';
55
import hasProp, { getSlot } from '../_util/props-util';
66
import { defaultConfigProvider } from '../config-provider';
77
import type { VueNode } from '../_util/type';
8+
import { useInjectFormItemContext } from '../form/FormItemContext';
89

910
export type CheckboxValueType = string | number | boolean;
1011
export interface CheckboxOptionType {
@@ -25,10 +26,13 @@ export default defineComponent({
2526
options: { type: Array as PropType<Array<CheckboxOptionType | string>> },
2627
disabled: PropTypes.looseBool,
2728
onChange: PropTypes.func,
29+
id: PropTypes.string,
2830
},
2931
emits: ['change', 'update:value'],
3032
setup() {
33+
const formItemContext = useInjectFormItemContext();
3134
return {
35+
formItemContext,
3236
configProvider: inject('configProvider', defaultConfigProvider),
3337
};
3438
},
@@ -95,11 +99,12 @@ export default defineComponent({
9599
// this.$emit('input', val);
96100
this.$emit('update:value', val);
97101
this.$emit('change', val);
102+
this.formItemContext.onFieldChange();
98103
},
99104
},
100105
render() {
101106
const { $props: props, $data: state } = this;
102-
const { prefixCls: customizePrefixCls, options } = props;
107+
const { prefixCls: customizePrefixCls, options, id = this.formItemContext.id.value } = props;
103108
const getPrefixCls = this.configProvider.getPrefixCls;
104109
const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
105110
let children = getSlot(this);
@@ -120,6 +125,10 @@ export default defineComponent({
120125
</Checkbox>
121126
));
122127
}
123-
return <div class={groupPrefixCls}>{children}</div>;
128+
return (
129+
<div class={groupPrefixCls} id={id}>
130+
{children}
131+
</div>
132+
);
124133
},
125134
});

components/date-picker/generatePicker/generateRangePicker.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { commonProps, rangePickerProps } from './props';
1515
import type { PanelMode, RangeValue } from '../../vc-picker/interface';
1616
import type { RangePickerSharedProps } from '../../vc-picker/RangePicker';
1717
import devWarning from '../../vc-util/devWarning';
18+
import { useInjectFormItemContext } from '../../form/FormItemContext';
1819

1920
export default function generateRangePicker<DateType, ExtraProps = {}>(
2021
generateConfig: GenerateConfig<DateType>,
@@ -52,6 +53,7 @@ export default function generateRangePicker<DateType, ExtraProps = {}>(
5253
'blur',
5354
],
5455
setup(props, { expose, slots, attrs, emit }) {
56+
const formItemContext = useInjectFormItemContext();
5557
devWarning(
5658
!attrs.getCalendarContainer,
5759
'DatePicker',
@@ -86,6 +88,7 @@ export default function generateRangePicker<DateType, ExtraProps = {}>(
8688
const values = maybeToStrings(dates);
8789
emit('update:value', values);
8890
emit('change', values, dateStrings);
91+
formItemContext.onFieldChange();
8992
};
9093
const onOpenChange = (open: boolean) => {
9194
emit('update:open', open);
@@ -96,6 +99,7 @@ export default function generateRangePicker<DateType, ExtraProps = {}>(
9699
};
97100
const onBlur = () => {
98101
emit('blur');
102+
formItemContext.onFieldBlur();
99103
};
100104
const onPanelChange = (dates: RangeValue<DateType>, modes: [PanelMode, PanelMode]) => {
101105
const values = maybeToStrings(dates);
@@ -154,6 +158,7 @@ export default function generateRangePicker<DateType, ExtraProps = {}>(
154158
renderExtraFooter = slots.renderExtraFooter,
155159
separator = slots.separator?.(),
156160
clearIcon = slots.clearIcon?.(),
161+
id = formItemContext.id.value,
157162
...restProps
158163
} = p;
159164
const { format, showTime } = p as any;
@@ -187,6 +192,7 @@ export default function generateRangePicker<DateType, ExtraProps = {}>(
187192
transitionName={transitionName || `${rootPrefixCls.value}-slide-up`}
188193
{...restProps}
189194
{...additionalOverrideProps}
195+
id={id}
190196
value={value.value}
191197
defaultValue={defaultValue.value}
192198
defaultPickerValue={defaultPickerValue.value}

0 commit comments

Comments
 (0)