Skip to content

Commit

Permalink
Merge pull request #5100 from Qubitza/main
Browse files Browse the repository at this point in the history
Added option to hide time caption
  • Loading branch information
martijnrusschen authored Sep 19, 2024
2 parents f75e1e6 + 342d370 commit 3e28fca
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
5 changes: 5 additions & 0 deletions docs-site/src/components/Examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Default from "../../examples/default";
import NoAnchorArrow from "../../examples/noAnchorArrow";
import ShowTime from "../../examples/showTime";
import ShowTimeOnly from "../../examples/showTimeOnly";
import HideTimeCaption from "../../examples/hideTimeCaption";
import ExcludeTimes from "../../examples/excludeTimes";
import IncludeTimes from "../../examples/includeTimes";
import InjectTimes from "../../examples/injectTimes";
Expand Down Expand Up @@ -487,6 +488,10 @@ export default class exampleComponents extends React.Component {
title: "Select Time Only",
component: ShowTimeOnly,
},
{
title: "Hide Time Caption",
component: HideTimeCaption,
},
{
title: "Show previous months",
component: MultiMonthPrevious,
Expand Down
14 changes: 14 additions & 0 deletions docs-site/src/examples/hideTimeCaption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
() => {
const [startDate, setStartDate] = useState(new Date());
return (
<DatePicker
selected={startDate}
onChange={(date) => setStartDate(date)}
showTimeSelect
showTimeSelectOnly
timeIntervals={15}
dateFormat="h:mm aa"
showTimeCaption={false}
/>
);
};
35 changes: 35 additions & 0 deletions src/test/timepicker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,41 @@ describe("TimePicker", () => {
expect(onKeyDownSpy).toHaveBeenCalledTimes(1);
});

it("shows custom time caption text", () => {
const { container } = render(
<DatePicker
open
showTimeSelect
showTimeSelectOnly
timeCaption="Custom time"
/>,
);

const header = container.querySelector(
".react-datepicker__header--time--only",
);

expect(header).not.toBeNull();
expect(header?.textContent).toEqual("Custom time");
});

it("hides time caption", () => {
const { container } = render(
<DatePicker
open
showTimeSelect
showTimeSelectOnly
showTimeCaption={false}
/>,
);

const header = container.querySelector(
".react-datepicker__header--time--only",
);

expect(header).toBeNull();
});

function setManually(string: string) {
if (!instance?.input) {
throw new Error("input is null/undefined");
Expand Down
40 changes: 26 additions & 14 deletions src/time.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface TimeProps
handleOnKeyDown?: React.KeyboardEventHandler<HTMLLIElement>;
locale?: Locale;
showTimeSelectOnly?: boolean;
showTimeCaption?: boolean;
}

interface TimeState {
Expand All @@ -48,6 +49,7 @@ export default class Time extends Component<TimeProps, TimeState> {
intervals: 30,
todayButton: null,
timeCaption: "Time",
showTimeCaption: true,
};
}

Expand Down Expand Up @@ -250,6 +252,29 @@ export default class Time extends Component<TimeProps, TimeState> {
});
};

renderTimeCaption = (): JSX.Element => {
if (this.props.showTimeCaption === false) {
return <></>;
}

return (
<div
className={`react-datepicker__header react-datepicker__header--time ${
this.props.showTimeSelectOnly
? "react-datepicker__header--time--only"
: ""
}`}
ref={(header: HTMLDivElement) => {
this.header = header;
}}
>
<div className="react-datepicker-time__header">
{this.props.timeCaption}
</div>
</div>
);
};

render() {
const { height } = this.state;

Expand All @@ -261,20 +286,7 @@ export default class Time extends Component<TimeProps, TimeState> {
: ""
}`}
>
<div
className={`react-datepicker__header react-datepicker__header--time ${
this.props.showTimeSelectOnly
? "react-datepicker__header--time--only"
: ""
}`}
ref={(header: HTMLDivElement) => {
this.header = header;
}}
>
<div className="react-datepicker-time__header">
{this.props.timeCaption}
</div>
</div>
{this.renderTimeCaption()}
<div className="react-datepicker__time">
<div className="react-datepicker__time-box">
<ul
Expand Down

0 comments on commit 3e28fca

Please sign in to comment.