Skip to content

Commit

Permalink
fix(DatePicker): fix bug where minDate set to today is disabled (#3176)
Browse files Browse the repository at this point in the history
  • Loading branch information
joakbjerk authored Jan 5, 2024
1 parent ba130ba commit ef65676
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import getDaysInMonth from 'date-fns/getDaysInMonth'
import toDate from 'date-fns/toDate'
import parseISO from 'date-fns/parseISO'
import parse from 'date-fns/parse'
import startOfDay from 'date-fns/startOfDay'

import { warn } from '../../shared/component-helper'

Expand Down Expand Up @@ -171,8 +172,8 @@ const isWithinSelectionCalc = (date, startDate, endDate) => {
const isDisabledCalc = (date, minDate, maxDate) => {
// isBefore and isAfter return false if comparison date is undefined, which is useful here in case minDate and maxDate aren't supplied
return (
(minDate && isBefore(date, minDate)) ||
(maxDate && isAfter(date, maxDate))
(minDate && isBefore(date, startOfDay(minDate))) ||
(maxDate && isAfter(date, startOfDay(maxDate)))
)
}
export { isDisabledCalc as isDisabled }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,24 @@ describe('DatePicker component', () => {
expect.objectContaining({ target: secondInput })
)
})

it('should have todays date enabled in calendar if minDate is today', async () => {
const minDate = new Date()

render(<DatePicker min_date={minDate} />)

const button = document.querySelector(
'.dnb-input__submit-element > button'
)

await userEvent.click(button)

const todayButton = document.querySelector(
'.dnb-date-picker__day--today > button'
)

expect(todayButton).not.toBeDisabled()
})
})

// for the unit calc tests
Expand Down

0 comments on commit ef65676

Please sign in to comment.