-
Notifications
You must be signed in to change notification settings - Fork 1
/
MonthCalendar.tsx
165 lines (146 loc) · 5.25 KB
/
MonthCalendar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import classNames from 'classnames';
import { computed, observable } from 'mobx';
import { observer } from 'mobx-react';
import { observePropsState } from 'mobx-react-helper';
import { Component, ReactNode } from 'react';
import { Badge, Button, ButtonProps, Table, TableProps } from 'react-bootstrap';
import {
changeMonth,
Day,
formatDate,
splitArray,
TimeData
} from 'web-utility';
import { text2color } from './color';
import { OverlayBox } from './OverlayBox';
export interface DateData {
date: TimeData;
content: ReactNode;
link?: string;
}
export interface MonthCalendarProps
extends Omit<TableProps, 'onChange' | 'onSelect'>,
Pick<ButtonProps, 'variant'> {
locale?: Navigator['language'];
value?: DateData[];
onSelect?: (value: DateData) => any;
onChange?: (date: Date) => any;
}
/**
* Re-implement from https://github.com/EasyWebApp/BootCell/blob/3d30027a97fe0a8c4ab8fabc8dfef22aede04de7/source/Calendar/MonthCalendar.tsx
*/
@observer
@observePropsState
export class MonthCalendar extends Component<MonthCalendarProps> {
static displayName = 'MonthCalendar';
declare observedProps: MonthCalendarProps;
@computed
get weekFormatter() {
const { locale = globalThis.navigator?.language } = this.observedProps;
return new Intl.DateTimeFormat(locale, { weekday: 'long' });
}
@observable
accessor currentDate = new Date();
@computed
get dateGrid() {
let startDate = new Date(this.currentDate);
startDate.setDate(1);
startDate = new Date(+startDate - startDate.getDay() * Day);
const dateList = Array.from(
new Array(42),
(_, index) => new Date(+startDate + index * Day)
);
return splitArray(dateList, 7);
}
changeMonth(delta: number) {
this.currentDate = changeMonth(this.currentDate, delta);
this.props.onChange?.(this.currentDate);
}
renderDate = (date: Date) => {
const { variant = 'primary', value, onSelect } = this.observedProps,
dateText = formatDate(date, 'YYYY-MM-DD');
const list = value?.filter(
({ date }) => formatDate(date, 'YYYY-MM-DD') === dateText
);
return (
<td
key={date + ''}
className={classNames({
'opacity-50':
date.getMonth() !== this.currentDate.getMonth(),
'fw-bold': dateText === formatDate(new Date(), 'YYYY-MM-DD')
})}
>
<time className="d-block" dateTime={date.toJSON()}>
{date.getDate()}
</time>
{list?.map(item =>
typeof item.content === 'object' ? (
item.content
) : (
<OverlayBox key={item.date + ''} title={item.content}>
<Badge
as="a"
className="d-inline-block text-decoration-none w-100 text-truncate"
bg={text2color(item.content + '', ['light'])}
href={item.link}
onClick={() => onSelect?.(item)}
>
{item.content}
</Badge>
</OverlayBox>
)
)}
</td>
);
};
render() {
const { weekFormatter, currentDate, dateGrid } = this,
{
style,
variant = 'primary',
onChange,
onSelect,
...props
} = this.observedProps;
return (
<Table {...props} style={{ tableLayout: 'fixed', ...style }}>
<caption>
<div className="d-flex justify-content-between align-items-center">
<Button
variant={variant}
onClick={() => this.changeMonth(-1)}
>
<
</Button>
{formatDate(currentDate, 'YYYY-MM')}
<Button
variant={variant}
onClick={() => this.changeMonth(1)}
>
>
</Button>
</div>
</caption>
<thead>
<tr>
{dateGrid[0].map((date, index, { length }) => (
<td
key={index}
className={`bg-${variant} text-white`}
style={{ width: `calc(100% / ${length})` }}
>
{weekFormatter.format(date)}
</td>
))}
</tr>
</thead>
<tbody>
{dateGrid.map(days => (
<tr key={days[0] + ''}>{days.map(this.renderDate)}</tr>
))}
</tbody>
</Table>
);
}
}