139 lines
4.8 KiB
TypeScript
139 lines
4.8 KiB
TypeScript
import * as SelectPrimitive from '@rn-primitives/select';
|
|
import * as React from 'react';
|
|
import { StyleSheet, View } from 'react-native';
|
|
import Animated, { FadeIn, FadeOut } from 'react-native-reanimated';
|
|
import { Check, ChevronDown, ChevronUp } from '~/components/ui/icons';
|
|
import { cn } from '~/lib/utils';
|
|
|
|
type Option = SelectPrimitive.Option;
|
|
|
|
const Select = SelectPrimitive.Root;
|
|
|
|
const SelectGroup = SelectPrimitive.Group;
|
|
|
|
const SelectValue = SelectPrimitive.Value;
|
|
|
|
const SelectTrigger = React.forwardRef<SelectPrimitive.TriggerRef, SelectPrimitive.TriggerProps>(
|
|
({ className, children, ...props }, ref) => (
|
|
<SelectPrimitive.Trigger
|
|
ref={ref}
|
|
className={cn(
|
|
'flex flex-row h-10 native:h-12 items-center text-sm justify-between rounded-md border border-input bg-background px-3 py-2 text-muted-foreground [&>span]:line-clamp-1',
|
|
props.disabled && 'opacity-50',
|
|
className
|
|
)}
|
|
{...props}
|
|
>
|
|
{typeof children === 'function' ? children({ pressed: false }) : children}
|
|
<ChevronDown size={16} aria-hidden={true} className='text-foreground opacity-50' />
|
|
</SelectPrimitive.Trigger>
|
|
)
|
|
);
|
|
SelectTrigger.displayName = SelectPrimitive.Trigger.displayName;
|
|
|
|
const SelectScrollUpButton = () => null;
|
|
|
|
const SelectScrollDownButton = () => null;
|
|
|
|
const SelectContent = React.forwardRef<
|
|
SelectPrimitive.ContentRef,
|
|
SelectPrimitive.ContentProps & { portalHost?: string }
|
|
>(({ className, children, position = 'popper', portalHost, ...props }, ref) => {
|
|
const { open } = SelectPrimitive.useRootContext();
|
|
|
|
return (
|
|
<SelectPrimitive.Portal hostName={portalHost}>
|
|
<SelectPrimitive.Overlay style={StyleSheet.absoluteFill}>
|
|
<Animated.View className='z-50' entering={FadeIn} exiting={FadeOut}>
|
|
<SelectPrimitive.Content
|
|
ref={ref}
|
|
className={cn(
|
|
'relative z-50 max-h-96 min-w-[8rem] rounded-md border border-border bg-popover shadow-md shadow-foreground/10 py-2 px-1 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
|
|
position === 'popper' &&
|
|
'data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1',
|
|
className
|
|
)}
|
|
position={position}
|
|
{...props}
|
|
>
|
|
<SelectScrollUpButton />
|
|
<SelectPrimitive.Viewport
|
|
className={cn(
|
|
'p-1',
|
|
position === 'popper' &&
|
|
'h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]'
|
|
)}
|
|
>
|
|
{children}
|
|
</SelectPrimitive.Viewport>
|
|
<SelectScrollDownButton />
|
|
</SelectPrimitive.Content>
|
|
</Animated.View>
|
|
</SelectPrimitive.Overlay>
|
|
</SelectPrimitive.Portal>
|
|
);
|
|
});
|
|
SelectContent.displayName = SelectPrimitive.Content.displayName;
|
|
|
|
const SelectLabel = React.forwardRef<SelectPrimitive.LabelRef, SelectPrimitive.LabelProps>(
|
|
({ className, ...props }, ref) => (
|
|
<SelectPrimitive.Label
|
|
ref={ref}
|
|
className={cn(
|
|
'py-1.5 native:pb-2 pl-8 native:pl-10 pr-2 text-popover-foreground text-sm native:text-base font-semibold',
|
|
className
|
|
)}
|
|
{...props}
|
|
/>
|
|
)
|
|
);
|
|
SelectLabel.displayName = SelectPrimitive.Label.displayName;
|
|
|
|
const SelectItem = React.forwardRef<SelectPrimitive.ItemRef, SelectPrimitive.ItemProps>(
|
|
({ className, children, ...props }, ref) => (
|
|
<SelectPrimitive.Item
|
|
ref={ref}
|
|
className={cn(
|
|
'relative flex flex-row w-full items-center rounded-sm py-1.5 native:py-2 pl-8 native:pl-10 pr-2 active:bg-accent',
|
|
props.disabled && 'opacity-50',
|
|
className
|
|
)}
|
|
{...props}
|
|
>
|
|
<View className='absolute left-2 native:left-3.5 flex h-3.5 native:pt-px w-3.5 items-center justify-center'>
|
|
<SelectPrimitive.ItemIndicator>
|
|
<Check size={16} strokeWidth={3} className='text-popover-foreground' />
|
|
</SelectPrimitive.ItemIndicator>
|
|
</View>
|
|
<SelectPrimitive.ItemText className='text-sm text-popover-foreground native:text-base' />
|
|
</SelectPrimitive.Item>
|
|
)
|
|
);
|
|
SelectItem.displayName = SelectPrimitive.Item.displayName;
|
|
|
|
const SelectSeparator = React.forwardRef<
|
|
SelectPrimitive.SeparatorRef,
|
|
SelectPrimitive.SeparatorProps
|
|
>(({ className, ...props }, ref) => (
|
|
<SelectPrimitive.Separator
|
|
ref={ref}
|
|
className={cn('-mx-1 my-1 h-px bg-muted', className)}
|
|
{...props}
|
|
/>
|
|
));
|
|
SelectSeparator.displayName = SelectPrimitive.Separator.displayName;
|
|
|
|
export {
|
|
Select,
|
|
SelectContent,
|
|
SelectGroup,
|
|
SelectItem,
|
|
SelectLabel,
|
|
SelectScrollDownButton,
|
|
SelectScrollUpButton,
|
|
SelectSeparator,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
type Option,
|
|
};
|