diff --git a/packages/smarthr-ui/src/components/DatePicker/DatePicker.stories.tsx b/packages/smarthr-ui/src/components/DatePicker/DatePicker.stories.tsx index 81ce166b7a..0bb624b7db 100644 --- a/packages/smarthr-ui/src/components/DatePicker/DatePicker.stories.tsx +++ b/packages/smarthr-ui/src/components/DatePicker/DatePicker.stories.tsx @@ -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')} /> diff --git a/packages/smarthr-ui/src/components/DatePicker/DatePicker.tsx b/packages/smarthr-ui/src/components/DatePicker/DatePicker.tsx index f2914bf99d..6dee24d1ab 100644 --- a/packages/smarthr-ui/src/components/DatePicker/DatePicker.tsx +++ b/packages/smarthr-ui/src/components/DatePicker/DatePicker.tsx @@ -57,7 +57,6 @@ type OmitInputAttributes = | 'onChange' | 'onKeyPress' | 'onFocus' - | 'onBlur' | 'aria-expanded' | 'aria-controls' | 'aria-haspopup' @@ -90,6 +89,7 @@ export const DatePicker = forwardRef( formatDate, showAlternative, onChangeDate, + onBlur, ...inputAttrs }, ref, @@ -266,6 +266,22 @@ export const DatePicker = forwardRef( }, [isInputFocused, switchCalendarVisibility], ) + const handleBlur = useCallback>( + (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(() => { @@ -322,15 +338,7 @@ export const DatePicker = forwardRef( setIsInputFocused(true) switchCalendarVisibility(true) }} - onBlur={({ target: { value: inputString } }) => { - setIsInputFocused(false) - if (inputString === '') { - updateDate(null) - return - } - const newDate = stringToDate(inputString) - updateDate(newDate) - }} + onBlur={handleBlur} suffix={