|
| 1 | +import React, { useContext, useRef } from 'react'; |
| 2 | +import { DatePickerContext } from './DatePickerContext'; |
| 3 | +import { SpinnButton } from './SpinnButton'; |
| 4 | + |
| 5 | +export const DatePickerComp: React.FC = () => { |
| 6 | + /* |
| 7 | + const dayRef = useRef<HTMLSpanElement>(null); |
| 8 | +*/ |
| 9 | + const monthRef = useRef<HTMLSpanElement>(null); |
| 10 | + const yearRef = useRef<HTMLSpanElement>(null); |
| 11 | + const { day, setDay, year, setYear, month, setMonth } = |
| 12 | + 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 | + }; |
| 38 | + |
| 39 | + 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>*/} |
| 56 | + <SpinnButton |
| 57 | + onChange={history => { |
| 58 | + console.log(history); |
| 59 | + }} |
| 60 | + > |
| 61 | + {typeof day === 'number' |
| 62 | + ? `${day < 10 ? '0' : ''}${day}` |
| 63 | + : 'dd'} |
| 64 | + </SpinnButton> |
| 65 | + . |
| 66 | + {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} |
| 67 | + <span |
| 68 | + className="ffe-dateinput__field" |
| 69 | + tabIndex={0} |
| 70 | + ref={monthRef} |
| 71 | + onKeyDown={handleMonthChange} |
| 72 | + > |
| 73 | + {month ?? 'mm'} |
| 74 | + </span> |
| 75 | + . |
| 76 | + {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} |
| 77 | + <span |
| 78 | + className="ffe-dateinput__field" |
| 79 | + tabIndex={0} |
| 80 | + ref={yearRef} |
| 81 | + onKeyDown={handleYearChange} |
| 82 | + > |
| 83 | + {year ?? 'yyyy'} |
| 84 | + </span> |
| 85 | + </div> |
| 86 | + {/* <Button |
| 87 | + onClick={calendarButtonClickHandler} |
| 88 | + value={value} |
| 89 | + locale={locale} |
| 90 | + ref={buttonRef} |
| 91 | + />*/} |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
0 commit comments