This repository was archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.tsx
More file actions
189 lines (172 loc) · 5.16 KB
/
index.tsx
File metadata and controls
189 lines (172 loc) · 5.16 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { CalendarAlt } from '@styled-icons/fa-solid/CalendarAlt';
import styled, { useTheme } from 'styled-components';
import { flex, position } from '../../Utils/Mixins';
import { useTransition } from '../../Utils/Hooks';
import {
InputFragment,
LabelLayout as LL,
LabelLayoutProps,
} from '../../Fragments';
import { Datebox, IDataObject } from './Datebox';
const printDate = (date?: Date): string => {
if (date) {
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const year = date.getFullYear();
return `${month}-${day}-${year}`;
}
return '';
};
export interface DatepickerProps extends LabelLayoutProps {
disabled?: boolean;
placeholder?: string;
onChange?: Function;
onClear?: Function;
value?: Date;
initialShow?: boolean;
adjustedPriceDays?: IDataObject[];
}
export const Datepicker: React.FC<DatepickerProps> = ({
value,
onChange = (): void => undefined,
onClear = (): void => undefined,
placeholder = 'MM-DD-YYYY',
initialShow,
adjustedPriceDays = [
{ date: new Date(0, 0, 0), price: '0', priceStatus: 0 },
],
...props
}): React.ReactElement => {
const theme = useTheme();
const [selectedDate, setDate] = useState(value);
const ref = useRef<HTMLDivElement>(null);
const dateText = useMemo(
(): string => (printDate(value) ? printDate(value) : placeholder),
[value],
);
const [show, setShow] = useState(initialShow || false);
const [text, setText] = useState(dateText);
const [, mount, animate] = useTransition(show, {
end: theme.speed.normal,
});
useEffect((): void | (() => undefined | void) => {
if (!mount) return undefined;
const handler = ({ target }: MouseEvent): void => {
if (ref.current && !ref.current.contains(target as HTMLElement)) {
setShow(false);
}
};
window.setTimeout((): void => {
window.addEventListener('click', handler);
}, 100);
return (): void => {
window.removeEventListener('click', handler);
};
}, [mount]);
useEffect((): void => {
setDate(value);
setText(printDate(value));
}, [value]);
const handleText = useCallback(
(el): void => setText(el.target.value),
[text],
);
const selectDate = useCallback((el): void => {
const val = new Date(el.target.getAttribute('data'));
el.target = {
...el.target,
name: props.name,
value: val,
};
onChange(el);
setText(printDate(val));
setDate(val);
}, []);
const changePage = useCallback(
(change = 1): React.MouseEventHandler =>
(): void => {
setDate((d): Date => {
const curr: Date = new Date(d || new Date());
curr.setMonth(curr.getMonth() + change);
return curr;
});
},
[],
);
const clearDate = (): void => {
setText(placeholder);
setShow(false);
onClear();
};
const handleKeys = useCallback(
(el): void => {
const d = new Date(el.target.value);
switch (el.key) {
case 'Tab':
setShow(false);
break;
case 'Enter':
el.target = {
...el.target,
name: props.name,
value: d,
};
if (d.toDateString() === value?.toDateString()) {
setShow((v): boolean => !v);
} else if (!Number.isNaN(d.getTime())) {
setText(printDate(d));
onChange(el);
}
break;
default:
break;
}
},
[value],
);
return (
<LabelLayout ref={ref} {...props}>
<Wrapper>
<InputFragment
{...props}
placeholder={placeholder}
onChange={handleText}
onFocus={(): void => setShow(true)}
onKeyDown={handleKeys}
value={text}
/>
<Icon />
</Wrapper>
{mount && (
<Datebox
changePage={changePage}
selectedDate={selectedDate}
selectDate={selectDate}
animate={animate}
value={value}
clearDate={clearDate}
adjustedPriceDays={adjustedPriceDays}
/>
)}
</LabelLayout>
);
};
const LabelLayout = styled(LL)<{ ref: React.RefObject<HTMLDivElement> }>`
position: relative;
`;
const Icon = styled(CalendarAlt)`
${position('absolute', 'auto 20px auto auto')}
width: 10px;
`;
const Wrapper = styled.div`
${flex('column')}
position: relative;
`;
export default Datepicker;