Skip to content

Commit b1b0afe

Browse files
authored
Merge pull request #133 from t0m3k/fixShortcutsTypes
Fix shortcuts types
2 parents 3a88d5f + b946333 commit b1b0afe

File tree

3 files changed

+45
-47
lines changed

3 files changed

+45
-47
lines changed

react-tailwindcss-datepicker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 21e1da23ca1591cc8fd3b7bcefae00c168b0a1b1

src/components/Shortcuts.tsx

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -91,46 +91,41 @@ const Shortcuts: React.FC = () => {
9191
}, []);
9292

9393
const shortcutOptions = useMemo<[string, ShortcutsItem | ShortcutsItem[]][]>(() => {
94-
let options;
95-
if (configs?.shortcuts && configs?.shortcuts) {
96-
const formatConfig = Object.keys(configs.shortcuts).map(item => {
94+
const options: [string, ShortcutsItem | ShortcutsItem[]][] = [];
95+
if (configs && configs.shortcuts) {
96+
Object.keys(configs.shortcuts).forEach(item => {
9797
if (Object.keys(DEFAULT_SHORTCUTS).includes(item)) {
98-
return [item, DEFAULT_SHORTCUTS[item]];
99-
} else {
100-
// using | makes this fail in typecheck as [string] is no longer recognised?
101-
if (configs.shortcuts && configs?.shortcuts[item]) {
102-
const customConfig = configs?.shortcuts[item];
103-
const text = customConfig?.text;
104-
const start = dayjs(customConfig?.period?.start);
105-
const end = dayjs(customConfig?.period?.end);
106-
if (
107-
text &&
108-
start.isValid() &&
109-
end.isValid() &&
110-
(start.isBefore(end) || start.isSame(end))
111-
) {
112-
return [
113-
item,
114-
{
115-
text,
116-
period: {
117-
start: start.format(DATE_FORMAT),
118-
end: end.format(DATE_FORMAT)
119-
}
120-
}
121-
];
122-
} else {
123-
return undefined;
124-
}
125-
}
126-
return undefined;
98+
options.push([item, DEFAULT_SHORTCUTS[item]]);
12799
}
128100
});
129-
options = formatConfig?.filter(item => !!item);
101+
if (configs.shortcuts.custom && configs.shortcuts.custom.length > 0) {
102+
configs.shortcuts.custom.forEach(customConfig => {
103+
const text = customConfig.text;
104+
const start = dayjs(customConfig.period.start);
105+
const end = dayjs(customConfig.period.end);
106+
if (
107+
text &&
108+
start.isValid() &&
109+
end.isValid() &&
110+
(start.isBefore(end) || start.isSame(end))
111+
) {
112+
options.push([
113+
text,
114+
{
115+
text,
116+
period: {
117+
start: start.format(DATE_FORMAT),
118+
end: end.format(DATE_FORMAT)
119+
}
120+
}
121+
]);
122+
}
123+
});
124+
}
130125
} else {
131-
options = Object.entries(DEFAULT_SHORTCUTS);
126+
return Object.entries(DEFAULT_SHORTCUTS);
132127
}
133-
return options as [string, ShortcutsItem | ShortcutsItem[]][];
128+
return options;
134129
}, [configs]);
135130

136131
const printItemText = useCallback((item: ShortcutsItem) => {
@@ -140,7 +135,7 @@ const Shortcuts: React.FC = () => {
140135
return shortcutOptions?.length ? (
141136
<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">
142137
<ul className="w-full tracking-wide flex flex-wrap lg:flex-col pb-1 lg:pb-0">
143-
{shortcutOptions?.map(([key, item], index: number) =>
138+
{shortcutOptions.map(([key, item], index: number) =>
144139
Array.isArray(item) ? (
145140
item.map((item, index) => (
146141
<ItemTemplate key={index} item={item}>

src/types/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ export interface Period {
77
end: string | null;
88
}
99

10+
interface DefaultShortcuts {
11+
today?: string;
12+
yesterday?: string;
13+
past?: (period: number) => string;
14+
currentMonth?: string;
15+
pastMonth?: string;
16+
custom?: ShortcutsItem[];
17+
}
1018
export interface Configs {
11-
shortcuts?: {
12-
today?: string;
13-
yesterday?: string;
14-
past?: (period: number) => string;
15-
currentMonth?: string;
16-
pastMonth?: string;
17-
} & { [key: string]: ShortcutsItem };
19+
shortcuts?: DefaultShortcuts;
1820
footer?: {
1921
cancel?: string;
2022
apply?: string;
2123
};
2224
}
2325

2426
export interface ShortcutsItem {
25-
text?: string;
27+
text: string;
2628
daysNumber?: number;
27-
period?: {
28-
start: string;
29-
end: string;
29+
period: {
30+
start: Date | string;
31+
end: Date | string;
3032
};
3133
}
3234

0 commit comments

Comments
 (0)