|
1 |
| -import React, { useContext, useRef } from 'react'; |
| 1 | +import React, { useContext, useRef, useState } from 'react'; |
2 | 2 | import { DatePickerContext } from './DatePickerContext';
|
3 | 3 | import { SpinnButton } from './SpinnButton';
|
| 4 | +import { PadZero } from './PadZero'; |
| 5 | +import { Button } from '../button'; |
| 6 | +import { Calendar } from './calendar'; |
| 7 | +import { CalendarWrapper, CalendarWrapperHandle } from './CalenderWrapper'; |
4 | 8 |
|
5 | 9 | export const DatePickerComp: React.FC = () => {
|
6 |
| - /* |
7 |
| - const dayRef = useRef<HTMLSpanElement>(null); |
8 |
| -*/ |
| 10 | + const containerRef = useRef<HTMLDivElement>(null); |
9 | 11 | const monthRef = useRef<HTMLSpanElement>(null);
|
10 | 12 | const yearRef = useRef<HTMLSpanElement>(null);
|
11 |
| - const { day, setDay, year, setYear, month, setMonth } = |
| 13 | + const { day, setDay, year, setYear, month, setMonth, locale } = |
12 | 14 | useContext(DatePickerContext);
|
13 |
| - |
14 |
| - const handleDayChange = (evt: React.KeyboardEvent<HTMLSpanElement>) => { |
15 |
| - evt.stopPropagation(); |
16 |
| - if (/\d/.test(evt.key)) { |
17 |
| - setDay(parseInt(evt.key), () => |
18 |
| - monthRef.current?.focus({ preventScroll: true }), |
19 |
| - ); |
20 |
| - } |
21 |
| - }; |
22 |
| - |
23 |
| - const handleMonthChange = (evt: React.KeyboardEvent<HTMLSpanElement>) => { |
24 |
| - evt.stopPropagation(); |
25 |
| - if (/\d/.test(evt.key)) { |
26 |
| - setMonth(parseInt(evt.key), () => |
27 |
| - yearRef.current?.focus({ preventScroll: true }), |
28 |
| - ); |
29 |
| - } |
30 |
| - }; |
31 |
| - |
32 |
| - const handleYearChange = (evt: React.KeyboardEvent<HTMLSpanElement>) => { |
33 |
| - evt.stopPropagation(); |
34 |
| - if (/\d/.test(evt.key)) { |
35 |
| - setYear(parseInt(evt.key)); |
36 |
| - } |
37 |
| - }; |
| 15 | + const calendarRef = useRef<CalendarWrapperHandle>(null); |
38 | 16 |
|
39 | 17 | return (
|
40 |
| - <div className="ffe-datepicker--wrapper"> |
41 |
| - {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} |
42 |
| - <div |
43 |
| - className="ffe-input-field ffe-dateinput" |
44 |
| - /* tabIndex={0}*/ |
45 |
| - > |
46 |
| - {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} |
47 |
| - {/* <span |
48 |
| - className="ffe-dateinput__field" |
49 |
| - tabIndex={0} |
50 |
| - onKeyDown={handleDayChange} |
51 |
| - > |
52 |
| - {typeof day === 'number' |
53 |
| - ? `${day < 10 ? '0' : ''}${day}` |
54 |
| - : 'dd'} |
55 |
| - </span>*/} |
| 18 | + <div className="ffe-datepicker" ref={containerRef}> |
| 19 | + <div className="ffe-input-field ffe-dateinput"> |
56 | 20 | <SpinnButton
|
57 |
| - onChange={history => { |
58 |
| - console.log(history); |
| 21 | + onChange={value => { |
| 22 | + setDay(value, () => |
| 23 | + monthRef.current?.focus({ preventScroll: true }), |
| 24 | + ); |
59 | 25 | }}
|
| 26 | + maxLength={2} |
60 | 27 | >
|
61 |
| - {typeof day === 'number' |
62 |
| - ? `${day < 10 ? '0' : ''}${day}` |
63 |
| - : 'dd'} |
| 28 | + {day ? <PadZero value={day} /> : 'dd'} |
64 | 29 | </SpinnButton>
|
65 | 30 | .
|
66 |
| - {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} |
67 |
| - <span |
68 |
| - className="ffe-dateinput__field" |
69 |
| - tabIndex={0} |
| 31 | + <SpinnButton |
70 | 32 | ref={monthRef}
|
71 |
| - onKeyDown={handleMonthChange} |
| 33 | + onChange={value => { |
| 34 | + setMonth(value, () => |
| 35 | + yearRef.current?.focus({ preventScroll: true }), |
| 36 | + ); |
| 37 | + }} |
| 38 | + maxLength={2} |
72 | 39 | >
|
73 |
| - {month ?? 'mm'} |
74 |
| - </span> |
| 40 | + {month ? <PadZero value={month} /> : 'mm'} |
| 41 | + </SpinnButton> |
75 | 42 | .
|
76 |
| - {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} |
77 |
| - <span |
78 |
| - className="ffe-dateinput__field" |
79 |
| - tabIndex={0} |
| 43 | + <SpinnButton |
80 | 44 | ref={yearRef}
|
81 |
| - onKeyDown={handleYearChange} |
| 45 | + onChange={value => { |
| 46 | + setYear(value); |
| 47 | + }} |
| 48 | + maxLength={4} |
82 | 49 | >
|
83 |
| - {year ?? 'yyyy'} |
84 |
| - </span> |
| 50 | + {year ? year : 'yyyy'} |
| 51 | + </SpinnButton> |
85 | 52 | </div>
|
86 |
| - {/* <Button |
87 |
| - onClick={calendarButtonClickHandler} |
88 |
| - value={value} |
89 |
| - locale={locale} |
90 |
| - ref={buttonRef} |
91 |
| - />*/} |
| 53 | + <Button |
| 54 | + onClick={() => { |
| 55 | + const rect = containerRef.current?.getBoundingClientRect(); |
| 56 | + calendarRef.current?.open({ |
| 57 | + top: rect?.bottom ?? 0, |
| 58 | + left: rect?.left ?? 0, |
| 59 | + }); |
| 60 | + }} |
| 61 | + value={'TODO'} |
| 62 | + locale={locale} |
| 63 | + /> |
| 64 | + <CalendarWrapper ref={calendarRef}> |
| 65 | + <Calendar /> |
| 66 | + </CalendarWrapper> |
92 | 67 | </div>
|
93 | 68 | );
|
94 | 69 | };
|
0 commit comments