import * as React from 'react'; import classnames from 'classnames'; import Icon, {ICON_COLOR} from '../icons/Icon'; type OptionsPropsType = { value: string; text: string; }; type GroupedOptionsPropsType = { label: string; options: ReadonlyArray; }; type SelectSizeType = 's' | 'm' | 'l'; type SelectColorType = 'default' | 'white'; export const COLOR = { DEAFAULT: 'default', WHITE: 'white', } as const; export const SIZE = { S: 's', M: 'm', L: 'l', } as const; const ICON_SIZE_MAP = { [SIZE.L]: 32, [SIZE.M]: 24, [SIZE.S]: 16, } as const; export type SelectPropsType = { /** * Optional specification for select value * @example */ valid?: boolean; /** * Optional boolean to specified if it's invalid * @example */ capitalized?: boolean; /** * There are two color variants for form elements, default does not have to be specified * @example * @see size="m" https://styleguide.brainly.com/latest/docs/interactive.html?size="m"#select * @see size="l" https://styleguide.brainly.com/latest/docs/interactive.html?size="l"#select */ size?: SelectSizeType; /** * Optional boolean to specified if it's full width * @example */ options?: ReadonlyArray; /** * Additional class names */ className?: string; } & Omit< React.AllHTMLAttributes, | 'value' | 'valid' | 'invalid' | 'capitalized' | 'color' | 'size' | 'fullWidth' | 'options' | 'className' >; const getOptionElement = ({value, text}: OptionsPropsType) => ( ); const Select = React.forwardRef( (props: SelectPropsType, ref) => { const { valid, invalid, capitalized, fullWidth, value, size = SIZE.M, color, className, options = [], ...additionalProps } = props; if (valid === true && invalid === true) { throw { name: 'WrongValidation', message: 'Select can be either valid or invalid!', }; } const selectClass = classnames( 'sg-select', { 'sg-select--selected': value, 'sg-select--valid': valid, 'sg-select--invalid': invalid, 'sg-select--capitalized': capitalized, 'sg-select--full-width': fullWidth, [`sg-select--${String(color)}`]: color, [`sg-select--${String(size)}`]: size && size !== 'm', }, className ); const optionsElements = options.map((item, index) => { if ('options' in item) { return ( {item.options.map(getOptionElement)} ); } if (item.text || item.value) { return getOptionElement(item); } return null; }); return (
); } ); Select.displayName = 'Select'; export default Select;