Skip to content

Commit

Permalink
Merge pull request #91 from newrelic/date-picker
Browse files Browse the repository at this point in the history
fix: disallow bubbling of onclick
  • Loading branch information
amit-y committed Dec 19, 2023
2 parents 4805f44 + b3702b0 commit 628e3e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- DatePicker - disallow bubbling of onclick event, which was causing parent popover to close

## [1.21.0] - 2023-12-18

### Added
Expand Down
1 change: 1 addition & 0 deletions src/components/date-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ function MyComponent() {

- date (date): The default date
- onChange (function): A function that is called when the user selects a date
- validFrom (date): only allow dates starting from the provided value
37 changes: 22 additions & 15 deletions src/components/date-picker/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import PropTypes from 'prop-types';

import { Icon, Popover, PopoverTrigger, PopoverBody, TextField } from 'nr1';
Expand All @@ -18,7 +18,7 @@ const DAYS_OF_WEEK = daysOfWeek();

import styles from './styles.scss';

const DatePicker = ({ date, onChange, validFrom }) => {
const DatePicker = ({ date, validFrom, onChange }) => {
const [opened, setOpened] = useState(false);
const [current, setCurrent] = useState(extractDateParts(new Date()));

Expand All @@ -37,22 +37,29 @@ const DatePicker = ({ date, onChange, validFrom }) => {
setCurrent(extractDateParts(nextMo));
};

const isDateInCurrentMonth = (d = new Date()) =>
d.getFullYear() === current.yr && d.getMonth() === current.mo;
const isDateInCurrentMonth = useCallback(
(d = new Date()) =>
d.getFullYear() === current.yr && d.getMonth() === current.mo,
[current]
);

const clickHandler = (dt) => {
if (!isSelectableDate(current, dt + 1, validFrom) || !onChange) return;
const clickHandler = useCallback(
(e, dt) => {
e.stopPropagation();
if (!isSelectableDate(current, dt + 1, validFrom) || !onChange) return;

const d = date instanceof Date ? new Date(date.getTime()) : new Date();
d.setFullYear(current.yr);
d.setMonth(current.mo);
d.setDate(dt + 1);
onChange(d);
const d = date instanceof Date ? new Date(date.getTime()) : new Date();
d.setFullYear(current.yr);
d.setMonth(current.mo);
d.setDate(dt + 1);
onChange(d);

setOpened(false);
};
setOpened(false);
},
[current]
);

const changeHandler = (_, o) => setOpened(o);
const changeHandler = useCallback((_, o) => setOpened(o), []);

return (
<Popover opened={opened} onChange={changeHandler}>
Expand Down Expand Up @@ -101,7 +108,7 @@ const DatePicker = ({ date, onChange, validFrom }) => {
? styles.disabled
: ''
}`}
onClick={() => clickHandler(i)}
onClick={(e) => clickHandler(e, i)}
>
{i + 1}
</div>
Expand Down

0 comments on commit 628e3e6

Please sign in to comment.