Skip to content

Commit ddc6795

Browse files
authored
fix(form-item): fixed an issue where validation would fail to trigger after continuous input when form items were configured with validation stabilization and the focus would quickly drop. (#4038)
* fix(form-item): 修复当表单项配置了校验防抖,在连续输入后快速失焦无法触发校验的问题 * fix(form): update role for date and time pickers in basic usage tests
1 parent 2b08d39 commit ddc6795

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

examples/sites/demos/pc/app/form/basic-usage.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ test('测试基本表单', async ({ page }) => {
1818
await decreaseIcon.click()
1919
await expect(numericInput).toHaveValue('0')
2020

21-
// 日期选择器
22-
const datePicker = demo.getByRole('textbox').first()
21+
// 日期选择器(PR #4028 后,role 从 textbox 改为 combobox)
22+
const datePicker = demo.getByRole('combobox').first()
2323
await datePicker.click()
24+
await page.waitForTimeout(200) // 等待弹出层显示
2425
await page.getByRole('cell', { name: '15' }).getByText('15').click()
2526
await expect(datePicker).toHaveValue(/15/)
2627

27-
// 時間选择器
28-
const timePicker = demo.getByRole('combobox').nth(0)
28+
// 时间选择器
29+
const timePicker = demo.getByRole('combobox').nth(1) // 第二个 combobox(第一个是日期选择器)
2930
await timePicker.click()
31+
await page.waitForTimeout(200) // 等待下拉列表显示
3032
await page.getByRole('option').filter({ hasText: '00:30' }).click()
3133
await expect(timePicker).toHaveValue('00:30')
3234

packages/renderless/src/form-item/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,12 @@ export const getFilteredRule =
416416
.map((rule) => merge({}, rule))
417417
}
418418

419+
// blur 事件触发时,直接调用原始的 validate 函数,不应用防抖
419420
export const onFieldBlur = (api: IFormItemRenderlessParams['api']) => (): void => {
420-
api.validate('blur')
421+
api.validateOrigin('blur')
421422
}
422423

424+
// change 事件触发时,调用可能被防抖包装的 validate 函数
423425
export const onFieldChange =
424426
({ api, state }: Pick<IFormItemRenderlessParams, 'api' | 'state'>) =>
425427
(): void => {

packages/renderless/src/form-item/vue.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import type {
5656
export const api = [
5757
'state',
5858
'validate',
59+
'validateOrigin',
5960
'clearValidate',
6061
'resetField',
6162
'getRules',
@@ -144,6 +145,9 @@ const initState = ({
144145
}
145146

146147
const initApi = ({ api, state, dispatch, broadcast, props, constants, vm, t, nextTick, slots }) => {
148+
// 创建原始的 validate 函数(不经过防抖处理)
149+
const validateOriginFunc = validate({ api, props, state, t })
150+
147151
Object.assign(api, {
148152
state,
149153
dispatch,
@@ -172,7 +176,8 @@ const initApi = ({ api, state, dispatch, broadcast, props, constants, vm, t, nex
172176
onFieldBlur: onFieldBlur(api),
173177
onFieldChange: onFieldChange({ api, state }),
174178
addValidateEvents: addValidateEvents({ api, vm, props, state }),
175-
validate: wrapValidate({ validateFunc: validate({ api, props, state, t }), props }),
179+
validateOrigin: validateOriginFunc,
180+
validate: wrapValidate({ validateFunc: validateOriginFunc, props }),
176181
getDisplayedValue: getDisplayedValue({ state }),
177182
clearDisplayedValue: clearDisplayedValue({ state }),
178183
handleLabelMouseenter: handleLabelMouseenter({ props, state, slots }),

packages/renderless/types/form-item.type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export interface IFormItemApi {
144144
onFieldChange: ReturnType<typeof onFieldChange>
145145
addValidateEvents: ReturnType<typeof addValidateEvents>
146146
validate: ReturnType<typeof validate>
147+
validateOrigin: ReturnType<typeof validate> // 原始的 validate 函数,不经过防抖处理
147148
getDisplayedValue: ReturnType<typeof getDisplayedValue>
148149
clearDisplayedValue: ReturnType<typeof clearDisplayedValue>
149150
handleMouseenter: ReturnType<typeof handleMouseenter>

0 commit comments

Comments
 (0)