2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-23 17:10:35 +03:00

fix: should click twice to open popup in mobile (#725)

This commit is contained in:
mengxiong10
2022-12-01 17:06:20 +08:00
parent 97afac80e5
commit 375edbbae8
5 changed files with 33323 additions and 15059 deletions
@@ -87,11 +87,6 @@ exports[`DatePicker prop: editable 1`] = `
readonly="readonly" readonly="readonly"
type="text" type="text"
/> />
<i
class="mx-icon-clear"
>
<anonymous-stub />
</i>
<i <i
class="mx-icon-calendar" class="mx-icon-calendar"
> >
+8 -5
View File
@@ -17,7 +17,7 @@ describe('DatePicker', () => {
expect(wrapper.find('.mx-datepicker-popup').exists()).toBe(false); expect(wrapper.find('.mx-datepicker-popup').exists()).toBe(false);
// expect click input should show the popup // expect click input should show the popup
const input = wrapper.find('input'); const input = wrapper.find('input');
await input.trigger('mousedown'); await input.trigger('click');
expect(wrapper.find('.mx-datepicker-popup').exists()).toBe(true); expect(wrapper.find('.mx-datepicker-popup').exists()).toBe(true);
// expect click out side should hide the popup // expect click out side should hide the popup
const bodyWrapper = createWrapper(document.body); const bodyWrapper = createWrapper(document.body);
@@ -296,28 +296,31 @@ describe('DatePicker', () => {
expect(popup.element.parentNode).toBe(document.body); expect(popup.element.parentNode).toBe(document.body);
}); });
it('feat: should emit clear event when click clear button', () => { it('feat: should emit clear event when click clear button', async () => {
wrapper = shallowMount(DatePicker, { wrapper = shallowMount(DatePicker, {
propsData: { propsData: {
value: new Date(2019, 10, 9), value: new Date(2019, 10, 9),
}, },
}); });
await wrapper.find('.mx-input-wrapper').trigger('mouseenter');
const clearButton = wrapper.find('.mx-icon-clear'); const clearButton = wrapper.find('.mx-icon-clear');
clearButton.trigger('mousedown');
clearButton.trigger('click');
const emitted = wrapper.emitted(); const emitted = wrapper.emitted();
expect(emitted.clear).toBeTruthy(); expect(emitted.clear).toBeTruthy();
expect(emitted.input[0][0]).toBe(null); expect(emitted.input[0][0]).toBe(null);
}); });
it('feat: should emit [null, null] when clear range', () => { it('feat: should emit [null, null] when clear range', async () => {
wrapper = shallowMount(DatePicker, { wrapper = shallowMount(DatePicker, {
propsData: { propsData: {
range: true, range: true,
value: [new Date(2019, 10, 9), new Date(2019, 11, 9)], value: [new Date(2019, 10, 9), new Date(2019, 11, 9)],
}, },
}); });
await wrapper.find('.mx-input-wrapper').trigger('mouseenter');
const clearButton = wrapper.find('.mx-icon-clear'); const clearButton = wrapper.find('.mx-icon-clear');
clearButton.trigger('mousedown'); clearButton.trigger('click');
const emitted = wrapper.emitted(); const emitted = wrapper.emitted();
expect(emitted.input[0][0]).toEqual([null, null]); expect(emitted.input[0][0]).toEqual([null, null]);
}); });
+18311 -41
View File
File diff suppressed because it is too large Load Diff
+19 -12
View File
@@ -140,6 +140,7 @@ export default {
currentValue: null, currentValue: null,
userInput: null, userInput: null,
defaultOpen: false, defaultOpen: false,
mouseInInput: false,
}; };
}, },
computed: { computed: {
@@ -188,7 +189,7 @@ export default {
return this.formatDate(this.innerValue); return this.formatDate(this.innerValue);
}, },
showClearIcon() { showClearIcon() {
return !this.disabled && this.clearable && this.text; return !this.disabled && this.clearable && this.text && this.mouseInInput;
}, },
locale() { locale() {
if (isObject(this.lang)) { if (isObject(this.lang)) {
@@ -224,6 +225,12 @@ export default {
} }
}, },
methods: { methods: {
handleMouseEnter() {
this.mouseInInput = true;
},
handleMouseLeave() {
this.mouseInInput = false;
},
handleClickOutSide(evt) { handleClickOutSide(evt) {
const { target } = evt; const { target } = evt;
if (!this.$el.contains(target)) { if (!this.$el.contains(target)) {
@@ -475,25 +482,25 @@ export default {
} }
); );
const calendarIcon = this.type === 'time' ? <IconTime /> : <IconCalendar />; const calendarIcon = this.type === 'time' ? <IconTime /> : <IconCalendar />;
// remove touchstart event to avoid opens the popup while scrolling in mobile #469
return ( return (
<div <div
class={`${prefixClass}-input-wrapper`} class={`${prefixClass}-input-wrapper`}
onMousedown={this.openPopup} onMouseenter={this.handleMouseEnter}
onTouchstart={this.openPopup} onMouseleave={this.handleMouseLeave}
onClick={this.openPopup}
ref="inputWrapper"
> >
{input} {input}
{this.showClearIcon ? ( {this.showClearIcon ? (
<i <i class={`${prefixClass}-icon-clear`} onClick={this.handleClear}>
class={`${prefixClass}-icon-clear`}
onMousedown={this.handleClear}
onTouchstart={this.handleClear}
>
{this.renderSlot('icon-clear', <IconClose />)} {this.renderSlot('icon-clear', <IconClose />)}
</i> </i>
) : null} ) : (
<i class={`${prefixClass}-icon-calendar`}> <i class={`${prefixClass}-icon-calendar`}>
{this.renderSlot('icon-calendar', calendarIcon)} {this.renderSlot('icon-calendar', calendarIcon)}
</i> </i>
)}
</div> </div>
); );
}, },
-11
View File
@@ -27,17 +27,6 @@
.#{$namespace}-input-wrapper { .#{$namespace}-input-wrapper {
position: relative; position: relative;
.#{$namespace}-icon-clear {
display: none;
}
&:hover {
.#{$namespace}-icon-clear {
display: block;
}
.#{$namespace}-icon-clear + .#{$namespace}-icon-calendar {
display: none;
}
}
} }
.#{$namespace}-input { .#{$namespace}-input {