Skip to content

Add shortcutClassName and shortcutItemClassName properties to allow c… #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/Datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const Datepicker: React.FC<DatepickerType> = ({
i18n = LANGUAGE,
disabled = false,
inputClassName = null,
shortcutClassName = null,
shortcutItemClassName = null,
containerClassName = null,
toggleClassName = null,
toggleIcon = undefined,
Expand Down Expand Up @@ -267,6 +269,8 @@ const Datepicker: React.FC<DatepickerType> = ({
value,
disabled,
inputClassName,
shortcutClassName,
shortcutItemClassName,
containerClassName,
toggleClassName,
toggleIcon,
Expand Down Expand Up @@ -300,6 +304,8 @@ const Datepicker: React.FC<DatepickerType> = ({
value,
disabled,
inputClassName,
shortcutClassName,
shortcutItemClassName,
containerClassName,
toggleClassName,
toggleIcon,
Expand Down
27 changes: 22 additions & 5 deletions src/components/Shortcuts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ const ItemTemplate = React.memo((props: ItemTemplateProps) => {
dayHover,
changeDayHover,
hideDatepicker,
changeDatepickerValue
changeDatepickerValue,
shortcutItemClassName
} = useContext(DatepickerContext);

// Functions
const getClassName: () => string = useCallback(() => {
const textColor = TEXT_COLOR["600"][primaryColor as keyof (typeof TEXT_COLOR)["600"]];
const textColorHover = TEXT_COLOR.hover[primaryColor as keyof typeof TEXT_COLOR.hover];
return `whitespace-nowrap w-1/2 md:w-1/3 lg:w-auto transition-all duration-300 hover:bg-gray-100 dark:hover:bg-white/10 p-2 rounded cursor-pointer ${textColor} ${textColorHover}`;
}, [primaryColor]);

const defaultShortcutClassName = `whitespace-nowrap w-1/2 md:w-1/3 lg:w-auto transition-all duration-300 hover:bg-gray-100 dark:hover:bg-white/10 p-2 rounded cursor-pointer ${textColor} ${textColorHover}`;
return typeof shortcutItemClassName === "function"
? shortcutItemClassName(defaultShortcutClassName)
: typeof shortcutItemClassName === "string" && shortcutItemClassName !== ""
? shortcutItemClassName
: defaultShortcutClassName;
}, [primaryColor, shortcutItemClassName]);

const chosePeriod = useCallback(
(item: Period) => {
Expand Down Expand Up @@ -81,7 +88,7 @@ const ItemTemplate = React.memo((props: ItemTemplateProps) => {

const Shortcuts: React.FC = () => {
// Contexts
const { configs } = useContext(DatepickerContext);
const { configs, shortcutClassName } = useContext(DatepickerContext);

const callPastFunction = useCallback((data: unknown, numberValue: number) => {
return typeof data === "function" ? data(numberValue) : null;
Expand Down Expand Up @@ -131,9 +138,19 @@ const Shortcuts: React.FC = () => {
return item?.text ?? null;
}, []);

const shurtcutClassNameOverload = useMemo(() => {
const defaultShortcutClassName =
"w-full tracking-wide flex flex-wrap lg:flex-col pb-1 lg:pb-0";
return typeof shortcutClassName === "function"
? shortcutClassName(defaultShortcutClassName)
: typeof shortcutClassName === "string" && shortcutClassName !== ""
? shortcutClassName
: defaultShortcutClassName;
}, [shortcutClassName]);

return shortcutOptions?.length ? (
<div className="md:border-b mb-3 lg:mb-0 lg:border-r lg:border-b-0 border-gray-300 dark:border-gray-700 pr-1">
<ul className="w-full tracking-wide flex flex-wrap lg:flex-col pb-1 lg:pb-0">
<ul className={shurtcutClassNameOverload}>
{shortcutOptions.map(([key, item], index: number) =>
Array.isArray(item) ? (
item.map((item, index) => (
Expand Down
4 changes: 4 additions & 0 deletions src/contexts/DatepickerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ interface DatepickerStore {
value: DateValueType;
disabled?: boolean;
inputClassName?: ((className: string) => string) | string | null;
shortcutClassName?: ((className: string) => string) | string | null;
shortcutItemClassName?: ((className: string) => string) | string | null;
containerClassName?: ((className: string) => string) | string | null;
toggleClassName?: ((className: string) => string) | string | null;
toggleIcon?: (open: boolean) => React.ReactNode;
Expand Down Expand Up @@ -78,6 +80,8 @@ const DatepickerContext = createContext<DatepickerStore>({
i18n: LANGUAGE,
disabled: false,
inputClassName: "",
shortcutClassName: "",
shortcutItemClassName: "",
containerClassName: "",
toggleClassName: "",
readOnly: false,
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface DatepickerType {
disabled?: boolean;
classNames?: ClassNamesTypeProp | undefined;
containerClassName?: ((className: string) => string) | string | null;
shortcutClassName?: ((className: string) => string) | string | null;
shortcutItemClassName?: ((className: string) => string) | string | null;
inputClassName?: ((className: string) => string) | string | null;
toggleClassName?: ((className: string) => string) | string | null;
toggleIcon?: (open: boolean) => React.ReactNode;
Expand Down