Added default props for messages/infos in Autocomplete

This commit is contained in:
Philipp Czora
2018-11-22 10:18:13 +01:00
parent d6eee33dec
commit 8090e3b7f7

View File

@@ -18,18 +18,30 @@ type Props = {
valueSelected: SelectValue => void, valueSelected: SelectValue => void,
label: string, label: string,
helpText?: string, helpText?: string,
value?: SelectValue value?: SelectValue,
placeholder: string,
loadingMessage: string,
noOptionsMessage: string
}; };
type State = {}; type State = {};
class Autocomplete extends React.Component<Props, State> { class Autocomplete extends React.Component<Props, State> {
static defaultProps = {
placeholder: "Type here",
loadingMessage: "Loading...",
noOptionsMessage: "No suggestion available"
}
handleInputChange = (newValue: SelectValue) => { handleInputChange = (newValue: SelectValue) => {
this.props.valueSelected(newValue); this.props.valueSelected(newValue);
}; };
isValidNewOption = (inputValue, selectValue, selectOptions) => { // We overwrite this to avoid running into a bug (https://github.com/JedWatson/react-select/issues/2944)
//TODO: types isValidNewOption = (inputValue: string, selectValue: SelectValue, selectOptions: SelectValue[]) => {
const isNotDuplicated = !selectOptions const isNotDuplicated = !selectOptions
.map(option => option.label) .map(option => option.label)
.includes(inputValue); .includes(inputValue);
@@ -38,19 +50,19 @@ class Autocomplete extends React.Component<Props, State> {
}; };
render() { render() {
const { label, helpText, value } = this.props; const { label, helpText, value, placeholder, loadingMessage, noOptionsMessage, loadSuggestions } = this.props;
return ( return (
<div className="field"> <div className="field">
<LabelWithHelpIcon label={label} helpText={helpText} /> <LabelWithHelpIcon label={label} helpText={helpText} />
<div className="control"> <div className="control">
<AsyncCreatable <AsyncCreatable
cacheOptions cacheOptions
loadOptions={this.props.loadSuggestions} loadOptions={loadSuggestions}
onChange={this.handleInputChange} onChange={this.handleInputChange}
value={value} value={value}
placeholder="Start typing..." // TODO: i18n placeholder={placeholder}
loadingMessage={() => <>Loading...</>} // TODO: i18n loadingMessage={() => loadingMessage}
noOptionsMessage={() => <>No suggestion available</>} // TODO: i18n noOptionsMessage={() => noOptionsMessage}
isValidNewOption={this.isValidNewOption} isValidNewOption={this.isValidNewOption}
onCreateOption={value => { onCreateOption={value => {
this.handleInputChange({ this.handleInputChange({
@@ -65,4 +77,5 @@ class Autocomplete extends React.Component<Props, State> {
} }
} }
export default Autocomplete; export default Autocomplete;