Skip to content

Onboarding #851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable react/jsx-no-bind */
import { FC } from 'react'
import { FC, forwardRef, KeyboardEvent, useRef, useState } from 'react'
import { getMonth, getYear } from 'date-fns'
import { range } from 'lodash'
import DatePicker from 'react-datepicker'
import DatePicker, { ReactDatePicker } from 'react-datepicker'
import classNames from 'classnames'
import 'react-datepicker/dist/react-datepicker.css'

Expand All @@ -22,10 +22,10 @@ interface InputDatePickerProps {
readonly hideInlineErrors?: boolean
readonly hint?: string
readonly label: string | JSX.Element
readonly maxDate?: Date | null | undefined;
readonly maxTime?: Date | undefined;
readonly minDate?: Date | null | undefined;
readonly minTime?: Date | undefined;
readonly maxDate?: Date | null | undefined
readonly maxTime?: Date | undefined
readonly minDate?: Date | null | undefined
readonly minTime?: Date | undefined
readonly placeholder?: string
readonly showMonthPicker?: boolean
readonly showYearPicker?: boolean
Expand All @@ -48,7 +48,34 @@ const months: string[] = [
'December',
]

const CustomInput = forwardRef((props: any, ref) => {
const { stateHasFocus, datePickerRef, ...remaining }: any = props

function handleKeyDown(event: KeyboardEvent<HTMLButtonElement> | undefined): void {
if (event?.key === 'Enter') {
event?.stopPropagation()
event?.preventDefault()
datePickerRef.current?.setOpen(!datePickerRef.current?.isCalendarOpen(), true)
}
}

return (
<input
type='text'
ref={ref}
{...remaining}
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={stateHasFocus}
onKeyDown={handleKeyDown}
/>
)
})

const InputDatePicker: FC<InputDatePickerProps> = (props: InputDatePickerProps) => {
const datePickerRef = useRef<ReactDatePicker<never, undefined>>(null)

const [stateHasFocus, setStateHasFocus] = useState(false)

function renderCustomHeader({
date,
changeYear,
Expand Down Expand Up @@ -112,15 +139,34 @@ const InputDatePicker: FC<InputDatePickerProps> = (props: InputDatePickerProps)
className={classNames(props.className, styles.container)}
>
<DatePicker
ref={datePickerRef}
customInput={<CustomInput stateHasFocus={stateHasFocus} datePickerRef={datePickerRef} />}
renderCustomHeader={renderCustomHeader}
selected={props.date}
disabled={props.disabled}
onChange={(
date: Date | null,
event: React.SyntheticEvent<any> | undefined,
) => {
event?.stopPropagation()
event?.preventDefault()
props.onChange?.(date)

// re-focus on date input field after select date
const calendarPortal = document.getElementById('react-date-portal')
if (calendarPortal) {
calendarPortal.style.display = 'none'
}

setTimeout(() => {
datePickerRef.current?.setFocus()
setTimeout(() => {
datePickerRef.current?.setOpen(false, true)
if (calendarPortal) {
calendarPortal.style.display = ''
}
})
})
}}
placeholderText={props.placeholder || 'Select a date'}
className={styles.datePickerWrapper}
Expand All @@ -132,6 +178,8 @@ const InputDatePicker: FC<InputDatePickerProps> = (props: InputDatePickerProps)
dateFormat={props.dateFormat}
popperPlacement='bottom'
portalId='react-date-portal'
onFocus={() => setStateHasFocus(true)}
onBlur={() => setStateHasFocus(false)}
/>
</InputWrapper>
)
Expand Down