Skip to content

Commit b4c780f

Browse files
authored
Merge pull request #80 from giero/master
[Fix] inputClassName, containerClassName, and toggleClassName accept functions...
2 parents 8dbe933 + c135486 commit b4c780f

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

pages/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ export default function Playground() {
7878
i18n={i18n}
7979
disabled={disabled}
8080
inputClassName={inputClassName}
81-
/**
82-
* `twMerge` Test
83-
*/
84-
// inputClassName={twMerge(inputClassName, 'dark:bg-white')}
8581
containerClassName={containerClassName}
8682
toggleClassName={toggleClassName}
8783
displayFormat={displayFormat}

src/components/Datepicker.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,18 @@ const Datepicker: React.FC<DatepickerType> = ({
300300
inputRef
301301
]);
302302

303+
const defaultContainerClassName = "relative w-full text-gray-700";
304+
305+
const containerClassNameOverload =
306+
typeof containerClassName === "function"
307+
? containerClassName(defaultContainerClassName)
308+
: typeof containerClassName === "string" && containerClassName !== ""
309+
? containerClassName
310+
: defaultContainerClassName;
311+
303312
return (
304313
<DatepickerContext.Provider value={contextValues}>
305-
<div
306-
className={`relative w-full text-gray-700 ${containerClassName}`}
307-
ref={containerRef}
308-
>
314+
<div className={containerClassNameOverload} ref={containerRef}>
309315
<Input setContextRef={setInputRef} />
310316

311317
<div

src/components/Input.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,21 @@ const Input: React.FC<Props> = (e: Props) => {
5252
const getClassName = useCallback(() => {
5353
const input = inputRef.current;
5454

55-
if (input && typeof classNames != "undefined" && typeof classNames.input === "function") {
56-
return classNames?.input(input);
55+
if (input && typeof classNames !== "undefined" && typeof classNames?.input === "function") {
56+
return classNames.input(input);
5757
}
5858

5959
const border = BORDER_COLOR.focus[primaryColor as keyof typeof BORDER_COLOR.focus];
6060
const ring =
6161
RING_COLOR["second-focus"][primaryColor as keyof (typeof RING_COLOR)["second-focus"]];
62-
const classNameOverload = typeof inputClassName === "string" ? inputClassName : "";
63-
return `relative transition-all duration-300 py-2.5 pl-4 pr-14 w-full border-gray-300 dark:bg-slate-800 dark:text-white/80 dark:border-slate-600 rounded-lg tracking-wide font-light text-sm placeholder-gray-400 bg-white focus:ring disabled:opacity-40 disabled:cursor-not-allowed ${border} ${ring} ${classNameOverload}`;
62+
63+
const defaultInputClassName = `relative transition-all duration-300 py-2.5 pl-4 pr-14 w-full border-gray-300 dark:bg-slate-800 dark:text-white/80 dark:border-slate-600 rounded-lg tracking-wide font-light text-sm placeholder-gray-400 bg-white focus:ring disabled:opacity-40 disabled:cursor-not-allowed ${border} ${ring}`;
64+
65+
return typeof inputClassName === "function"
66+
? inputClassName(defaultInputClassName)
67+
: typeof inputClassName === "string" && inputClassName !== ""
68+
? inputClassName
69+
: defaultInputClassName;
6470
}, [inputRef, classNames, primaryColor, inputClassName]);
6571

6672
const handleInputChange = useCallback(
@@ -210,12 +216,19 @@ const Input: React.FC<Props> = (e: Props) => {
210216
if (
211217
button &&
212218
typeof classNames !== "undefined" &&
213-
typeof classNames.toggleButton === "function"
219+
typeof classNames?.toggleButton === "function"
214220
) {
215221
return classNames.toggleButton(button);
216222
}
217223

218-
return `absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed ${toggleClassName}`;
224+
const defaultToggleClassName =
225+
"absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed";
226+
227+
return typeof toggleClassName === "function"
228+
? toggleClassName(defaultToggleClassName)
229+
: typeof toggleClassName === "string" && toggleClassName !== ""
230+
? toggleClassName
231+
: defaultToggleClassName;
219232
}, [toggleClassName, buttonRef, classNames]);
220233

221234
return (

src/contexts/DatepickerContext.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
DateValueType,
99
DateType,
1010
DateRangeType,
11-
ClassNamesTypeProp,
12-
ClassNameParam
11+
ClassNamesTypeProp
1312
} from "../types";
1413

1514
interface DatepickerStore {
@@ -34,9 +33,9 @@ interface DatepickerStore {
3433
i18n: string;
3534
value: DateValueType;
3635
disabled?: boolean;
37-
inputClassName?: ((args?: ClassNameParam) => string) | string | null;
38-
containerClassName?: ((args?: ClassNameParam) => string) | string | null;
39-
toggleClassName?: ((args?: ClassNameParam) => string) | string | null;
36+
inputClassName?: ((className: string) => string) | string | null;
37+
containerClassName?: ((className: string) => string) | string | null;
38+
toggleClassName?: ((className: string) => string) | string | null;
4039
toggleIcon?: (open: boolean) => React.ReactNode;
4140
readOnly?: boolean;
4241
startWeekOn?: string | null;
@@ -46,7 +45,7 @@ interface DatepickerStore {
4645
disabledDates?: DateRangeType[] | null;
4746
inputId?: string;
4847
inputName?: string;
49-
classNames?: ClassNamesTypeProp | undefined;
48+
classNames?: ClassNamesTypeProp;
5049
}
5150

5251
const DatepickerContext = createContext<DatepickerStore>({

src/types/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export type ClassNamesTypeProp = {
4444
footer?: (p?: object | null | undefined) => string | undefined;
4545
};
4646

47-
export type ClassNameParam = ClassNameParam[] | string | number | boolean | undefined;
48-
4947
export interface DatepickerType {
5048
primaryColor?: string;
5149
value: DateValueType;
@@ -73,12 +71,12 @@ export interface DatepickerType {
7371
i18n?: string;
7472
disabled?: boolean;
7573
classNames?: ClassNamesTypeProp | undefined;
76-
inputClassName?: ((args?: ClassNameParam) => string) | string | null;
77-
toggleClassName?: string | null;
78-
toggleIcon?: ((open: ClassNameParam) => React.ReactNode) | undefined;
74+
containerClassName?: ((className: string) => string) | string | null;
75+
inputClassName?: ((className: string) => string) | string | null;
76+
toggleClassName?: ((className: string) => string) | string | null;
77+
toggleIcon?: (open: boolean) => React.ReactNode;
7978
inputId?: string;
8079
inputName?: string;
81-
containerClassName?: ((args?: ClassNameParam) => string) | string | null;
8280
displayFormat?: string;
8381
readOnly?: boolean;
8482
minDate?: DateType | null;

0 commit comments

Comments
 (0)