// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import type { FocusEventHandler } from 'react'; import React, { forwardRef, useRef } from 'react'; import classNames from 'classnames'; import { refMerger } from '../util/refMerger'; import type { ConversationType } from '../state/ducks/conversations'; import type { LocalizerType } from '../types/Util'; import { Avatar, AvatarSize } from './Avatar'; type PropsType = { disabled?: boolean; i18n: LocalizerType; onBlur?: FocusEventHandler; onChangeValue: (newValue: string) => unknown; onClear: () => unknown; searchConversation?: ConversationType; value: string; }; export const LeftPaneSearchInput = forwardRef( ( { disabled, i18n, onBlur, onChangeValue, onClear, searchConversation, value, }, outerRef ) => { const inputRef = useRef(null); const emptyOrClear = searchConversation && value ? () => onChangeValue('') : onClear; const label = i18n(searchConversation ? 'searchIn' : 'search'); return (
{searchConversation ? ( // Clicking the non-X part of the pill should focus the input but have a normal // cursor. This effectively simulates `pointer-events: none` while still // letting us change the cursor. // eslint-disable-next-line max-len // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
{ inputRef.current?.focus(); }} >
) : (
)} { onChangeValue(event.currentTarget.value); }} onKeyDown={event => { 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') { emptyOrClear(); event.preventDefault(); event.stopPropagation(); } }} placeholder={label} ref={refMerger(inputRef, outerRef)} value={value} /> {value && (
); } );