|
1 |
| -import React, { ComponentProps, forwardRef, useMemo } from 'react' |
| 1 | +import React, { ComponentProps, forwardRef, useId, useMemo } from 'react' |
| 2 | +import { tv } from 'tailwind-variants' |
2 | 3 |
|
3 |
| -import { FaSearchIcon } from '../../Icon' |
| 4 | +import { UnstyledButton } from '../../Button' |
| 5 | +import { FaCircleXmarkIcon, FaMagnifyingGlassIcon } from '../../Icon' |
| 6 | +import { VisuallyHiddenText } from '../../VisuallyHiddenText' |
4 | 7 | import { InputWithTooltip } from '../InputWithTooltip'
|
5 | 8 |
|
6 | 9 | import type { DecoratorsType } from '../../../types'
|
7 | 10 |
|
8 | 11 | type Props = Omit<ComponentProps<typeof InputWithTooltip>, 'tooltipMessage' | 'prefix'> & {
|
9 | 12 | /** 入力欄の説明を紐付けるツールチップに表示するメッセージ */
|
10 | 13 | tooltipMessage: React.ReactNode
|
11 |
| - decorators?: DecoratorsType<'iconAlt'> |
| 14 | + decorators?: DecoratorsType<'iconAlt' | 'clearButtonAlt'> |
| 15 | + onClickClear?: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void |
12 | 16 | }
|
13 | 17 |
|
14 | 18 | const ICON_ALT = '検索'
|
| 19 | +const CLEAR_BUTTON_ALT = '削除' |
15 | 20 |
|
16 |
| -export const SearchInput = forwardRef<HTMLInputElement, Props>(({ decorators, ...props }, ref) => { |
17 |
| - const iconAlt = useMemo(() => decorators?.iconAlt?.(ICON_ALT) || ICON_ALT, [decorators]) |
18 |
| - |
19 |
| - return ( |
20 |
| - <label> |
21 |
| - <InputWithTooltip |
22 |
| - {...props} |
23 |
| - ref={ref} |
24 |
| - prefix={<FaSearchIcon alt={iconAlt} color="TEXT_GREY" />} |
25 |
| - /> |
26 |
| - </label> |
27 |
| - ) |
| 21 | +const searchInput = tv({ |
| 22 | + slots: { |
| 23 | + clearButton: [ |
| 24 | + 'smarthr-ui-SearchInput-clearButton', |
| 25 | + 'shr-group/clearButton', |
| 26 | + 'shr-cursor-pointer', |
| 27 | + 'focus-visible:shr-shadow-none', |
| 28 | + ], |
| 29 | + clearButtonIcon: [ |
| 30 | + 'shr-block', |
| 31 | + 'group-focus-visible/clearButton:shr-focus-indicator group-focus-visible/clearButton:shr-rounded-full', |
| 32 | + ], |
| 33 | + }, |
28 | 34 | })
|
| 35 | + |
| 36 | +export const SearchInput = forwardRef<HTMLInputElement, Props>( |
| 37 | + ({ decorators, onClickClear, ...props }, ref) => { |
| 38 | + const { clearButton, clearButtonIcon } = searchInput() |
| 39 | + const { clearButtonStyle, clearButtonIconStyle } = useMemo( |
| 40 | + () => ({ |
| 41 | + clearButtonStyle: clearButton(), |
| 42 | + clearButtonIconStyle: clearButtonIcon(), |
| 43 | + }), |
| 44 | + [clearButton, clearButtonIcon], |
| 45 | + ) |
| 46 | + |
| 47 | + const iconAlt = useMemo(() => decorators?.iconAlt?.(ICON_ALT) || ICON_ALT, [decorators]) |
| 48 | + const clearButtonAlt = useMemo( |
| 49 | + () => decorators?.clearButtonAlt?.(CLEAR_BUTTON_ALT) || CLEAR_BUTTON_ALT, |
| 50 | + [decorators], |
| 51 | + ) |
| 52 | + |
| 53 | + const defaultInptuId = useId() |
| 54 | + const inputId = props.id || defaultInptuId |
| 55 | + |
| 56 | + return ( |
| 57 | + <span> |
| 58 | + <label htmlFor={inputId}> |
| 59 | + <VisuallyHiddenText>{iconAlt}</VisuallyHiddenText> |
| 60 | + </label> |
| 61 | + <InputWithTooltip |
| 62 | + {...props} |
| 63 | + id={inputId} |
| 64 | + ref={ref} |
| 65 | + prefix={<FaMagnifyingGlassIcon color="TEXT_GREY" />} |
| 66 | + suffix={ |
| 67 | + onClickClear && |
| 68 | + props.value && ( |
| 69 | + <UnstyledButton onClick={(e) => onClickClear(e)} className={clearButtonStyle}> |
| 70 | + <FaCircleXmarkIcon |
| 71 | + color="TEXT_BLACK" |
| 72 | + alt={clearButtonAlt} |
| 73 | + className={clearButtonIconStyle} |
| 74 | + /> |
| 75 | + </UnstyledButton> |
| 76 | + ) |
| 77 | + } |
| 78 | + /> |
| 79 | + </span> |
| 80 | + ) |
| 81 | + }, |
| 82 | +) |
0 commit comments