2019-06-20 19:37:28 +07:00
|
|
|
import React, { KeyboardEvent } from 'react';
|
2019-06-30 05:39:56 +07:00
|
|
|
import { css } from 'emotion';
|
2019-02-03 17:23:33 +07:00
|
|
|
import Autosuggest from 'react-autosuggest';
|
|
|
|
import match from 'autosuggest-highlight/match';
|
|
|
|
import parse from 'autosuggest-highlight/parse';
|
|
|
|
import MenuItem from '@material-ui/core/MenuItem';
|
|
|
|
|
|
|
|
import { fontWeight } from '../../utils/styles/sizes';
|
2019-10-08 03:19:18 +07:00
|
|
|
|
2019-05-11 13:59:39 +07:00
|
|
|
import { Wrapper, InputField, SuggestionContainer } from './styles';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
interface Props {
|
|
|
|
suggestions: unknown[];
|
2019-06-20 19:37:28 +07:00
|
|
|
suggestionsLoading?: boolean;
|
|
|
|
suggestionsLoaded?: boolean;
|
|
|
|
suggestionsError?: boolean;
|
|
|
|
apiLoading?: boolean;
|
|
|
|
color?: string;
|
|
|
|
value?: string;
|
|
|
|
placeholder?: string;
|
2019-06-25 05:54:32 +07:00
|
|
|
startAdornment?: JSX.Element;
|
2019-06-20 19:37:28 +07:00
|
|
|
disableUnderline?: boolean;
|
|
|
|
onChange?: (event: KeyboardEvent<HTMLInputElement>, { newValue, method }: { newValue: string; method: string }) => void;
|
|
|
|
onSuggestionsFetch?: ({ value: string }) => Promise<void>;
|
|
|
|
onCleanSuggestions?: () => void;
|
2019-06-25 05:54:32 +07:00
|
|
|
onClick?: (event: KeyboardEvent<HTMLInputElement>, { suggestionValue, method }: { suggestionValue: string[]; method: string }) => void;
|
2019-06-20 19:37:28 +07:00
|
|
|
onKeyDown?: (event: KeyboardEvent<HTMLInputElement>) => void;
|
|
|
|
onBlur?: (event: KeyboardEvent<HTMLInputElement>) => void;
|
|
|
|
}
|
|
|
|
|
2019-10-08 03:19:18 +07:00
|
|
|
/* eslint-disable react/jsx-sort-props */
|
|
|
|
/* eslint-disable verdaccio/jsx-spread */
|
2019-06-25 05:01:13 +07:00
|
|
|
const renderInputComponent = (inputProps): JSX.Element => {
|
2019-02-03 17:23:33 +07:00
|
|
|
const { ref, startAdornment, disableUnderline, onKeyDown, ...others } = inputProps;
|
|
|
|
return (
|
|
|
|
<InputField
|
2019-10-08 03:19:18 +07:00
|
|
|
fullWidth={true}
|
2019-02-03 17:23:33 +07:00
|
|
|
InputProps={{
|
|
|
|
inputRef: node => {
|
|
|
|
ref(node);
|
|
|
|
},
|
|
|
|
startAdornment,
|
|
|
|
disableUnderline,
|
|
|
|
onKeyDown,
|
|
|
|
}}
|
|
|
|
{...others}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSuggestionValue = (suggestion): string => suggestion.name;
|
|
|
|
|
2019-06-25 05:01:13 +07:00
|
|
|
const renderSuggestion = (suggestion, { query, isHighlighted }): JSX.Element => {
|
2019-02-03 17:23:33 +07:00
|
|
|
const matches = match(suggestion.name, query);
|
|
|
|
const parts = parse(suggestion.name, matches);
|
|
|
|
return (
|
2019-06-20 19:37:28 +07:00
|
|
|
<MenuItem component="div" selected={isHighlighted}>
|
2019-02-03 17:23:33 +07:00
|
|
|
<div>
|
|
|
|
{parts.map((part, index) => {
|
2019-06-30 05:39:56 +07:00
|
|
|
const fw = part.highlight ? fontWeight.semiBold : fontWeight.light;
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
className={css`
|
|
|
|
font-weight: ${fw};
|
|
|
|
`}
|
|
|
|
href={suggestion.link}
|
|
|
|
key={String(index)}>
|
2019-02-03 17:23:33 +07:00
|
|
|
{part.text}
|
2019-06-20 19:37:28 +07:00
|
|
|
</a>
|
2019-02-03 17:23:33 +07:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-06-25 05:01:13 +07:00
|
|
|
const renderMessage = (message): JSX.Element => {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
2019-06-20 19:37:28 +07:00
|
|
|
<MenuItem component="div" selected={false}>
|
2019-02-03 17:23:33 +07:00
|
|
|
<div>{message}</div>
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const SUGGESTIONS_RESPONSE = {
|
|
|
|
LOADING: 'Loading...',
|
|
|
|
FAILURE: 'Something went wrong.',
|
|
|
|
NO_RESULT: 'No results found.',
|
|
|
|
};
|
|
|
|
|
|
|
|
const AutoComplete = ({
|
|
|
|
suggestions,
|
|
|
|
startAdornment,
|
|
|
|
onChange,
|
|
|
|
onSuggestionsFetch,
|
|
|
|
onCleanSuggestions,
|
|
|
|
value = '',
|
|
|
|
placeholder = '',
|
|
|
|
disableUnderline = false,
|
|
|
|
color,
|
|
|
|
onClick,
|
|
|
|
onKeyDown,
|
|
|
|
onBlur,
|
|
|
|
suggestionsLoading = false,
|
|
|
|
suggestionsLoaded = false,
|
|
|
|
suggestionsError = false,
|
2019-06-25 05:01:13 +07:00
|
|
|
}: Props): JSX.Element => {
|
2019-02-03 17:23:33 +07:00
|
|
|
const autosuggestProps = {
|
|
|
|
renderInputComponent,
|
|
|
|
suggestions,
|
|
|
|
getSuggestionValue,
|
|
|
|
renderSuggestion,
|
|
|
|
onSuggestionsFetchRequested: onSuggestionsFetch,
|
|
|
|
onSuggestionsClearRequested: onCleanSuggestions,
|
|
|
|
};
|
|
|
|
const inputProps = {
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
placeholder,
|
|
|
|
startAdornment,
|
|
|
|
disableUnderline,
|
|
|
|
color,
|
|
|
|
onKeyDown,
|
|
|
|
onBlur,
|
|
|
|
};
|
|
|
|
|
|
|
|
// this format avoid arrow function eslint rule
|
2019-06-25 05:01:13 +07:00
|
|
|
function renderSuggestionsContainer({ containerProps, children, query }): JSX.Element {
|
2019-02-03 17:23:33 +07:00
|
|
|
return (
|
2019-05-11 13:59:39 +07:00
|
|
|
<SuggestionContainer {...containerProps} square={true}>
|
2019-02-03 17:23:33 +07:00
|
|
|
{suggestionsLoaded && children === null && query && renderMessage(SUGGESTIONS_RESPONSE.NO_RESULT)}
|
|
|
|
{suggestionsLoading && query && renderMessage(SUGGESTIONS_RESPONSE.LOADING)}
|
|
|
|
{suggestionsError && renderMessage(SUGGESTIONS_RESPONSE.FAILURE)}
|
|
|
|
{children}
|
2019-05-11 13:59:39 +07:00
|
|
|
</SuggestionContainer>
|
2019-02-03 17:23:33 +07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Wrapper>
|
|
|
|
<Autosuggest {...autosuggestProps} inputProps={inputProps} onSuggestionSelected={onClick} renderSuggestionsContainer={renderSuggestionsContainer} />
|
|
|
|
</Wrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AutoComplete;
|