Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DatePicker): make keyboard usage in input not throw #1475

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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