import { useMergeRefs } from '@floating-ui/react'; import { EMPTY_CELL } from '@scrabble-solver/constants'; import { type Locale } from '@scrabble-solver/types'; import { type ChangeEventHandler, type FocusEventHandler, type FunctionComponent, type KeyboardEventHandler, type MouseEventHandler, type Ref, type TouchEventHandler, useCallback, useRef, } from 'react'; import { noop } from '@/lib/noop'; import { TilePure } from './TilePure'; interface Props { 'aria-label': string; autoFocus?: boolean; character?: string; className?: string; disabled?: boolean; highlighted?: boolean; inputRef?: Ref; isBlank?: boolean; isValid?: boolean; locale: Locale; placeholder?: string; points?: number; raised?: boolean; tabIndex?: number; onChange?: ChangeEventHandler; onFocus?: FocusEventHandler; onKeyDown?: KeyboardEventHandler; onMouseDown?: MouseEventHandler; onTouchStart?: TouchEventHandler; } export const Tile: FunctionComponent = ({ 'aria-label': ariaLabel, autoFocus, className, character = '', disabled, highlighted, inputRef, isBlank, isValid, locale, placeholder, points, raised, tabIndex, onChange, onFocus = noop, onKeyDown = noop, onMouseDown = noop, onTouchStart = noop, }) => { const ref = useRef(null); const mergedRef = useMergeRefs(inputRef ? [ref, inputRef] : [ref]); const isEmpty = !character || character === EMPTY_CELL; const canShowPoints = (!isEmpty || isBlank) && typeof points !== 'undefined'; const pointsFormatted = typeof points === 'number' ? points.toLocaleString(locale) : ''; const handleKeyDown: KeyboardEventHandler = useCallback( (event) => { ref.current?.select(); onKeyDown(event); }, [onKeyDown], ); return ( ); };