import ChevronIcon from "@Assets/Icons/chevron.svg"; import WindowStore from "@Front/Stores/WindowStore"; import classNames from "classnames"; import Image from "next/image"; import React, { FormEvent, ReactNode } from "react"; import Typography, { ITypo } from "../Typography"; import classes from "./classes.module.scss"; type IProps = { selectedOption?: IOption; onChange: (selectedOption: IOption) => void; options: IOption[]; hasBorderRightCollapsed?: boolean; placeholder?: string; className?: string; name?: string; }; export type IOption = { value: unknown; label: string; icon?: ReactNode; }; type IState = { isOpen: boolean; listWidth: number; listHeight: number; selectedOption: IOption | null; }; export default class Select extends React.Component { private contentRef = React.createRef(); private rootRef = React.createRef(); private removeOnresize = () => {}; constructor(props: IProps) { super(props); this.state = { isOpen: false, listHeight: 0, listWidth: 0, selectedOption: null, }; this.toggle = this.toggle.bind(this); this.onSelect = this.onSelect.bind(this); } public override render(): JSX.Element { const selectedOption = this.state.selectedOption ?? this.props.selectedOption; return (
{selectedOption && }
    {this.props.options.map((option, index) => (
  • this.onSelect(option, e)}>
    {option.icon}
    {option.label}
  • ))}
{this.state.isOpen &&
}
); } static getDerivedStateFromProps(props: IProps, state: IState) { if (props.selectedOption?.value) { return { value: props.selectedOption?.value, }; } return null; } public override componentDidMount(): void { this.onResize(); this.removeOnresize = WindowStore.getInstance().onResize(() => this.onResize()); } public override componentWillUnmount() { this.removeOnresize(); } private onResize() { let listHeight = 0; let listWidth = 0; listWidth = this.rootRef.current?.scrollWidth ?? 0; if (this.state.listHeight) { listHeight = this.contentRef.current?.scrollHeight ?? 0; } this.setState({ listHeight, listWidth }); } private toggle(e: FormEvent) { e.preventDefault(); let listHeight = 0; let listWidth = this.rootRef.current?.scrollWidth ?? 0; if (!this.state.listHeight) { listHeight = this.contentRef.current?.scrollHeight ?? 0; } this.setState((state) => { return { isOpen: !state.isOpen, listHeight, listWidth }; }); } private onSelect(option: IOption, e: React.MouseEvent) { this.props.onChange && this.props.onChange(option); this.setState({ selectedOption: option, }); this.toggle(e); } }