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

FieldMetaBadge.tsx

80 lines2.5 KB

1import { cva } from 'class-variance-authority'
2import { splitProps } from 'solid-js'
3import { Badge } from '../ui/badge'
4import type { VariantProps } from 'class-variance-authority'
5import type { HTMLArkProps } from '@ark-ui/solid'
6import { cn } from '@/utils'
7​
8const fieldMetaBadgeVariants = cva('', {
9 variants: {
10 tone: {
11 neutral: 'bg-transparent text-foreground border-border',
12 change: 'bg-blue-100 text-blue-700 dark:bg-blue-950 dark:text-blue-300',
13 success:
14 'bg-green-100 text-green-700 dark:bg-green-950 dark:text-green-300',
15 information:
16 'bg-purple-100 text-purple-700 dark:bg-purple-950 dark:text-purple-300',
17 interaction: 'bg-sky-100 text-sky-700 dark:bg-sky-950 dark:text-sky-300',
18 failure: 'bg-red-100 text-red-700 dark:bg-red-950 dark:text-red-300',
19 caution:
20 'bg-amber-100 text-amber-700 dark:bg-amber-950 dark:text-amber-300',
21 },
22 },
23 defaultVariants: {
24 tone: 'neutral',
25 },
26})
27​
28type BadgeVariantsProps = VariantProps<typeof fieldMetaBadgeVariants>
29​
30export type FieldMetaBadgeKind =
31 | 'dirty'
32 | 'pristine'
33 | 'valid'
34 | 'invalid'
35 | 'invalidHidden'
36 | 'validating'
37 | 'touched'
38 | 'untouched'
39 | 'blurred'
40 | 'defaultValue'
41 | 'nonDefaultValue'
42 | 'unmounted'
43​
44const fieldMetaBadgeDefinitions: Record<
45 FieldMetaBadgeKind,
46 { label: string; tone: NonNullable<BadgeVariantsProps['tone']> }
47> = {
48 dirty: { label: 'Dirty', tone: 'change' },
49 pristine: { label: 'Pristine', tone: 'neutral' },
50 valid: { label: 'Valid', tone: 'success' },
51 invalid: { label: 'Invalid', tone: 'failure' },
52 invalidHidden: { label: 'Invalid (hidden)', tone: 'caution' },
53 validating: { label: 'Validating', tone: 'information' },
54 touched: { label: 'Touched', tone: 'interaction' },
55 untouched: { label: 'Untouched', tone: 'neutral' },
56 blurred: { label: 'Blurred', tone: 'interaction' },
57 defaultValue: { label: 'Default value', tone: 'neutral' },
58 nonDefaultValue: { label: 'Non-default value', tone: 'change' },
59 unmounted: { label: 'Not rendered', tone: 'information' },
61​
62type FieldMetaBadgeProps = { kind: FieldMetaBadgeKind } & HTMLArkProps<'span'>
63​
64export function FieldMetaBadge(props: FieldMetaBadgeProps) {
65 const [local, others] = splitProps(props, ['kind', 'class', 'children'])
66​
67 const definition = () => fieldMetaBadgeDefinitions[local.kind]
68​
69 return (
70 <Badge
71 class={cn(
72 fieldMetaBadgeVariants({ tone: definition().tone }),
73 local.class,
74 )}
75 {...others}
76 >
77 {local.children ?? definition().label}
78 </Badge>
79 )