2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-05-11 00:50:43 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-02-14 17:57:11 +00:00
|
|
|
import type {
|
|
|
|
ChangeEvent,
|
|
|
|
FocusEventHandler,
|
|
|
|
KeyboardEvent,
|
|
|
|
ReactNode,
|
|
|
|
} from 'react';
|
2021-10-26 19:15:33 +00:00
|
|
|
import React, { forwardRef } from 'react';
|
2022-02-14 17:57:11 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import type { LocalizerType } from '../types/Util';
|
2021-05-11 00:50:43 +00:00
|
|
|
import { getClassNamesFor } from '../util/getClassNamesFor';
|
|
|
|
|
|
|
|
export type PropTypes = {
|
2022-02-14 17:57:11 +00:00
|
|
|
readonly children?: ReactNode;
|
2021-05-11 00:50:43 +00:00
|
|
|
readonly disabled?: boolean;
|
2022-02-14 17:57:11 +00:00
|
|
|
readonly label?: string;
|
|
|
|
readonly hasSearchIcon?: boolean;
|
|
|
|
readonly i18n: LocalizerType;
|
2021-05-11 00:50:43 +00:00
|
|
|
readonly moduleClassName?: string;
|
2022-02-14 17:57:11 +00:00
|
|
|
readonly onClear?: () => unknown;
|
|
|
|
readonly onBlur?: FocusEventHandler<HTMLInputElement>;
|
2021-05-11 00:50:43 +00:00
|
|
|
readonly onChange: (ev: ChangeEvent<HTMLInputElement>) => unknown;
|
|
|
|
readonly onKeyDown?: (ev: KeyboardEvent<HTMLInputElement>) => unknown;
|
|
|
|
readonly placeholder: string;
|
|
|
|
readonly value: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const BASE_CLASS_NAME = 'module-SearchInput';
|
|
|
|
|
|
|
|
export const SearchInput = forwardRef<HTMLInputElement, PropTypes>(
|
2022-11-18 00:45:19 +00:00
|
|
|
function SearchInputInner(
|
2021-05-11 00:50:43 +00:00
|
|
|
{
|
2022-02-14 17:57:11 +00:00
|
|
|
children,
|
2021-05-11 00:50:43 +00:00
|
|
|
disabled = false,
|
2022-02-14 17:57:11 +00:00
|
|
|
hasSearchIcon = true,
|
|
|
|
i18n,
|
|
|
|
label,
|
2021-05-11 00:50:43 +00:00
|
|
|
moduleClassName,
|
2022-02-14 17:57:11 +00:00
|
|
|
onClear,
|
|
|
|
onBlur,
|
2021-05-11 00:50:43 +00:00
|
|
|
onChange,
|
|
|
|
onKeyDown,
|
|
|
|
placeholder,
|
|
|
|
value,
|
|
|
|
},
|
|
|
|
ref
|
2022-11-18 00:45:19 +00:00
|
|
|
) {
|
2021-05-11 00:50:43 +00:00
|
|
|
const getClassName = getClassNamesFor(BASE_CLASS_NAME, moduleClassName);
|
|
|
|
return (
|
2023-05-09 15:52:03 +00:00
|
|
|
<div className={getClassName('__container')} data-supertab>
|
2022-02-14 17:57:11 +00:00
|
|
|
{hasSearchIcon && <i className={getClassName('__icon')} />}
|
|
|
|
{children}
|
2021-05-11 00:50:43 +00:00
|
|
|
<input
|
2023-03-30 00:03:25 +00:00
|
|
|
aria-label={label || i18n('icu:search')}
|
2022-02-14 17:57:11 +00:00
|
|
|
className={classNames(
|
|
|
|
getClassName('__input'),
|
|
|
|
value && getClassName('__input--with-text'),
|
|
|
|
children && getClassName('__input--with-children')
|
|
|
|
)}
|
2021-05-11 00:50:43 +00:00
|
|
|
dir="auto"
|
|
|
|
disabled={disabled}
|
2022-02-14 17:57:11 +00:00
|
|
|
onBlur={onBlur}
|
2021-05-11 00:50:43 +00:00
|
|
|
onChange={onChange}
|
2022-02-14 17:57:11 +00:00
|
|
|
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' && onClear) {
|
|
|
|
onClear();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
onKeyDown?.(event);
|
|
|
|
}}
|
2021-05-11 00:50:43 +00:00
|
|
|
placeholder={placeholder}
|
|
|
|
ref={ref}
|
|
|
|
type="text"
|
|
|
|
value={value}
|
|
|
|
/>
|
2022-02-14 17:57:11 +00:00
|
|
|
{value && onClear && (
|
|
|
|
<button
|
2023-03-30 00:03:25 +00:00
|
|
|
aria-label={i18n('icu:cancel')}
|
2022-02-14 17:57:11 +00:00
|
|
|
className={getClassName('__cancel')}
|
|
|
|
onClick={onClear}
|
|
|
|
tabIndex={-1}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
)}
|
2021-05-11 00:50:43 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|