Skip to content

Commit f218699

Browse files
committed
Updating shortcuts to hide when ommited
When setting shortcut config settings, omit the buttons for unset options
1 parent 88b33d7 commit f218699

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/components/Shortcuts.tsx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,36 @@ const ItemTemplate = React.memo((props: ItemTemplateProps) => {
8383
);
8484
});
8585

86-
const Shortcuts = () => {
86+
const printItemText = (item: ShortcutsItem) => {
87+
return item.text ?? null;
88+
};
89+
90+
const Shortcuts: React.FC = () => {
8791
// Contexts
8892
const { configs } = useContext(DatepickerContext);
8993

9094
const callPastFunction = (data: unknown, numberValue: number) => {
9195
return typeof data === "function" ? data(numberValue) : null;
9296
};
9397

94-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
95-
// @ts-ignore
96-
const printItemText = item => {
97-
return "text" in item ? item.text : "";
98-
};
98+
const shortcutOptions = configs
99+
? Object.entries(DEFAULT_SHORTCUTS).filter(([key]) => {
100+
return configs.shortcuts ? Object.keys(configs.shortcuts).includes(key) : true;
101+
})
102+
: Object.entries(DEFAULT_SHORTCUTS);
99103

100104
return (
101105
<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">
102106
<ul className="w-full tracking-wide flex flex-wrap lg:flex-col pb-1 lg:pb-0">
103-
{Object.entries(DEFAULT_SHORTCUTS).map(([key, item], index) =>
104-
key === "past" ? (
105-
(Array.isArray(item) ? item : []).map((item, index) => (
107+
{shortcutOptions.map(([key, item], index) =>
108+
Array.isArray(item) ? (
109+
item.map((item, index) => (
106110
<ItemTemplate key={index} item={item}>
107111
<>
108-
{configs && configs.shortcuts && key in configs.shortcuts
112+
{key === "past" &&
113+
configs?.shortcuts &&
114+
key in configs.shortcuts &&
115+
item.daysNumber
109116
? callPastFunction(configs.shortcuts[key], item.daysNumber)
110117
: item.text}
111118
</>
@@ -114,7 +121,7 @@ const Shortcuts = () => {
114121
) : (
115122
<ItemTemplate key={index} item={item}>
116123
<>
117-
{configs && configs.shortcuts && key in configs.shortcuts
124+
{configs?.shortcuts && key in configs.shortcuts
118125
? configs.shortcuts[key as keyof typeof configs.shortcuts]
119126
: printItemText(item)}
120127
</>

src/constants/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import dayjs from "dayjs";
22

33
import { formatDate, previousMonth } from "../helpers";
44

5+
import { ShortcutsItem } from "types";
6+
57
export const COLORS = [
68
"blue",
79
"orange",
@@ -270,7 +272,9 @@ export const BUTTON_COLOR = {
270272
}
271273
};
272274

273-
export const DEFAULT_SHORTCUTS = {
275+
export const DEFAULT_SHORTCUTS: {
276+
[key in string]: ShortcutsItem | ShortcutsItem[];
277+
} = {
274278
today: {
275279
text: "Today",
276280
period: {

0 commit comments

Comments
 (0)