Skip to content

Commit

Permalink
feat: DatePicker に onBlur を渡せるように修正 (#4770)
Browse files Browse the repository at this point in the history
* docs: DatePciekr の Story に onBlur を追加

* feat: DatePicker に onBlur を渡せるように修正
  • Loading branch information
uknmr authored Jul 10, 2024
1 parent 51bdca1 commit 0422440
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const All: StoryFn = () => {
from={new Date(1901, 0, 1)}
to={new Date(2100, 11, 30)}
onChangeDate={action('change')}
onBlur={action('blur')}
/>
</FormControl>
<FormControl title="Custom format (ex. Date.toDateString)">
Expand Down
28 changes: 18 additions & 10 deletions packages/smarthr-ui/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type OmitInputAttributes =
| 'onChange'
| 'onKeyPress'
| 'onFocus'
| 'onBlur'
| 'aria-expanded'
| 'aria-controls'
| 'aria-haspopup'
Expand Down Expand Up @@ -90,6 +89,7 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(
formatDate,
showAlternative,
onChangeDate,
onBlur,
...inputAttrs
},
ref,
Expand Down Expand Up @@ -266,6 +266,22 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(
},
[isInputFocused, switchCalendarVisibility],
)
const handleBlur = useCallback<React.FocusEventHandler<HTMLInputElement>>(
(e) => {
const {
target: { value: inputString },
} = e
setIsInputFocused(false)
if (inputString === '') {
updateDate(null)
} else {
const newDate = stringToDate(inputString)
updateDate(newDate)
}
onBlur && onBlur(e)
},
[onBlur, stringToDate, updateDate],
)
useGlobalKeyDown(handleKeyDown)

const caretIconColor = useMemo(() => {
Expand Down Expand Up @@ -322,15 +338,7 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(
setIsInputFocused(true)
switchCalendarVisibility(true)
}}
onBlur={({ target: { value: inputString } }) => {
setIsInputFocused(false)
if (inputString === '') {
updateDate(null)
return
}
const newDate = stringToDate(inputString)
updateDate(newDate)
}}
onBlur={handleBlur}
suffix={
<span className={inputSuffixLayoutStyle}>
<span className={inputSuffixWrapperStyle}>
Expand Down

0 comments on commit 0422440

Please sign in to comment.