Skip to content

Commit

Permalink
fix(datepicker): Clear date via delete/backspace key
Browse files Browse the repository at this point in the history
  • Loading branch information
mst101 committed Jan 23, 2022
1 parent 2336cd7 commit e8eb64f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/components/DateInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
@blur="handleInputBlur"
@click="handleInputClick"
@focus="handleInputFocus"
@keydown.backspace="handleDelete"
@keydown.delete="handleDelete"
@keydown.down.prevent="handleKeydownDown"
@keydown.enter.prevent="handleKeydownEnter"
@keydown.esc.prevent="handleKeydownEscape"
Expand Down Expand Up @@ -206,6 +208,14 @@ export default {
this.shouldToggleOnFocus = true
}
},
/**
* Clears the calendar when the `delete` or `backspace` key is pressed
*/
handleDelete() {
if (!this.typeable && this.selectedDate) {
this.clearDate()
}
},
/**
* Toggles the calendar (unless `show-calendar-on-button-click` is true)
*/
Expand Down
5 changes: 5 additions & 0 deletions src/components/Datepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,14 @@ export default {
* Clear the selected date
*/
clearDate() {
if (!this.selectedDate) {
return
}
this.selectedDate = null
this.focus.refs = ['input']
this.close()
this.setPageDate()
this.$emit('selected', null)
this.$emit('input', null)
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/specs/ClearOnDelete.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: Clear date on delete
As a user
I want to press the delete or backspace key
So that I can clear the date


@id-1
Scenario Outline: Clear date via "<key>"
Given the calendar has a selected date
When the user focuses the input field and presses "<key>"
Then the date is cleared
And the input field has focus

Examples:
| # | key |
| 1 | backspace |
| 2 | del |
28 changes: 28 additions & 0 deletions test/e2e/specs/ClearOnDelete/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Given, When, Then, And } from 'cypress-cucumber-preprocessor/steps'

const { createCalendar, focusThe, the } = cy

describe('Clear date on delete', () => {
describe('@id-1: Clear date via {string}', () => {
Given('the calendar has a selected date', () => {
createCalendar({
value: new Date(2020, 2, 15),
})
the('calendar').should('not.be.visible')
the('input').should('have.value', '15 Mar 2020')
})

When('the user focuses the input field and presses {string}', (key) => {
// Use force as a non-typeable calendar input is `read-only`
focusThe('input').type(`{${key}}`, { force: true })
})

Then('the date is cleared', () => {
the('input').should('have.value', '')
})

And('the input field has focus', () => {
the('input').should('be.focused')
})
})
})
14 changes: 14 additions & 0 deletions test/unit/specs/DateInput/DateInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,18 @@ describe('DateInput', () => {

expect(wrapper.emitted('open')).toBeTruthy()
})

it('emits `clear-date` when delete is pressed', async () => {
const input = wrapper.find('input')

await input.trigger('keydown.del')
expect(wrapper.emitted('clear-date')).toBeTruthy()
})

it('emits `clear-date` when backspace is pressed', async () => {
const input = wrapper.find('input')

await input.trigger('keydown.backspace')
expect(wrapper.emitted('clear-date')).toBeTruthy()
})
})

0 comments on commit e8eb64f

Please sign in to comment.