/** * Copyright (c) Paymium. * * This source code is licensed under the MIT license found in the * LICENSE file in the root of this projects source tree. */ import { withStaticProperties } from '@crossed/core'; import { ComponentProps, forwardRef, memo, RefAttributes, useCallback, useId, } from 'react'; import { composeStyles, inlineStyle, useTheme } from '@crossed/styled'; import { ChevronDown } from '@crossed/unicons'; import { form } from '../../styles/form'; import { VisibilityHidden } from '../../other/VisibilityHidden'; import { CloseButton } from '../../buttons/CloseButton'; import { Button } from '../../buttons/Button'; import { XBox } from '../../layout/XBox'; import { Text } from '../../typography/Text'; import { FormControl } from '../../forms/Form'; import { useSelectConfig, useSelectValue } from './context'; import { useSelect } from './styles'; import { TextInput, View } from 'react-native'; import { Floating } from '../../overlay/Floating'; type ButtonProps = ComponentProps; const ClearButton = memo(() => { const { value, setValue } = useSelectValue(); const showClear = !!value; const handleClear = useCallback(() => setValue(''), [setValue]); return showClear ? ( ({ base: { marginRight: space.xl }, })) )} > ) : null; }); const Value = () => { const { value, items, renderValue } = useSelectValue(); const { multiple } = useSelectConfig(); const id = useId(); const tmp = !value ? [] : Array.isArray(value) ? value : [value]; const toRender = tmp && tmp.length > 3 ? tmp.slice(0, 3) : tmp; const labelByValue = items.reduce((acc, i) => { if ('title' in i) { return { ...acc, ...i.data.reduce((acc2, d) => ({ ...acc2, [`${d.value}`]: d }), {}), }; } return { ...acc, [`${i.value}`]: i }; }, {}); return ( <> ({ base: { gap: 5, flexShrink: 1 } }))}> {renderValue ? renderValue(value as any) : toRender.map((e, i) => ( ({ base: { backgroundColor: colors.info.light, paddingLeft: space.xs, paddingRight: space.xs, paddingTop: space.xs, paddingBottom: space.xs, borderRadius: 4, borderWidth: 1, borderColor: colors.info.primary, color: colors.info.dark, }, })) // style )} > {i === 2 ? `...+${tmp.length - i}` : (labelByValue as any)[`${e}`]?.label} ))} ); }; export const SelectTrigger = withStaticProperties( memo>( forwardRef(({ children, ...props }, ref) => { const { clearable } = useSelectConfig(); useTheme(); return ( {children ?? ( <> )} {clearable && } ); }) ), { Text: Button.Text } ); SelectTrigger.displayName = 'Select.Trigger';