// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import type { ChangeEvent, FocusEventHandler, KeyboardEvent, ReactNode, } from 'react'; import React, { forwardRef } from 'react'; import classNames from 'classnames'; import type { LocalizerType } from '../types/Util'; import { getClassNamesFor } from '../util/getClassNamesFor'; export type PropTypes = { readonly children?: ReactNode; readonly disabled?: boolean; readonly label?: string; readonly hasSearchIcon?: boolean; readonly i18n: LocalizerType; readonly moduleClassName?: string; readonly onClear?: () => unknown; readonly onBlur?: FocusEventHandler; readonly onChange: (ev: ChangeEvent) => unknown; readonly onKeyDown?: (ev: KeyboardEvent) => unknown; readonly placeholder: string; readonly value: string; }; const BASE_CLASS_NAME = 'module-SearchInput'; export const SearchInput = forwardRef( function SearchInputInner( { children, disabled = false, hasSearchIcon = true, i18n, label, moduleClassName, onClear, onBlur, onChange, onKeyDown, placeholder, value, }, ref ) { const getClassName = getClassNamesFor(BASE_CLASS_NAME, moduleClassName); return (
{hasSearchIcon && } {children} { const { ctrlKey, key } = event; // On Linux, this key combo selects all text. if (window.platform === 'linux' && ctrlKey && key === '/') { event.preventDefault(); event.stopPropagation(); } else if (key === 'Escape' && onClear) { onClear(); event.preventDefault(); event.stopPropagation(); } onKeyDown?.(event); }} placeholder={placeholder} ref={ref} type="text" value={value} /> {value && onClear && (
); } );