Skip to content

Commit 55569d8

Browse files
authored
Merge pull request #76 from devinmatte/hide-unused-shortcuts
Updating shortcuts to hide when ommited
2 parents 88b33d7 + d0ec811 commit 55569d8

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/components/Shortcuts.tsx

+21-11
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,39 @@ 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
101+
? Object.keys(configs.shortcuts).includes(key) &&
102+
configs.shortcuts[key as keyof typeof configs.shortcuts]
103+
: true;
104+
})
105+
: Object.entries(DEFAULT_SHORTCUTS);
99106

100107
return (
101108
<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">
102109
<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) => (
110+
{shortcutOptions.map(([key, item], index) =>
111+
Array.isArray(item) ? (
112+
item.map((item, index) => (
106113
<ItemTemplate key={index} item={item}>
107114
<>
108-
{configs && configs.shortcuts && key in configs.shortcuts
115+
{key === "past" &&
116+
configs?.shortcuts &&
117+
key in configs.shortcuts &&
118+
item.daysNumber
109119
? callPastFunction(configs.shortcuts[key], item.daysNumber)
110120
: item.text}
111121
</>
@@ -114,7 +124,7 @@ const Shortcuts = () => {
114124
) : (
115125
<ItemTemplate key={index} item={item}>
116126
<>
117-
{configs && configs.shortcuts && key in configs.shortcuts
127+
{configs?.shortcuts && key in configs.shortcuts
118128
? configs.shortcuts[key as keyof typeof configs.shortcuts]
119129
: printItemText(item)}
120130
</>

src/constants/index.ts

+5-1
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)