Skip to content

Commit

Permalink
chore: DatePickerのInputSuffixIconを切り出す
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsushiM committed Jan 24, 2025
1 parent 366e55e commit 8d05c8b
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions packages/smarthr-ui/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(

const stringToDate = useMemo(
() =>
parseInt
? (str?: string | null) => (str ? parseInput(str) : undefined)
: (str?: string | null) => (str ? parseJpnDateString(str) : undefined),
parseInput
? (str?: string | null) => (str ? parseInput(str) : null)
: (str?: string | null) => (str ? parseJpnDateString(str) : null),
[parseInput],
)

Expand Down Expand Up @@ -168,12 +168,13 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(
}

const nextDate = isValid ? newDate : null
const currentValue = dateToString(nextDate)

inputRef.current.value = dateToString(nextDate)
inputRef.current.value = currentValue
setAlternativeFormat(dateToAlternativeFormat(nextDate))
setSelectedDate(nextDate)

return [nextDate, errors]
return [nextDate, currentValue, errors] as [Date | null, string, string[]]
},
[selectedDate, dateToString, dateToAlternativeFormat],
)
Expand All @@ -184,9 +185,9 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(
const result = baseUpdateDate(newDate)

if (result) {
const [nextDate, errors] = result
const [nextDate, currentValue, errors] = result

onChangeDate(nextDate, inputRef.current.value, { errors })
onChangeDate(nextDate, currentValue, { errors })
}
}
: baseUpdateDate,
Expand Down Expand Up @@ -320,7 +321,7 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(
[closeCalendar],
)
const onKeyPressInput = useCallback(
(e: React.KeyboardEvent) => {
(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
switchCalendarVisibility(!isCalendarShown)
updateDate(stringToDate(e.currentTarget.value))
Expand Down Expand Up @@ -362,14 +363,11 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(
onFocus={onFocusInput}
onBlur={handleBlur}
suffix={
<span className={styles.inputSuffixLayout}>
<span className={styles.inputSuffixWrapper}>
{showAlternative && (
<span className={styles.inputSuffixText}>{alternativeFormat}</span>
)}
<FaCalendarAltIcon color={caretIconColor} />
</span>
</span>
<InputSuffixIcon
alternativeFormat={showAlternative ? alternativeFormat : null}
caretIconColor={caretIconColor}
styles={styles}
/>
}
disabled={disabled}
error={error}
Expand All @@ -395,3 +393,16 @@ export const DatePicker = forwardRef<HTMLInputElement, Props & InputAttributes>(
)
},
)

const InputSuffixIcon = React.memo<{
styles: { inputSuffixLayout: string; inputSuffixWrapper: string; inputSuffixText: string }
alternativeFormat: null | ReactNode
caretIconColor: React.ComponentProps<typeof FaCalendarAltIcon>['color']
}>(({ styles, alternativeFormat, caretIconColor }) => (
<span className={styles.inputSuffixLayout}>
<span className={styles.inputSuffixWrapper}>
{alternativeFormat && <span className={styles.inputSuffixText}>{alternativeFormat}</span>}
<FaCalendarAltIcon color={caretIconColor} />
</span>
</span>
))

0 comments on commit 8d05c8b

Please sign in to comment.