import * as Ariakit from '@ariakit/react'; import { getClassNames } from '@websolutespa/bom-core'; import { IconX } from '@websolutespa/bom-mixer-icons'; import { forwardRef, HTMLProps, useContext } from 'react'; import styled from 'styled-components'; import { getVariantKey } from '../../variants'; import { SelectContext } from './select.context'; import { ISelectVariants, selectVariants } from './select.variants'; const SelectValueStyle = styled.div` ${props => getVariantKey('value', props.variant, props.variants)} `; export type SelectValueProps = HTMLProps & { value?: string | string[]; label?: string; variant?: keyof typeof selectVariants | (string & {}); variants?: ISelectVariants; }; export const SelectValue = forwardRef( function SelectValue({ value, label, variant, variants, className, ...props }: SelectValueProps, ref) { const selectContext = Ariakit.useSelectContext(); const context = useContext(SelectContext); if (!selectContext) { return; } if (!context) { return; } const onRemove = () => { const state = selectContext?.getState(); const stateValue = (Array.isArray(state.value) ? state.value : []).filter(x => x !== value); selectContext.setValue([...stateValue]); // console.log(value, selectContext); }; const classNames = getClassNames('select__value', className); return ( onRemove()} > {label} ); });