Skip to content

Commit

Permalink
fix(DatePicker): make keyboard usage in input not throw (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Jun 22, 2022
1 parent 5fcbf17 commit d0756e2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
79 changes: 43 additions & 36 deletions packages/dnb-eufemia/src/components/date-picker/DatePickerInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,47 +259,54 @@ export default class DatePickerInput extends React.PureComponent {
}

prepareCounting = async ({ keyCode, target, event }) => {
const isDate = target
.getAttribute('class')
.match(/__input--([day|month|year]+)($|\s)/)[1]
const isInRange = target
.getAttribute('id')
.match(/[0-9]-([start|end]+)-/)[1]

let date =
isInRange === 'start' ? this.context.startDate : this.context.endDate

// do nothing if date is not set yet
if (!date) {
return
}

const count = keyCode === 'up' ? 1 : -1
try {
const isDate = target
.getAttribute('class')
.match(/__input--([day|month|year]+)($|\s)/)[1]

const isInRange = target
.getAttribute('id')
.match(/-([start|end]+)-/)[1]

let date =
isInRange === 'start'
? this.context.startDate
: this.context.endDate

// do nothing if date is not set yet
if (!date) {
return
}

if (keyCode === 'up' || keyCode === 'down') {
switch (isDate) {
case 'day':
date = addDays(date, count)
break
case 'month':
date = addMonths(date, count)
break
case 'year':
date = addYears(date, count)
break
const count = keyCode === 'up' ? 1 : -1

if (keyCode === 'up' || keyCode === 'down') {
switch (isDate) {
case 'day':
date = addDays(date, count)
break
case 'month':
date = addMonths(date, count)
break
case 'year':
date = addYears(date, count)
break
}
}
}

this.callOnChange({
[isInRange === 'start' ? 'startDate' : 'endDate']: date,
event,
})
this.callOnChange({
[isInRange === 'start' ? 'startDate' : 'endDate']: date,
event,
})

await wait(1) // to get the correct position afterwards
await wait(1) // to get the correct position afterwards

const endPos = target.value.length
target.focus()
target.setSelectionRange(0, endPos)
const endPos = target.value.length
target.focus()
target.setSelectionRange(0, endPos)
} catch (e) {
warn(e)
}
}

onFocusHandler = (event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ describe('DatePicker component', () => {
range
start_date={defaultProps.start_date}
end_date={defaultProps.end_date}
id="unique-id"
/>,
{ attachTo: attachToBody() }
)
Expand Down

0 comments on commit d0756e2

Please sign in to comment.