Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

FieldDetailSettingsMenu.tsx

123 lines3.5 KB

1import EllipsisIcon from 'lucide-solid/icons/ellipsis'
2import type { FieldDetailSettings } from '@/eventClientTypes'
3import type { FieldId } from '@/types/branded'
4import { Button } from '@/components/ui/button'
5import { Label } from '@/components/ui/label'
6import {
7 Popover,
8 PopoverContent,
9 PopoverDescription,
10 PopoverHeader,
11 PopoverTitle,
12 PopoverTrigger,
13} from '@/components/ui/popover'
14import {
15 Select,
16 SelectContent,
17 SelectItem,
18 SelectTrigger,
19 SelectValue,
20} from '@/components/ui/select'
21import {
22 SliderControl,
23 SliderLabel,
24 SliderRoot,
25 SliderValue,
26} from '@/components/ui/slider'
27import { Switch } from '@/components/ui/switch'
28import { useFormDevtoolsStore } from '@/stores/formDevtoolsStore'
29​
30interface FieldDetailSettingsMenuProps {
31 fieldId: FieldId
32 fieldPath: string
34​
35function formatDebounce({ value }: { value: number }) {
36 if (!value) return 'Instant'
37 return `${value}ms`
39​
40export function FieldDetailSettingsMenu({
41 fieldId,
42 fieldPath,
43}: FieldDetailSettingsMenuProps) {
44 const store = useFormDevtoolsStore()
45 const { getFieldDetailSettings, updateFieldDetailSettings } =
46 store.fieldDetails
47​
48 const settings = () => getFieldDetailSettings(fieldId)
49​
50 const patchSettings = (patch: Partial<FieldDetailSettings>) =>
51 updateFieldDetailSettings(fieldId, patch)
52​
53 const debounceLabel = () => formatDebounce({ value: settings().debounceMs })
54​
55 return (
56 <Popover positioning={{ placement: 'right' }}>
57 <PopoverTrigger
58 asChild={(innerProps) => (
59 <Button variant="ghost" size="icon-sm" {...innerProps()} />
60 )}
61 >
62 <EllipsisIcon />
63 </PopoverTrigger>
64 <PopoverContent class="min-w-32 p-3 flex flex-col gap-4">
65 <PopoverHeader>
66 <PopoverTitle>{fieldPath}</PopoverTitle>
67 <PopoverDescription>Settings</PopoverDescription>
68 </PopoverHeader>
69 <Switch
70 label="Listen for values data"
71 checked={settings().includeValues}
72 onCheckedChange={({ checked }) =>
73 patchSettings({
74 includeValues: checked,
75 })
76 }
77 />
78 <div class="grid gap-2">
79 <Label>Error payload</Label>
80 <Select
81 value={[settings().errorPayloadMode]}
82 onValueChange={({ value }) => {
83 const errorPayloadMode = value[0]
84 if (
85 errorPayloadMode === 'full' ||
86 errorPayloadMode === 'messages'
87 ) {
88 patchSettings({
89 errorPayloadMode,
90 })
91 }
92 }}
93 >
94 <SelectTrigger class="w-full">
95 <SelectValue placeholder="Select error payload" />
96 </SelectTrigger>
97 <SelectContent>
98 <SelectItem value="full">Full</SelectItem>
99 <SelectItem value="messages">Messages only</SelectItem>
100 </SelectContent>
101 </Select>
102 </div>
103 <SliderRoot
104 class="mx-auto w-full max-w-xs"
105 value={[settings().debounceMs]}
106 onValueChange={({ value }) =>
107 patchSettings({
108 debounceMs: value[0] ?? 0,
109 })
110 }
111 getAriaValueText={formatDebounce}
112 min={0}
113 max={500}
114 step={50}
115 >
116 <SliderLabel>Detail debounce</SliderLabel>
117 <SliderValue>{debounceLabel()}</SliderValue>
118 <SliderControl />
119 </SliderRoot>
120 </PopoverContent>
121 </Popover>
122 )