Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
fix: change minDate to start at the beginning of the day (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uladzimir Havenchyk authored Sep 3, 2020
1 parent 91be387 commit 545cada
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/calendar/__specs__/calendar_spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react'
import ReactTestRenderer from 'react-test-renderer'

import Calendar from '../calendar'
import Month from '../month'

describe('Calendar', () => {
let instance, props, wrapper
Expand Down Expand Up @@ -229,7 +230,7 @@ describe('Calendar', () => {

describe('when prop `onSelectionProgress` is defined', () => {
it('returns prop `selected.start`', () => {
props = { mode: 'range', onSelectionProgress: () => {}, selected }
props = { mode: 'range', onSelectionProgress: () => { }, selected }
wrapper = shallow(<Calendar {...props} />)
wrapper.setState({ selection })

Expand Down Expand Up @@ -354,10 +355,21 @@ describe('Calendar', () => {
describe('#render', () => {
it('renders <Calendar />', () => {
const tree = ReactTestRenderer.create(
<Calendar activeMonth={date} onMonthChange={() => {}} />
<Calendar activeMonth={date} onMonthChange={() => { }} />
).toJSON()

expect(tree).toMatchSnapshot()
})
})

it('uses normalized minDate with a start at 00:00', () => {
const dirtyMinDate = '2020-09-03T11:26:49.526Z'
const { root } = ReactTestRenderer.create(
<Calendar minDate={dirtyMinDate} activeMonth={date} onMonthChange={() => { }} />
)

const startOfMinDate = new Date(2020, 8, 3) // start of the day in local time - 00:00

expect(root.findByType(Month).props.minDate).toEqual(startOfMinDate)
})
})
5 changes: 4 additions & 1 deletion src/calendar/calendar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component, ComponentProps, ReactElement } from 'react'
import isSameMonth from 'date-fns/is_same_month'
import isValidDate from 'date-fns/is_valid'
import startOfDay from 'date-fns/start_of_day'
import startOfMonth from 'date-fns/start_of_month'

import * as helper from '../helper'
Expand Down Expand Up @@ -278,6 +279,8 @@ export default class Calendar extends Component<Props, State> {
const selection = this._selection()
const highlight = this._highlight()

const normalizedMinDate = minDate ? startOfDay(minDate) : minDate

return (
// @ts-ignore: No overload matches this call
<Month
Expand All @@ -295,7 +298,7 @@ export default class Calendar extends Component<Props, State> {
highlightedEnd={highlight.end}
highlightedStart={highlight.start}
maxDate={maxDate}
minDate={minDate}
minDate={normalizedMinDate}
minNumberOfWeeks={minNumberOfWeeks}
mode={mode as 'range' | 'single'}
onChange={this._selectionChanged.bind(this)}
Expand Down
2 changes: 1 addition & 1 deletion src/calendar/stories/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ stories

// minDate
const minDateLabel = 'minDate'
const minDateDefaultValue = new Date('2018-01-01')
const minDateDefaultValue = new Date('2018-01-02')
const minDate = date(minDateLabel, minDateDefaultValue)

// minNumberOfWeeks
Expand Down

0 comments on commit 545cada

Please sign in to comment.