import React, { useRef, useState } from 'react'; import styled from 'styled-components'; import { FilledVariant, Size } from '../../types'; import Dropdown from '../Dropdown'; import { Icon } from '../Icons'; import IconText from '../IconText/IconText'; import { InputField } from '../InputField'; import { TypographyWeight } from '../Typography'; import { SelectProps } from './Select.types'; const SelectContainer = styled.div<{ $width?: number | string }>` ${({ $width }) => $width && `width: ${typeof $width === 'string' ? $width : `${$width}px`};`} `; export default function Select({ children, onChange, dataTest, disabled, forceTheme, ghostColor, maxHeight, menuControls, placeholder, size = Size.LARGE, value, width, fullWidth = true, variant = FilledVariant.UNFILLED, zIndex }: SelectProps) { // Whether the dropdown is visible or not const [menuOpen, setMenuOpen] = useState(false); const isOpen = menuControls?.isOpen || menuOpen; const endIcon = menuOpen ? Icon.ChevronUp : Icon.ChevronDown; const typographyWeight = TypographyWeight.REGULAR; // To get dropdown anchor const selectTriggerRef: React.MutableRefObject = useRef(null); const selectedLabel = children.find((child) => value === child.props.value)?.props.label; const toggleOpen = (e?: React.MouseEvent) => { e?.stopPropagation(); if (!!menuControls) menuControls.setIsOpen(!isOpen); else setMenuOpen(!isOpen); }; const renderDisabledField = () => ( ); const renderEnabledField = () => variant === FilledVariant.FILLED ? ( // Filled field ) : ( // Ghost field ); const renderSelectItems = () => children.map((child) => { return React.cloneElement(child, { active: value === child.props.value, key: typeof child.props.label === 'string' ? child.props.label : child.props.value, onClick: async (e: React.MouseEvent) => { e.stopPropagation(); // If the child has its own onClick function passed, then it is not a normal select item // and we should run onClick without running onChange if (!!child.props.onClick) await child.props.onClick(e); else if (!!child.props.value) onChange(child.props.value); toggleOpen(); } }); }); const renderOptionMenu = () => ( toggleOpen()} showDropdown={isOpen} fullWidth={fullWidth} maxHeight={maxHeight} zIndex={zIndex} > {renderSelectItems()} ); return ( {/* Field */} {disabled ? renderDisabledField() : renderEnabledField()} {/* Option menu */} {renderOptionMenu()} ); }