import * as _ from "lodash"; import * as React from "react"; import ReactSelect from "react-select/lib/Async"; import { Input, IInputProps } from "../Input"; export interface ISelectProps { id: string; placeholder?: string; defaultValue?: { value: string, label: string, input?: IInputProps }; defaultOptions?: Array<{ value: string, label: string, input?: IInputProps}>; noOptionsMessage?: string; isSearchable?: boolean; hideDropdown?: boolean; subtext?: string; supertext?: string; loadOptions?: (inputValue: string, complete: (value: any) => void) => any; onChange?: (id: string, selectedOption: string | number | { value: string, label: string, input?: IInputProps }) => void; } interface ISelectState { selectedOption: { value: string, label: string, input?: IInputProps }; } const defaultSelectState: ISelectState = { selectedOption: null, }; export class Select extends React.PureComponent { public static getDerivedStateFromProps(props: ISelectProps, state: ISelectState) { return { selectedOption: state.selectedOption || props.defaultValue, }; } public state: ISelectState = defaultSelectState; public componentDidMount() { if (this.props.defaultValue) { this.handleChange(this.props.defaultValue); } } public render() { return (
this.props.noOptionsMessage }} /> {!_.isEmpty(this.props.supertext) && {this.props.supertext}} {!_.isEmpty(this.props.subtext) && {this.props.subtext}} {this.state.selectedOption && this.state.selectedOption.input && (
{ this.props.onChange(id, value) }} />
)}
); } private handleChange = (selectedOption: any) => { this.setState({ selectedOption, }, () => { this.props.onChange(this.props.id, selectedOption); }); } private loadOptions = (inputValue: string, complete: any) => { if (this.props.loadOptions) { this.props.loadOptions(inputValue, complete); } else { complete(this.props.defaultOptions); } } }