Skip to content

Commit

Permalink
fix(fields): focus next date part input when already filled in DatePi…
Browse files Browse the repository at this point in the history
…cker & DateRangePicker
  • Loading branch information
ivangabriele committed Dec 21, 2022
1 parent 226941a commit 38219de
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
9 changes: 8 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [Development](#development)
- [First Setup](#first-setup)
- [Run](#run)
- [Conventions](#conventions)
- [Release](#release)
- [Versionning](#versionning)
Expand Down Expand Up @@ -35,7 +36,13 @@ yarn
yarn setup
```

4. [Setup your IDE](https://yarnpkg.com/getting-started/editor-sdks).
### Run

```sh
yarn start
```

1. [Setup your IDE](https://yarnpkg.com/getting-started/editor-sdks).

### Conventions

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"build:copy": "sh ./scripts/build/copyPublicFiles.sh",
"build:dev": "rm -Rf ./dist && rollup -c",
"build:package": "node ./scripts/build/generatePackageJson.js",
"dev": "start-storybook -p 6006",
"dev": "start-storybook --no-open -p 6006",
"start": "start-storybook -p 6006",
"icons": "yarn icons:clean && yarn icons:generate && yarn icons:lint",
"icons:clean": "rm -Rf ./src/icons && mkdir ./src/icons",
"icons:generate": "node --experimental-json-modules ./scripts/icons/generateIcons.mjs",
Expand Down
10 changes: 6 additions & 4 deletions src/fields/DatePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export function DatePicker({
timeInputRef.current.focus()
}, [withTime])

const handleDateInputFilled = useCallback(
(nextDateTuple: DateTuple) => {
const handleDateInputChange = useCallback(
(nextDateTuple: DateTuple, isFilled: boolean) => {
selectedDateTupleRef.current = nextDateTuple

// If there is no time input or a time has already been selected,
Expand All @@ -125,7 +125,9 @@ export function DatePicker({
submit()
}

handleDateInputNext()
if (isFilled) {
handleDateInputNext()
}
},
[handleDateInputNext, submit, withTime]
)
Expand Down Expand Up @@ -207,7 +209,7 @@ export function DatePicker({
isCompact={isCompact}
isForcedFocused={isCalendarPickerOpenRef.current}
isLight={isLight}
onChange={handleDateInputFilled}
onChange={handleDateInputChange}
onClick={openCalendarPicker}
onNext={handleDateInputNext}
/>
Expand Down
13 changes: 6 additions & 7 deletions src/fields/DateRangePicker/DateInput.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react'
import { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react'
import styled from 'styled-components'

import { Calendar } from '../../icons'
import { getUtcizedDayjs } from '../../utils/getUtcizedDayjs'
import { NumberInput } from './NumberInput'
import { formatNumberAsDoubleDigit } from './utils'

Expand All @@ -19,7 +18,7 @@ export type DateInputProps = Pick<NumberInputProps, 'onBack' | 'onPrevious' | 'o
isLight: boolean
isStartDate?: boolean
/** Called each time the date input is changed to a new valid value. */
onChange: (nextDateTuple: DateTuple) => Promisable<void>
onChange: (nextDateTuple: DateTuple, isFilled: boolean) => Promisable<void>
onClick: () => Promisable<void>
}
function DateInputWithRef(
Expand Down Expand Up @@ -58,8 +57,6 @@ function DateInputWithRef(
}
}))

const currentUtcYear = useMemo(() => getUtcizedDayjs().year(), [])

const handleBlur = useCallback(() => {
setIsFocused(false)
}, [])
Expand All @@ -75,6 +72,8 @@ function DateInputWithRef(
const submit = useCallback(() => {
setHasValidationError(false)

const isFilled = window.document.activeElement === yearInputRef.current

switch (window.document.activeElement) {
case dayInputRef.current:
monthInputRef.current.focus()
Expand Down Expand Up @@ -110,7 +109,7 @@ function DateInputWithRef(
formatNumberAsDoubleDigit(dayInputRef.current.value)
]

onChange(nextDateTuple)
onChange(nextDateTuple, isFilled)
}, [onChange])

return (
Expand Down Expand Up @@ -161,7 +160,7 @@ function DateInputWithRef(
ref={yearInputRef}
aria-label={`Année${isStartDate ? ' de début' : ''}${isEndDate ? ' de fin' : ''}`}
defaultValue={defaultValue && defaultValue[0]}
max={currentUtcYear}
max={2030}
min={2020}
onBack={() => monthInputRef.current.focus()}
onBlur={handleBlur}
Expand Down
20 changes: 14 additions & 6 deletions src/fields/DateRangePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export function DateRangePicker({
endDateInputRef.current.focus()
}, [withTime])

const handleDateInputFilled = useCallback(
(position: DateRangePosition, nextDateTuple: DateTuple) => {
const handleDateInputChange = useCallback(
(position: DateRangePosition, nextDateTuple: DateTuple, isFilled: boolean) => {
if (position === DateRangePosition.START) {
selectedStartDateTupleRef.current = nextDateTuple

Expand All @@ -160,7 +160,9 @@ export function DateRangePicker({
submit()
}

handleStartDateInputNext()
if (isFilled) {
handleStartDateInputNext()
}
} else {
selectedEndDateTupleRef.current = nextDateTuple

Expand All @@ -175,7 +177,9 @@ export function DateRangePicker({
submit()
}

handleEndDateInputNext()
if (isFilled) {
handleEndDateInputNext()
}
}
},
[handleEndDateInputNext, handleStartDateInputNext, submit, withTime]
Expand Down Expand Up @@ -290,7 +294,9 @@ export function DateRangePicker({
isForcedFocused={isRangeCalendarPickerOpenRef.current}
isLight={isLight}
isStartDate
onChange={nextDateTuple => handleDateInputFilled(DateRangePosition.START, nextDateTuple)}
onChange={(nextDateTuple, isFilled) =>
handleDateInputChange(DateRangePosition.START, nextDateTuple, isFilled)
}
onClick={openRangeCalendarPicker}
onNext={handleStartDateInputNext}
/>
Expand Down Expand Up @@ -323,7 +329,9 @@ export function DateRangePicker({
isForcedFocused={isRangeCalendarPickerOpenRef.current}
isLight={isLight}
onBack={handleEndDateInputPrevious}
onChange={nextDateTuple => handleDateInputFilled(DateRangePosition.END, nextDateTuple)}
onChange={(nextDateTuple, isFilled) =>
handleDateInputChange(DateRangePosition.END, nextDateTuple, isFilled)
}
onClick={openRangeCalendarPicker}
onNext={handleEndDateInputNext}
onPrevious={handleEndDateInputPrevious}
Expand Down

0 comments on commit 38219de

Please sign in to comment.