Skip to content

Commit

Permalink
Merge branch 'master' into feature/custom-input-format
Browse files Browse the repository at this point in the history
  • Loading branch information
mgm793 committed Aug 11, 2021
2 parents 3fd7141 + 6c2cb61 commit 4d9b682
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -18,7 +18,7 @@ script:
- 'if [ -n "${KARMA-}" ]; then npm run tests-karma ; fi'
- 'if [ -n "${COVERAGE-}" ] && [ "${TRAVIS_BRANCH-}" = "master" ]; then npm run cover; cat ./coverage/lcov.info | ./node_modules/.bin/coveralls ; fi'
install:
- 'if [ -n "${LINT-}" ]; then npm install --legacy-bundling ; else npm install ; fi'
- 'if [ -n "${LINT-}" ]; then NPM_CONFIG_LEGACY_PEER_DEPS=true npm install --legacy-bundling ; else npm install ; fi'
env:
global:
- TEST=true
Expand Down
2 changes: 2 additions & 0 deletions examples/DateRangePickerWrapper.jsx
Expand Up @@ -14,6 +14,7 @@ import {
HORIZONTAL_ORIENTATION,
ANCHOR_LEFT,
NAV_POSITION_TOP,
OPEN_DOWN,
} from '../src/constants';
import isInclusivelyAfterDay from '../src/utils/isInclusivelyAfterDay';

