-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter Hellstrand
committed
Nov 25, 2024
1 parent
0dc6086
commit 507e696
Showing
14 changed files
with
520 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { useImperativeHandle, useRef, useState } from 'react'; | ||
|
||
export interface CalendarWrapperProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export type CalendarWrapperHandle = { | ||
readonly open: ({ left, top }: { left: number; top: number }) => void; | ||
readonly close: () => void; | ||
}; | ||
|
||
export const CalendarWrapper = React.forwardRef< | ||
CalendarWrapperHandle, | ||
CalendarWrapperProps | ||
>(({ children }, ref) => { | ||
const dialogRef = useRef<HTMLDialogElement>(null); | ||
const [style, setStyle] = useState<React.CSSProperties>(); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
open: ({ top, left }) => { | ||
setStyle({ | ||
'--top': `${top}px`, | ||
'--left': `${left}px`, | ||
} as React.CSSProperties); | ||
dialogRef.current?.showModal(); | ||
}, | ||
close: () => { | ||
dialogRef.current?.close(); | ||
}, | ||
})); | ||
|
||
return ( | ||
<dialog | ||
style={style} | ||
className="ffe-datepicker__dialog" | ||
ref={dialogRef} | ||
onClick={event => { | ||
const target = event.target as HTMLDialogElement; | ||
if (target.nodeName === 'DIALOG') { | ||
target.close(); | ||
} | ||
}} | ||
> | ||
{children} | ||
</dialog> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 46 additions & 71 deletions
117
packages/ffe-datepicker-react/src/v2/DatePickerComp.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,69 @@ | ||
import React, { useContext, useRef } from 'react'; | ||
import React, { useContext, useRef, useState } from 'react'; | ||
import { DatePickerContext } from './DatePickerContext'; | ||
import { SpinnButton } from './SpinnButton'; | ||
import { PadZero } from './PadZero'; | ||
import { Button } from '../button'; | ||
import { Calendar } from './calendar'; | ||
import { CalendarWrapper, CalendarWrapperHandle } from './CalenderWrapper'; | ||
|
||
export const DatePickerComp: React.FC = () => { | ||
/* | ||
const dayRef = useRef<HTMLSpanElement>(null); | ||
*/ | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const monthRef = useRef<HTMLSpanElement>(null); | ||
const yearRef = useRef<HTMLSpanElement>(null); | ||
const { day, setDay, year, setYear, month, setMonth } = | ||
const { day, setDay, year, setYear, month, setMonth, locale } = | ||
useContext(DatePickerContext); | ||
|
||
const handleDayChange = (evt: React.KeyboardEvent<HTMLSpanElement>) => { | ||
evt.stopPropagation(); | ||
if (/\d/.test(evt.key)) { | ||
setDay(parseInt(evt.key), () => | ||
monthRef.current?.focus({ preventScroll: true }), | ||
); | ||
} | ||
}; | ||
|
||
const handleMonthChange = (evt: React.KeyboardEvent<HTMLSpanElement>) => { | ||
evt.stopPropagation(); | ||
if (/\d/.test(evt.key)) { | ||
setMonth(parseInt(evt.key), () => | ||
yearRef.current?.focus({ preventScroll: true }), | ||
); | ||
} | ||
}; | ||
|
||
const handleYearChange = (evt: React.KeyboardEvent<HTMLSpanElement>) => { | ||
evt.stopPropagation(); | ||
if (/\d/.test(evt.key)) { | ||
setYear(parseInt(evt.key)); | ||
} | ||
}; | ||
const calendarRef = useRef<CalendarWrapperHandle>(null); | ||
|
||
return ( | ||
<div className="ffe-datepicker--wrapper"> | ||
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} | ||
<div | ||
className="ffe-input-field ffe-dateinput" | ||
/* tabIndex={0}*/ | ||
> | ||
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} | ||
{/* <span | ||
className="ffe-dateinput__field" | ||
tabIndex={0} | ||
onKeyDown={handleDayChange} | ||
> | ||
{typeof day === 'number' | ||
? `${day < 10 ? '0' : ''}${day}` | ||
: 'dd'} | ||
</span>*/} | ||
<div className="ffe-datepicker" ref={containerRef}> | ||
<div className="ffe-input-field ffe-dateinput"> | ||
<SpinnButton | ||
onChange={history => { | ||
console.log(history); | ||
onChange={value => { | ||
setDay(value, () => | ||
monthRef.current?.focus({ preventScroll: true }), | ||
); | ||
}} | ||
maxLength={2} | ||
> | ||
{typeof day === 'number' | ||
? `${day < 10 ? '0' : ''}${day}` | ||
: 'dd'} | ||
{day ? <PadZero value={day} /> : 'dd'} | ||
</SpinnButton> | ||
. | ||
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} | ||
<span | ||
className="ffe-dateinput__field" | ||
tabIndex={0} | ||
<SpinnButton | ||
ref={monthRef} | ||
onKeyDown={handleMonthChange} | ||
onChange={value => { | ||
setMonth(value, () => | ||
yearRef.current?.focus({ preventScroll: true }), | ||
); | ||
}} | ||
maxLength={2} | ||
> | ||
{month ?? 'mm'} | ||
</span> | ||
{month ? <PadZero value={month} /> : 'mm'} | ||
</SpinnButton> | ||
. | ||
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */} | ||
<span | ||
className="ffe-dateinput__field" | ||
tabIndex={0} | ||
<SpinnButton | ||
ref={yearRef} | ||
onKeyDown={handleYearChange} | ||
onChange={value => { | ||
setYear(value); | ||
}} | ||
maxLength={4} | ||
> | ||
{year ?? 'yyyy'} | ||
</span> | ||
{year ? year : 'yyyy'} | ||
</SpinnButton> | ||
</div> | ||
{/* <Button | ||
onClick={calendarButtonClickHandler} | ||
value={value} | ||
locale={locale} | ||
ref={buttonRef} | ||
/>*/} | ||
<Button | ||
onClick={() => { | ||
const rect = containerRef.current?.getBoundingClientRect(); | ||
calendarRef.current?.open({ | ||
top: rect?.bottom ?? 0, | ||
left: rect?.left ?? 0, | ||
}); | ||
}} | ||
value={'TODO'} | ||
locale={locale} | ||
/> | ||
<CalendarWrapper ref={calendarRef}> | ||
<Calendar /> | ||
</CalendarWrapper> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
|
||
interface Props { | ||
value: number; | ||
} | ||
|
||
export const PadZero: React.FC<Props> = ({ value }) => { | ||
if (value < 10) { | ||
return `0${value}`; | ||
} | ||
return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.