2022-01-10 14:17:52 -08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import { List as ImmutableList } from 'immutable';
|
2020-03-27 13:59:38 -07:00
|
|
|
import React from 'react';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-06-04 16:35:04 -07:00
|
|
|
import AutosuggestEmoji, { Emoji } from 'soapbox/components/autosuggest_emoji';
|
2021-11-01 11:53:30 -07:00
|
|
|
import Icon from 'soapbox/components/icon';
|
2022-06-04 16:21:16 -07:00
|
|
|
import AutosuggestAccount from 'soapbox/features/compose/components/autosuggest_account';
|
|
|
|
import { isRtl } from 'soapbox/rtl';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
import type { Menu, MenuItem } from 'soapbox/components/dropdown_menu';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
type CursorMatch = [
|
|
|
|
tokenStart: number | null,
|
|
|
|
token: string | null,
|
|
|
|
];
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-04 16:36:34 -07:00
|
|
|
export type AutoSuggestion = string | Emoji;
|
2022-06-04 16:35:04 -07:00
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
const textAtCursorMatchesToken = (str: string, caretPosition: number, searchTokens: string[]): CursorMatch => {
|
|
|
|
let word: string;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
const left: number = str.slice(0, caretPosition).search(/\S+$/);
|
|
|
|
const right: number = str.slice(caretPosition).search(/\s/);
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
if (right < 0) {
|
|
|
|
word = str.slice(left);
|
|
|
|
} else {
|
|
|
|
word = str.slice(left, right + caretPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!word || word.trim().length < 3 || searchTokens.indexOf(word[0]) === -1) {
|
|
|
|
return [null, null];
|
|
|
|
}
|
|
|
|
|
|
|
|
word = word.trim().toLowerCase();
|
|
|
|
|
|
|
|
if (word.length > 0) {
|
|
|
|
return [left + 1, word];
|
|
|
|
} else {
|
|
|
|
return [null, null];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
interface IAutosuggestInput extends Pick<React.HTMLAttributes<HTMLInputElement>, 'onChange' | 'onKeyUp' | 'onKeyDown'> {
|
|
|
|
value: string,
|
|
|
|
suggestions: ImmutableList<any>,
|
|
|
|
disabled?: boolean,
|
|
|
|
placeholder?: string,
|
2022-06-04 16:36:34 -07:00
|
|
|
onSuggestionSelected: (tokenStart: number, lastToken: string | null, suggestion: AutoSuggestion) => void,
|
2022-06-04 16:21:16 -07:00
|
|
|
onSuggestionsClearRequested: () => void,
|
|
|
|
onSuggestionsFetchRequested: (token: string) => void,
|
|
|
|
autoFocus: boolean,
|
|
|
|
autoSelect: boolean,
|
|
|
|
className?: string,
|
|
|
|
id?: string,
|
|
|
|
searchTokens: string[],
|
|
|
|
maxLength?: number,
|
|
|
|
menu?: Menu,
|
|
|
|
resultsPosition: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class AutosuggestInput extends ImmutablePureComponent<IAutosuggestInput> {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
static defaultProps = {
|
2021-10-14 10:35:05 -07:00
|
|
|
autoFocus: false,
|
2021-11-01 10:25:18 -07:00
|
|
|
autoSelect: true,
|
2020-03-27 13:59:38 -07:00
|
|
|
searchTokens: ImmutableList(['@', ':', '#']),
|
2022-06-04 15:23:25 -07:00
|
|
|
resultsPosition: 'below',
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
|
2021-11-01 10:25:18 -07:00
|
|
|
getFirstIndex = () => {
|
|
|
|
return this.props.autoSelect ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
state = {
|
|
|
|
suggestionsHidden: true,
|
|
|
|
focused: false,
|
2021-11-01 10:25:18 -07:00
|
|
|
selectedSuggestion: this.getFirstIndex(),
|
2020-03-27 13:59:38 -07:00
|
|
|
lastToken: null,
|
|
|
|
tokenStart: 0,
|
|
|
|
};
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
input: HTMLInputElement | null = null;
|
|
|
|
|
|
|
|
onChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
|
|
const [tokenStart, token] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart || 0, this.props.searchTokens);
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
if (token !== null && this.state.lastToken !== token) {
|
|
|
|
this.setState({ lastToken: token, selectedSuggestion: 0, tokenStart });
|
|
|
|
this.props.onSuggestionsFetchRequested(token);
|
|
|
|
} else if (token === null) {
|
|
|
|
this.setState({ lastToken: null });
|
|
|
|
this.props.onSuggestionsClearRequested();
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
if (this.props.onChange) {
|
|
|
|
this.props.onChange(e);
|
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
onKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
|
2021-11-01 11:45:17 -07:00
|
|
|
const { suggestions, menu, disabled } = this.props;
|
2020-03-27 13:59:38 -07:00
|
|
|
const { selectedSuggestion, suggestionsHidden } = this.state;
|
2021-11-01 10:25:18 -07:00
|
|
|
const firstIndex = this.getFirstIndex();
|
2021-11-01 11:45:17 -07:00
|
|
|
const lastIndex = suggestions.size + (menu || []).length - 1;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
if (e.which === 229) {
|
2020-03-27 13:59:38 -07:00
|
|
|
// Ignore key events during text composition
|
|
|
|
// e.key may be a name of the physical key even in this case (e.x. Safari / Chrome on Mac)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (e.key) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case 'Escape':
|
|
|
|
if (suggestions.size === 0 || suggestionsHidden) {
|
2022-06-04 16:21:16 -07:00
|
|
|
document.querySelector('.ui')?.parentElement?.focus();
|
2021-11-01 11:45:17 -07:00
|
|
|
} else {
|
2022-05-11 14:06:35 -07:00
|
|
|
e.preventDefault();
|
|
|
|
this.setState({ suggestionsHidden: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ArrowDown':
|
|
|
|
if (!suggestionsHidden && (suggestions.size > 0 || menu)) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.setState({ selectedSuggestion: Math.min(selectedSuggestion + 1, lastIndex) });
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ArrowUp':
|
|
|
|
if (!suggestionsHidden && (suggestions.size > 0 || menu)) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.setState({ selectedSuggestion: Math.max(selectedSuggestion - 1, firstIndex) });
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'Enter':
|
|
|
|
case 'Tab':
|
2022-06-04 16:21:16 -07:00
|
|
|
// Select suggestion
|
2022-05-11 14:06:35 -07:00
|
|
|
if (!suggestionsHidden && selectedSuggestion > -1 && (suggestions.size > 0 || menu)) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.setState({ selectedSuggestion: firstIndex });
|
|
|
|
|
|
|
|
if (selectedSuggestion < suggestions.size) {
|
|
|
|
this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestions.get(selectedSuggestion));
|
2022-06-04 16:21:16 -07:00
|
|
|
} else if (menu) {
|
2022-05-11 14:06:35 -07:00
|
|
|
const item = menu[selectedSuggestion - suggestions.size];
|
2022-06-04 16:21:16 -07:00
|
|
|
this.handleMenuItemAction(item, e);
|
2022-05-11 14:06:35 -07:00
|
|
|
}
|
2021-11-01 11:45:17 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-05-11 14:06:35 -07:00
|
|
|
break;
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (e.defaultPrevented || !this.props.onKeyDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
if (this.props.onKeyDown) {
|
|
|
|
this.props.onKeyDown(e);
|
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
onBlur = () => {
|
|
|
|
this.setState({ suggestionsHidden: true, focused: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
onFocus = () => {
|
|
|
|
this.setState({ focused: true });
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
onSuggestionClick: React.MouseEventHandler = (e) => {
|
|
|
|
const index = Number(e.currentTarget?.getAttribute('data-index'));
|
|
|
|
const suggestion = this.props.suggestions.get(index);
|
2020-03-27 13:59:38 -07:00
|
|
|
this.props.onSuggestionSelected(this.state.tokenStart, this.state.lastToken, suggestion);
|
2022-06-04 16:21:16 -07:00
|
|
|
this.input?.focus();
|
|
|
|
e.preventDefault();
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
componentDidUpdate(prevProps: IAutosuggestInput, prevState: any) {
|
2020-07-04 16:41:41 -07:00
|
|
|
const { suggestions } = this.props;
|
|
|
|
if (suggestions !== prevProps.suggestions && suggestions.size > 0 && prevState.suggestionsHidden && prevState.focused) {
|
|
|
|
this.setState({ suggestionsHidden: false });
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
setInput = (c: HTMLInputElement) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
this.input = c;
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:36:34 -07:00
|
|
|
renderSuggestion = (suggestion: AutoSuggestion, i: number) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
const { selectedSuggestion } = this.state;
|
|
|
|
let inner, key;
|
|
|
|
|
|
|
|
if (typeof suggestion === 'object') {
|
|
|
|
inner = <AutosuggestEmoji emoji={suggestion} />;
|
|
|
|
key = suggestion.id;
|
|
|
|
} else if (suggestion[0] === '#') {
|
|
|
|
inner = suggestion;
|
|
|
|
key = suggestion;
|
|
|
|
} else {
|
2022-03-21 11:09:01 -07:00
|
|
|
inner = <AutosuggestAccount id={suggestion} />;
|
2020-03-27 13:59:38 -07:00
|
|
|
key = suggestion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-03-21 11:09:01 -07:00
|
|
|
<div
|
|
|
|
role='button'
|
2022-06-04 16:21:16 -07:00
|
|
|
tabIndex={0}
|
2022-03-21 11:09:01 -07:00
|
|
|
key={key}
|
|
|
|
data-index={i}
|
|
|
|
className={classNames({
|
2022-04-14 06:36:07 -07:00
|
|
|
'px-4 py-2.5 text-sm text-gray-700 dark:text-gray-400 cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 group': true,
|
|
|
|
'bg-gray-100 dark:bg-slate-700 hover:bg-gray-100 dark:hover:bg-gray-700': i === selectedSuggestion,
|
2022-03-21 11:09:01 -07:00
|
|
|
})}
|
|
|
|
onMouseDown={this.onSuggestionClick}
|
|
|
|
>
|
2020-03-27 13:59:38 -07:00
|
|
|
{inner}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
handleMenuItemAction = (item: MenuItem | null, e: React.MouseEvent | React.KeyboardEvent) => {
|
2021-11-01 11:45:17 -07:00
|
|
|
this.onBlur();
|
2022-06-04 16:21:16 -07:00
|
|
|
if (item?.action) {
|
|
|
|
item.action(e);
|
|
|
|
}
|
2021-11-01 11:45:17 -07:00
|
|
|
}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
handleMenuItemClick = (item: MenuItem | null): React.MouseEventHandler => {
|
2021-11-01 11:45:17 -07:00
|
|
|
return e => {
|
|
|
|
e.preventDefault();
|
2022-06-04 16:21:16 -07:00
|
|
|
this.handleMenuItemAction(item, e);
|
2021-11-01 11:45:17 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMenu = () => {
|
|
|
|
const { menu, suggestions } = this.props;
|
|
|
|
const { selectedSuggestion } = this.state;
|
|
|
|
|
|
|
|
if (!menu) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return menu.map((item, i) => (
|
|
|
|
<a
|
2022-04-14 06:36:07 -07:00
|
|
|
className={classNames('flex items-center space-x-2 px-4 py-2.5 text-sm text-gray-700 dark:text-gray-400 cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700', { selected: suggestions.size - selectedSuggestion === i })}
|
2021-11-01 11:45:17 -07:00
|
|
|
href='#'
|
|
|
|
role='button'
|
2022-06-04 16:21:16 -07:00
|
|
|
tabIndex={0}
|
2021-11-01 11:45:17 -07:00
|
|
|
onMouseDown={this.handleMenuItemClick(item)}
|
|
|
|
key={i}
|
|
|
|
>
|
2022-06-04 16:21:16 -07:00
|
|
|
{item?.icon && (
|
2021-11-01 11:53:30 -07:00
|
|
|
<Icon src={item.icon} />
|
|
|
|
)}
|
|
|
|
|
2022-06-04 16:21:16 -07:00
|
|
|
<span>{item?.text}</span>
|
2021-11-01 11:45:17 -07:00
|
|
|
</a>
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
render() {
|
2022-06-04 15:23:25 -07:00
|
|
|
const { value, suggestions, disabled, placeholder, onKeyUp, autoFocus, className, id, maxLength, menu, resultsPosition } = this.props;
|
2020-03-27 13:59:38 -07:00
|
|
|
const { suggestionsHidden } = this.state;
|
2022-06-04 16:21:16 -07:00
|
|
|
const style: React.CSSProperties = { direction: 'ltr' };
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-11-01 11:45:17 -07:00
|
|
|
const visible = !suggestionsHidden && (!suggestions.isEmpty() || (menu && value));
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
if (isRtl(value)) {
|
|
|
|
style.direction = 'rtl';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-03-21 11:09:01 -07:00
|
|
|
<div className='relative'>
|
|
|
|
<label className='sr-only'>{placeholder}</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
className={classNames({
|
2022-04-16 14:10:50 -07:00
|
|
|
'block w-full sm:text-sm dark:bg-slate-800 dark:text-white dark:placeholder:text-gray-500 focus:ring-indigo-500 focus:border-indigo-500': true,
|
2022-06-04 16:21:16 -07:00
|
|
|
}, className)}
|
2022-03-21 11:09:01 -07:00
|
|
|
ref={this.setInput}
|
|
|
|
disabled={disabled}
|
|
|
|
placeholder={placeholder}
|
|
|
|
autoFocus={autoFocus}
|
|
|
|
value={value}
|
|
|
|
onChange={this.onChange}
|
|
|
|
onKeyDown={this.onKeyDown}
|
|
|
|
onKeyUp={onKeyUp}
|
|
|
|
onFocus={this.onFocus}
|
|
|
|
onBlur={this.onBlur}
|
|
|
|
style={style}
|
|
|
|
aria-autocomplete='list'
|
|
|
|
id={id}
|
|
|
|
maxLength={maxLength}
|
2022-04-06 07:10:21 -07:00
|
|
|
data-testid='autosuggest-input'
|
2022-03-21 11:09:01 -07:00
|
|
|
/>
|
|
|
|
|
|
|
|
<div className={classNames({
|
2022-06-04 15:23:25 -07:00
|
|
|
'absolute w-full z-50 shadow bg-white dark:bg-slate-800 rounded-lg py-1': true,
|
|
|
|
'top-full': resultsPosition === 'below',
|
|
|
|
'bottom-full': resultsPosition === 'above',
|
2022-03-21 11:09:01 -07:00
|
|
|
hidden: !visible,
|
|
|
|
block: visible,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<div className='space-y-0.5'>
|
|
|
|
{suggestions.map(this.renderSuggestion)}
|
|
|
|
</div>
|
|
|
|
|
2021-11-01 11:45:17 -07:00
|
|
|
{this.renderMenu()}
|
2020-03-27 13:59:38 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|