|
| 1 | +import { cn } from '@utils/cn' |
| 2 | + |
| 3 | +import { Checkbox, RadioGroupItem, Label as ShadLabel, Text } from './' |
| 4 | + |
| 5 | +interface CompProps { |
| 6 | + children: React.ReactNode |
| 7 | + className?: string |
| 8 | +} |
| 9 | + |
| 10 | +interface RootProps { |
| 11 | + children: React.ReactNode |
| 12 | + box?: boolean |
| 13 | + shaded?: boolean |
| 14 | + className?: string |
| 15 | +} |
| 16 | + |
| 17 | +enum MessageTheme { |
| 18 | + SUCCESS = 'success', |
| 19 | + WARNING = 'warning', |
| 20 | + ERROR = 'error', |
| 21 | + DEFAULT = 'default' |
| 22 | +} |
| 23 | + |
| 24 | +interface MessageProps { |
| 25 | + children: React.ReactNode |
| 26 | + className?: string |
| 27 | + theme: MessageTheme |
| 28 | +} |
| 29 | + |
| 30 | +interface ControlProps { |
| 31 | + children: React.ReactNode |
| 32 | + type?: 'button' |
| 33 | + className?: string |
| 34 | +} |
| 35 | + |
| 36 | +type ControlType = React.ReactElement<typeof RadioGroupItem> | React.ReactElement<typeof Checkbox> |
| 37 | + |
| 38 | +interface OptionProps { |
| 39 | + control: ControlType |
| 40 | + id: string |
| 41 | + label?: string |
| 42 | + description?: string |
| 43 | + className?: string |
| 44 | +} |
| 45 | + |
| 46 | +interface LabelProps { |
| 47 | + children: React.ReactNode |
| 48 | + htmlFor?: string |
| 49 | + optional?: boolean |
| 50 | + className?: string |
| 51 | +} |
| 52 | + |
| 53 | +interface SeparatorProps { |
| 54 | + className?: string |
| 55 | + dashed?: boolean |
| 56 | + dotted?: boolean |
| 57 | +} |
| 58 | + |
| 59 | +interface SpacerProps { |
| 60 | + className?: string |
| 61 | +} |
| 62 | + |
| 63 | +const themeClassMap: Record<MessageTheme, string> = { |
| 64 | + [MessageTheme.SUCCESS]: 'text-success', |
| 65 | + [MessageTheme.WARNING]: 'text-warning', |
| 66 | + [MessageTheme.ERROR]: 'text-foreground-danger', |
| 67 | + [MessageTheme.DEFAULT]: 'text-tertiary-background' |
| 68 | +} |
| 69 | + |
| 70 | +function Root({ children, box, shaded, className }: RootProps) { |
| 71 | + return ( |
| 72 | + <fieldset |
| 73 | + className={cn( |
| 74 | + 'flex flex-col', |
| 75 | + { 'rounded-md border px-5 py-3.5 pb-5': box, 'bg-primary/[0.02]': shaded }, |
| 76 | + className |
| 77 | + )} |
| 78 | + role="group" |
| 79 | + aria-describedby="fieldset-description" |
| 80 | + > |
| 81 | + {children} |
| 82 | + </fieldset> |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +function Legend({ children, className }: CompProps) { |
| 87 | + return ( |
| 88 | + <Text size={3} weight={'medium'} className={cn('mb-0', className)} as="p" role="heading"> |
| 89 | + {children} |
| 90 | + </Text> |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +function SubLegend({ children, className }: CompProps) { |
| 95 | + return ( |
| 96 | + <Text size={2} weight={'normal'} className={cn('text-primary/70 mb-0', className)} as="p" id="fieldset-description"> |
| 97 | + {children} |
| 98 | + </Text> |
| 99 | + ) |
| 100 | +} |
| 101 | + |
| 102 | +function Item({ children, className }: CompProps) { |
| 103 | + return ( |
| 104 | + <div className={cn('item-wrapper', className)} role="presentation"> |
| 105 | + {children} |
| 106 | + </div> |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +function Label({ htmlFor, optional, children, className }: LabelProps) { |
| 111 | + return ( |
| 112 | + <ShadLabel |
| 113 | + htmlFor={htmlFor} |
| 114 | + variant="default" |
| 115 | + className={cn('text-foreground-2 leading-none font-normal mb-2.5', className)} |
| 116 | + > |
| 117 | + {children} {optional && <span className="text-foreground-7 align-top">(optional)</span>} |
| 118 | + </ShadLabel> |
| 119 | + ) |
| 120 | +} |
| 121 | + |
| 122 | +function ControlGroup({ children, type, className }: ControlProps) { |
| 123 | + return ( |
| 124 | + <div |
| 125 | + className={cn('relative flex flex-col', { 'mt-2': type === 'button' }, className)} |
| 126 | + role="group" |
| 127 | + aria-label={type === 'button' ? 'Button control group' : 'Input control group'} |
| 128 | + > |
| 129 | + {children} |
| 130 | + </div> |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +function Caption({ children, className }: CompProps) { |
| 135 | + return ( |
| 136 | + <Text |
| 137 | + className={cn('mt-2 tracking-tight leading-none', className)} |
| 138 | + as="p" |
| 139 | + size={2} |
| 140 | + color="tertiaryBackground" |
| 141 | + role="note" |
| 142 | + aria-live="polite" |
| 143 | + > |
| 144 | + {children} |
| 145 | + </Text> |
| 146 | + ) |
| 147 | +} |
| 148 | + |
| 149 | +function Message({ children, theme, className }: MessageProps) { |
| 150 | + const textClass = themeClassMap[theme] |
| 151 | + const role = theme === MessageTheme.ERROR ? 'alert' : 'status' |
| 152 | + const ariaLive = theme === MessageTheme.ERROR ? 'assertive' : 'polite' |
| 153 | + |
| 154 | + return ( |
| 155 | + <div className={cn('absolute top-full', textClass, className)} role={role} aria-live={ariaLive}> |
| 156 | + <Text as="p" size={0} className="font-light tracking-tight text-inherit"> |
| 157 | + {children} |
| 158 | + </Text> |
| 159 | + </div> |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +function Option({ control, id, label, description, className }: OptionProps) { |
| 164 | + return ( |
| 165 | + <div className={cn('flex items-start gap-x-4', className)} role="option" aria-labelledby={`${id}-label`}> |
| 166 | + {control} |
| 167 | + <div className="flex flex-col gap-0"> |
| 168 | + <Label htmlFor={id} className="font-medium"> |
| 169 | + {label} |
| 170 | + </Label> |
| 171 | + {description && ( |
| 172 | + <Text |
| 173 | + className="leading-none tracking-tight" |
| 174 | + as="p" |
| 175 | + size={2} |
| 176 | + color="tertiaryBackground" |
| 177 | + id={`${id}-description`} |
| 178 | + role="note" |
| 179 | + > |
| 180 | + {description} |
| 181 | + </Text> |
| 182 | + )} |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + ) |
| 186 | +} |
| 187 | + |
| 188 | +function Separator({ dashed, dotted, className }: SeparatorProps) { |
| 189 | + return ( |
| 190 | + <div |
| 191 | + className={cn('border-b', { 'border-dashed': dashed, 'border-dotted': dotted }, className)} |
| 192 | + role="separator" |
| 193 | + aria-orientation="horizontal" |
| 194 | + /> |
| 195 | + ) |
| 196 | +} |
| 197 | + |
| 198 | +function Spacer({ className }: SpacerProps) { |
| 199 | + return <div className={cn('mt-1', className)} role="presentation" aria-hidden="true" /> |
| 200 | +} |
| 201 | + |
| 202 | +export { Root, Legend, SubLegend, Item, Label, ControlGroup, Caption, Message, Option, Separator, Spacer, MessageTheme } |
0 commit comments