Expand Down Expand Up @@ -70,6 +71,7 @@ const defaultProps = {
keepOpenOnDateSelect: false,
reopenPickerOnClearDates: false,
isRTL: false,
openDirection: OPEN_DOWN,

// navigation related props
navPosition: NAV_POSITION_TOP,
Expand Down
2 changes: 2 additions & 0 deletions examples/DayPickerSingleDateControllerWrapper.jsx
Expand Up @@ -19,6 +19,7 @@ const propTypes = forbidExtraProps({
initialDate: momentPropTypes.momentObj,
showInput: PropTypes.bool,

allowUnselect: PropTypes.bool,
keepOpenOnDateSelect: PropTypes.bool,
isOutsideRange: PropTypes.func,
isDayBlocked: PropTypes.func,
Expand Down Expand Up @@ -56,6 +57,7 @@ const defaultProps = {
showInput: false,

// day presentation and interaction related props
allowUnselect: false,
renderCalendarDay: undefined,
renderDayContents: null,
isDayBlocked: () => false,
Expand Down
3 changes: 2 additions & 1 deletion examples/SingleDatePickerWrapper.jsx
Expand Up @@ -8,7 +8,7 @@ import SingleDatePicker from '../src/components/SingleDatePicker';

import { SingleDatePickerPhrases } from '../src/defaultPhrases';
import SingleDatePickerShape from '../src/shapes/SingleDatePickerShape';
import { HORIZONTAL_ORIENTATION, ANCHOR_LEFT } from '../src/constants';
import { HORIZONTAL_ORIENTATION, ANCHOR_LEFT, OPEN_DOWN } from '../src/constants';
import isInclusivelyAfterDay from '../src/utils/isInclusivelyAfterDay';

const propTypes = {
Expand Down Expand Up @@ -56,6 +56,7 @@ const defaultProps = {
keepOpenOnDateSelect: false,
reopenPickerOnClearDate: false,
isRTL: false,
openDirection: OPEN_DOWN,

// navigation related props
navPrev: null,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -16,7 +16,7 @@
"lint": "eslint --ext .js,.jsx src test",
"mocha": "mocha ./test/_helpers",
"storybook:uninstall": "npm uninstall --no-save @storybook/react && rimraf node_modules/@storybook node_modules/react-modal node_modules/react-dom-factories",
"react": "enzyme-adapter-react-install 16",
"react": "NPM_CONFIG_LEGACY_PEER_DEPS=true enzyme-adapter-react-install 16",
"pretest": "npm run --silent lint",
"pretests-only": "npm run react",
"tests-only": "npm run mocha --silent",
Expand Down
9 changes: 9 additions & 0 deletions src/components/DayPicker.jsx
Expand Up @@ -391,10 +391,19 @@ class DayPicker extends React.PureComponent {
monthTitleHeight,
} = this.state;

let shouldAdjustHeight = false;
if (numberOfMonths !== prevProps.numberOfMonths) {
this.setCalendarMonthWeeks(currentMonth);
shouldAdjustHeight = true;
}
if (
this.isHorizontal()
&& (orientation !== prevProps.orientation || daySize !== prevProps.daySize)
) {
shouldAdjustHeight = true;
}

if (shouldAdjustHeight) {
const visibleCalendarWeeks = this.calendarMonthWeeks.slice(1, numberOfMonths + 1);
const calendarMonthWeeksHeight = Math.max(0, ...visibleCalendarWeeks) * (daySize - 1);
const newMonthHeight = monthTitleHeight + calendarMonthWeeksHeight + 1;
Expand Down
16 changes: 13 additions & 3 deletions src/components/DayPickerSingleDateController.jsx
Expand Up @@ -34,12 +34,18 @@ import {
import DayPicker from './DayPicker';
import getPooledMoment from '../utils/getPooledMoment';

// Default value of the date property. Represents the state
// when there is no date selected.
// TODO: use null
const DATE_UNSET_VALUE = undefined;

const propTypes = forbidExtraProps({
date: momentPropTypes.momentObj,
minDate: momentPropTypes.momentObj,
maxDate: momentPropTypes.momentObj,
onDateChange: PropTypes.func,

allowUnselect: PropTypes.bool,
focused: PropTypes.bool,
onFocusChange: PropTypes.func,
onClose: PropTypes.func,
Expand Down Expand Up @@ -102,11 +108,12 @@ const propTypes = forbidExtraProps({
});

const defaultProps = {
date: undefined, // TODO: use null
date: DATE_UNSET_VALUE,
minDate: null,
maxDate: null,
onDateChange() {},

allowUnselect: false,
focused: false,
onFocusChange() {},
onClose() {},
Expand Down Expand Up @@ -352,16 +359,19 @@ export default class DayPickerSingleDateController extends React.PureComponent {
if (e) e.preventDefault();
if (this.isBlocked(day)) return;
const {
allowUnselect,
onDateChange,
keepOpenOnDateSelect,
onFocusChange,
onClose,
} = this.props;

onDateChange(day);
const clickedDay = allowUnselect && this.isSelected(day) ? DATE_UNSET_VALUE : day;

onDateChange(clickedDay);
if (!keepOpenOnDateSelect) {
onFocusChange({ focused: false });
onClose({ date: day });
onClose({ date: clickedDay });
}
}

Expand Down
8 changes: 8 additions & 0 deletions stories/DayPickerSingleDateController.js
Expand Up @@ -145,6 +145,14 @@ storiesOf('DayPickerSingleDateController', module)
onNextMonthClick={action('DayPickerSingleDateController::onNextMonthClick')}
/>
)))
.add('with day unselection', withInfo()(() => (
<DayPickerSingleDateControllerWrapper
onOutsideClick={action('DayPickerSingleDateController::onOutsideClick')}
onPrevMonthClick={action('DayPickerSingleDateController::onPrevMonthClick')}
onNextMonthClick={action('DayPickerSingleDateController::onNextMonthClick')}
allowUnselect
/>
)))
.add('with custom input', withInfo()(() => (
<DayPickerSingleDateControllerWrapper
onOutsideClick={action('DayPickerSingleDateController::onOutsideClick')}
Expand Down
34 changes: 34 additions & 0 deletions test/components/DayPickerSingleDateController_spec.jsx
Expand Up @@ -539,6 +539,40 @@ describe('DayPickerSingleDateController', () => {
expect(onDateChangeStub.callCount).to.equal(1);
});

it('props.onDateChange receives undefined when day selected', () => {
const date = moment();
const onDateChangeStub = sinon.stub();
const wrapper = shallow((
<DayPickerSingleDateController
onDateChange={onDateChangeStub}
onFocusChange={() => {}}
date={date}
allowUnselect
/>
));
// Click same day as the provided date.
wrapper.instance().onDayClick(date);
expect(onDateChangeStub.callCount).to.equal(1);
expect(onDateChangeStub.getCall(0).args[0]).to.equal(undefined);
});

it('props.onDateChange receives day when allowUnselect is disabled', () => {
const date = moment();
const onDateChangeStub = sinon.stub();
const wrapper = shallow((
<DayPickerSingleDateController
onDateChange={onDateChangeStub}
onFocusChange={() => {}}
date={date}
allowUnselect={false}
/>
));
// Click same day as the provided date.
wrapper.instance().onDayClick(date);
expect(onDateChangeStub.callCount).to.equal(1);
expect(onDateChangeStub.getCall(0).args[0]).to.equal(date);
});

describe('props.keepOpenOnDateSelect is false', () => {
it('props.onFocusChange is called', () => {
const onFocusChangeStub = sinon.stub();
Expand Down
58 changes: 38 additions & 20 deletions test/components/DayPicker_spec.jsx
Expand Up @@ -12,6 +12,7 @@ import CalendarMonthGrid from '../../src/components/CalendarMonthGrid';
import DayPickerNavigation from '../../src/components/DayPickerNavigation';
import DayPickerKeyboardShortcuts from '../../src/components/DayPickerKeyboardShortcuts';
import {
DAY_SIZE,
HORIZONTAL_ORIENTATION,
VERTICAL_ORIENTATION,
VERTICAL_SCROLLABLE,
Expand All @@ -22,8 +23,9 @@ const today = moment().locale('en');
const event = { preventDefault() {}, stopPropagation() {} };

describe('DayPicker', () => {
let adjustDayPickerHeightSpy;
beforeEach(() => {
sinon.stub(PureDayPicker.prototype, 'adjustDayPickerHeight');
adjustDayPickerHeightSpy = sinon.stub(PureDayPicker.prototype, 'adjustDayPickerHeight');
});

afterEach(() => {
Expand Down Expand Up @@ -907,13 +909,8 @@ describe('DayPicker', () => {
});
});

describe.skip('life cycle methods', () => {
let adjustDayPickerHeightSpy;
beforeEach(() => {
adjustDayPickerHeightSpy = sinon.stub(PureDayPicker.prototype, 'adjustDayPickerHeight');
});

describe('#componentDidMount', () => {
describe('life cycle methods', () => {
describe.skip('#componentDidMount', () => {
describe('props.orientation === HORIZONTAL_ORIENTATION', () => {
it('calls adjustDayPickerHeight', () => {
mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
Expand All @@ -927,7 +924,7 @@ describe('DayPicker', () => {
});
});

describe('props.orientation === VERTICAL_ORIENTATION', () => {
describe.skip('props.orientation === VERTICAL_ORIENTATION', () => {
it('does not call adjustDayPickerHeight', () => {
mount(<DayPicker orientation={VERTICAL_ORIENTATION} />);
expect(adjustDayPickerHeightSpy.called).to.equal(false);
Expand All @@ -949,7 +946,7 @@ describe('DayPicker', () => {
});
});

describe('#componentWillReceiveProps', () => {
describe.skip('#componentWillReceiveProps', () => {
describe('props.orientation === VERTICAL_SCROLLABLE', () => {
it('updates state.currentMonthScrollTop', () => {
sinon.spy(DayPicker.prototype, 'setTransitionContainerRef');
Expand All @@ -966,39 +963,40 @@ describe('DayPicker', () => {

describe('#componentDidUpdate', () => {
let updateStateAfterMonthTransitionSpy;

beforeEach(() => {
updateStateAfterMonthTransitionSpy = sinon.stub(
DayPicker.prototype,
PureDayPicker.prototype,
'updateStateAfterMonthTransition',
);
});

describe('props.orientation === HORIZONTAL_ORIENTATION', () => {
it('calls adjustDayPickerHeight if state.monthTransition is truthy', () => {
it.skip('calls adjustDayPickerHeight if state.monthTransition is truthy', () => {
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
wrapper.setState({
monthTransition: 'foo',
});
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
});

it('does not call adjustDayPickerHeight if state.monthTransition is falsy', () => {
it.skip('does not call adjustDayPickerHeight if state.monthTransition is falsy', () => {
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
wrapper.setState({
monthTransition: null,
});
expect(adjustDayPickerHeightSpy.calledTwice).to.equal(false);
});

it('calls adjustDayPickerHeight if orientation has changed from HORIZONTAL_ORIENTATION to VERTICAL_ORIENTATION', () => {
it.skip('calls adjustDayPickerHeight if orientation has changed from HORIZONTAL_ORIENTATION to VERTICAL_ORIENTATION', () => {
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
wrapper.setState({
orientation: VERTICAL_ORIENTATION,
});
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
});

it('calls adjustDayPickerHeight if daySize has changed', () => {
it.skip('calls adjustDayPickerHeight if daySize has changed', () => {
const wrapper = mount(<DayPicker daySize={39} orientation={HORIZONTAL_ORIENTATION} />);
wrapper.setState({
daySize: 40,
Expand All @@ -1007,24 +1005,44 @@ describe('DayPicker', () => {
expect(adjustDayPickerHeightSpy).to.have.property('callCount', 2);
});

it('calls updateStateAfterMonthTransition if state.monthTransition is truthy', () => {
it.skip('calls updateStateAfterMonthTransition if state.monthTransition is truthy', () => {
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
wrapper.setState({
monthTransition: 'foo',
});
expect(updateStateAfterMonthTransitionSpy).to.have.property('callCount', 1);
});

it('does not call updateStateAfterMonthTransition if state.monthTransition is falsy', () => {
it.skip('does not call updateStateAfterMonthTransition if state.monthTransition is falsy', () => {
const wrapper = mount(<DayPicker orientation={HORIZONTAL_ORIENTATION} />);
wrapper.setState({
monthTransition: null,
});
expect(updateStateAfterMonthTransitionSpy.calledOnce).to.equal(false);
});

it('calls adjustDayPickerHeightSpy if props.numberOfMonths changes', () => {
const wrapper = shallow(<DayPicker numberOfMonths={2} />).dive();
wrapper.instance().componentDidUpdate({
daySize: DAY_SIZE,
numberOfMonths: 3,
orientation: HORIZONTAL_ORIENTATION,
});
expect(adjustDayPickerHeightSpy.callCount).to.equal(1);
});

it('does not call adjustDayPickerHeightSpy if props.numberOfMonths does not change', () => {
const wrapper = shallow(<DayPicker numberOfMonths={2} />).dive();
wrapper.instance().componentDidUpdate({
daySize: DAY_SIZE,
numberOfMonths: 2,
orientation: HORIZONTAL_ORIENTATION,
});
expect(adjustDayPickerHeightSpy.called).to.equal(false);
});
});

describe('props.orientation === VERTICAL_ORIENTATION', () => {
describe.skip('props.orientation === VERTICAL_ORIENTATION', () => {
it('does not call adjustDayPickerHeight if state.monthTransition is truthy', () => {
const wrapper = mount(<DayPicker orientation={VERTICAL_ORIENTATION} />);
wrapper.setState({
Expand Down Expand Up @@ -1075,7 +1093,7 @@ describe('DayPicker', () => {
});
});

describe('props.orientation === VERTICAL_SCROLLABLE', () => {
describe.skip('props.orientation === VERTICAL_SCROLLABLE', () => {
it('does not update transitionContainer ref`s scrollTop currentMonth stays the same', () => {
sinon.spy(DayPicker.prototype, 'setTransitionContainerRef');
const wrapper = mount(<DayPicker orientation={VERTICAL_SCROLLABLE} />);
Expand All @@ -1097,7 +1115,7 @@ describe('DayPicker', () => {
});
});

describe('when isFocused is updated to true', () => {
describe.skip('when isFocused is updated to true', () => {
const prevProps = { isFocused: false };
const newProps = { isFocused: true };

Expand Down

0 comments on commit 4d9b682

Please sign in to comment.