import classNames from "classnames"; import React from "react"; import ReactSelect, { ActionMeta, MultiValue, Options, PropsValue } from "react-select"; import { IOption } from "../Select"; import Typography, { ITypo } from "../Typography"; import classes from "./classes.module.scss"; import { styles } from "./styles"; type IProps = { options: IOption[]; label?: string | JSX.Element; placeholder?: string; onChange?: (newValue: MultiValue, actionMeta: ActionMeta) => void; defaultValue?: PropsValue; value?: PropsValue; isMulti?: boolean; shouldCloseMenuOnSelect: boolean; isOptionDisabled?: (option: IOption, selectValue: Options) => boolean; }; type IState = { isFocused: boolean; selectedOptions: MultiValue; }; export default class MultiSelect extends React.Component { public static defaultProps: Partial = { placeholder: "Sélectionner une option...", shouldCloseMenuOnSelect: false, }; constructor(props: IProps) { super(props); this.state = { isFocused: false, selectedOptions: [], }; this.onChange = this.onChange.bind(this); this.onEmptyResearch = this.onEmptyResearch.bind(this); this.onFocus = this.onFocus.bind(this); this.onBlur = this.onBlur.bind(this); } public override render(): JSX.Element { return (
= 1 && classes["active"])}> {this.props.label &&
{this.props.label}
} {this.props.placeholder && (
= 1).toString()}> {this.props.placeholder}
)}
); } public override componentDidMount(): void { if (this.props.defaultValue) { // If default value contains multiple IOptions if (Array.isArray(this.props.defaultValue)) { this.setState({ selectedOptions: this.props.defaultValue }); } // If default value is a single IOption if ("label" in this.props.defaultValue) { this.setState({ selectedOptions: [this.props.defaultValue] }); } } } private onFocus() { this.setState({ isFocused: true }); } private onBlur() { this.setState({ isFocused: false }); } private onChange(newValue: MultiValue, actionMeta: ActionMeta) { this.props.onChange && this.props.onChange(newValue, actionMeta); this.setState({ selectedOptions: newValue, }); } private onEmptyResearch() { if (this.state.selectedOptions.length === this.props.options.length) { return null; } return "Aucune option trouvée"; } }