fix(Field): sync input value when dynamically toggling input slot#13783
fix(Field): sync input value when dynamically toggling input slot#13783lllomh wants to merge 65 commits intoyouzan:mainfrom
Conversation
…t with the Chinese version under the English version
…y large, the component may return a value outside the max range.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
…y large, the component may return a value outside the max range test.
Summary of ChangesHello @lllomh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses two distinct issues within the Vant UI library. It resolves a bug in the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request addresses an issue where the input value in the Field component doesn't sync correctly when dynamically toggling the input slot. The solution involves adding a watcher to inputRef to ensure the value is updated when the input element is recreated. Additionally, there are changes in the Slider component to refine value formatting, specifically around handling values that exceed the maximum boundary when stepping. New test cases have been added for both Field and Slider components to cover these fixes.
| // Sync value when input element is recreated (e.g., when toggling custom input slot) | ||
| // https://github.com/youzan/vant/issues/13769 | ||
| watch( | ||
| () => inputRef.value, | ||
| (input) => { | ||
| if (input && input.value !== getModelValue()) { | ||
| input.value = getModelValue(); | ||
| } | ||
| }, | ||
| ); |
| test('should sync value when dynamically toggling input slot', async () => { | ||
| const wrapper = mount({ | ||
| data() { | ||
| return { | ||
| useSlot: false, | ||
| modelValue: 'hello', | ||
| }; | ||
| }, | ||
| render() { | ||
| return ( | ||
| <Field | ||
| v-model={this.modelValue} | ||
| v-slots={{ | ||
| input: this.useSlot ? () => 'Custom Input' : undefined, | ||
| }} | ||
| /> | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| let input = wrapper.find('input'); | ||
| expect(input.element.value).toEqual('hello'); | ||
|
|
||
| // Toggle to use custom input slot | ||
| await wrapper.vm.$data.useSlot = true; | ||
| await wrapper.vm.$nextTick(); | ||
| expect(wrapper.find('.van-field__control').text()).toContain('Custom Input'); | ||
|
|
||
| // Update model value while slot is active | ||
| await wrapper.vm.$data.modelValue = 'world'; | ||
| await wrapper.vm.$nextTick(); | ||
|
|
||
| // Toggle back to native input | ||
| await wrapper.vm.$data.useSlot = false; | ||
| await wrapper.vm.$nextTick(); | ||
|
|
||
| // The native input should have the updated value | ||
| input = wrapper.find('input'); | ||
| expect(input.element.value).toEqual('world'); | ||
| }); |
|
|
||
| const diff = Math.round((value - min) / step) * step; | ||
| return addNumber(min, diff); | ||
| const steppedValue = addNumber(min, diff); | ||
|
|
||
| if (steppedValue > max) { | ||
| const prevSteppedValue = addNumber(min, diff - step); | ||
| const distanceToMax = Math.abs(value - max); | ||
| const distanceToPrev = Math.abs(value - prevSteppedValue); | ||
|
|
||
| return distanceToMax <= distanceToPrev ? max : prevSteppedValue; | ||
| } | ||
| return steppedValue; |
There was a problem hiding this comment.
| //https://github.com/youzan/vant/issues/13625 | ||
| test('should return max when distanceToMax <= distanceToPrev', () => { | ||
| const wrapper = mount(Slider, { | ||
| props: { min: 0, max: 50, step: 60, modelValue: 45 }, | ||
| }); | ||
|
|
||
| const emitted = wrapper.emitted('update:modelValue'); | ||
| if (emitted && emitted.length > 0) { | ||
| const result = emitted[emitted.length - 1][0] as number; | ||
| expect(result).toBe(50); // Should return max | ||
| } |
| test('should enter steppedValue > max branch', () => { | ||
| const wrapper = mount(Slider, { | ||
| props: { min: 0, max: 12, step: 20, modelValue: 0 }, | ||
| }); | ||
|
|
||
| wrapper.setProps({ modelValue: 11 }); | ||
|
|
||
| const emitted = wrapper.emitted('update:modelValue'); | ||
| if (emitted && emitted.length > 0) { | ||
| const result = emitted[emitted.length - 1][0] as number; | ||
| expect(result).toBeGreaterThanOrEqual(0); | ||
| expect(result).toBeLessThanOrEqual(12); | ||
| } |
Description
When dynamically controlling whether to use the
inputslot on the Field component,the input value doesn't display correctly when switching back to the native input element.
Root Cause
When switching from a custom
inputslot to the native HTML input element:modelValueupdateValue()method is only called whenmodelValuechanges, on user input, or on mountmodelValuehasn't changed while the slot was active, the new element has an empty valueSolution
Added a watcher on
inputRefthat syncs the value whenever a new input element is created:Changes
packages/vant/src/field/Field.tsxto add the watcherpackages/vant/src/field/test/index.spec.jsto verify dynamic slot togglingRelated Issue
Closes #13769