Skip to content

Commit f0bdfc6

Browse files
committed
[Feature] Customization prop: toggleIcon (#49)
1 parent 1760383 commit f0bdfc6

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

pages/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export default function Playground() {
6969
maxDate={maxDate}
7070
disabledDates={disabledDates}
7171
startWeekOn={startWeekOn}
72+
toggleIcon={isEmpty => {
73+
return isEmpty ? "Select Date" : "Clear";
74+
}}
7275
/>
7376
</div>
7477

src/components/Datepicker.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface Props {
4141
disabled?: boolean;
4242
inputClassName?: string | null;
4343
toggleClassName?: string | null;
44+
toggleIcon?: ((open: boolean) => React.ReactNode) | undefined;
4445
inputId?: string;
4546
inputName?: string;
4647
containerClassName?: string | null;
@@ -69,6 +70,7 @@ const Datepicker: React.FC<Props> = ({
6970
inputClassName = null,
7071
containerClassName = null,
7172
toggleClassName = null,
73+
toggleIcon = undefined,
7274
displayFormat = "YYYY-MM-DD",
7375
readOnly = false,
7476
minDate = null,
@@ -286,6 +288,7 @@ const Datepicker: React.FC<Props> = ({
286288
inputClassName,
287289
containerClassName,
288290
toggleClassName,
291+
toggleIcon,
289292
readOnly,
290293
displayFormat,
291294
minDate,
@@ -313,6 +316,7 @@ const Datepicker: React.FC<Props> = ({
313316
inputClassName,
314317
containerClassName,
315318
toggleClassName,
319+
toggleIcon,
316320
readOnly,
317321
displayFormat,
318322
firstGotoDate,

src/components/Input.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BORDER_COLOR, RING_COLOR } from "../constants";
55
import DatepickerContext from "../contexts/DatepickerContext";
66
import { dateIsValid } from "../helpers";
77

8-
import { CloseIcon, DateIcon } from "./utils";
8+
import ToggleButton from "./ToggleButton";
99

1010
const Input: React.FC = () => {
1111
// Context
@@ -26,6 +26,7 @@ const Input: React.FC = () => {
2626
disabled,
2727
inputClassName,
2828
toggleClassName,
29+
toggleIcon,
2930
readOnly,
3031
displayFormat,
3132
inputId,
@@ -166,6 +167,17 @@ const Input: React.FC = () => {
166167
};
167168
}, [calendarContainer, arrowContainer]);
168169

170+
const renderToggleIcon = useCallback(
171+
(isEmpty: boolean) => {
172+
return typeof toggleIcon === "undefined" ? (
173+
<ToggleButton isEmpty={isEmpty} />
174+
) : (
175+
toggleIcon(isEmpty)
176+
);
177+
},
178+
[toggleIcon]
179+
);
180+
169181
return (
170182
<>
171183
<input
@@ -193,7 +205,7 @@ const Input: React.FC = () => {
193205
disabled={disabled}
194206
className={`absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed ${toggleClassName}`}
195207
>
196-
{inputText ? <CloseIcon className="h-5 w-5" /> : <DateIcon className="h-5 w-5" />}
208+
{renderToggleIcon(inputText == null || (inputText != null && !inputText.length))}
197209
</button>
198210
</>
199211
);

src/components/ToggleButton.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react";
2+
3+
import { CloseIcon, DateIcon } from "./utils";
4+
5+
interface ToggleButtonProps {
6+
isEmpty?: boolean | undefined;
7+
}
8+
9+
const ToggleButton: React.FC<ToggleButtonProps> = (e: ToggleButtonProps): JSX.Element => {
10+
return e.isEmpty ? <CloseIcon className="h-5 w-5" /> : <DateIcon className="h-5 w-5" />;
11+
};
12+
13+
export default ToggleButton;

src/contexts/DatepickerContext.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface DatepickerStore {
2727
inputClassName?: string | null;
2828
containerClassName?: string | null;
2929
toggleClassName?: string | null;
30+
toggleIcon?: (open: boolean) => React.ReactNode;
3031
readOnly?: boolean;
3132
startWeekOn?: string | null;
3233
displayFormat?: string;
@@ -70,7 +71,8 @@ const DatepickerContext = createContext<DatepickerStore>({
7071
disabledDates: null,
7172
inputId: undefined,
7273
inputName: undefined,
73-
startWeekOn: "sun"
74+
startWeekOn: "sun",
75+
toggleIcon: undefined
7476
});
7577

7678
export default DatepickerContext;

0 commit comments

Comments
 (0)