Skip to content

Commit

Permalink
Field converted to tailwind
Browse files Browse the repository at this point in the history
  • Loading branch information
corbanbrook committed Jan 16, 2025
1 parent 0e629b5 commit ec4b56e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ export const Checkbox = forwardRef(
return (
<Field
disabled={disabled}
display="flex"
id={id ?? name}
label={label}
labelLocation={labelLocation}
whiteSpace="nowrap"
className="whitespace-nowrap"
>
<CheckboxPrimitive.Root
className={cn(checkboxVariants({ size }), className)}
Expand Down
41 changes: 23 additions & 18 deletions src/components/Field/Field.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { clsx } from 'clsx'
import { ElementType, ReactNode } from 'react'
import { cva, VariantProps } from 'class-variance-authority'
import { ReactNode } from 'react'

import { Box, PolymorphicComponent, PolymorphicProps } from '~/components/Box'
import { Text } from '~/components/Text'
import { cn } from '~/utils'

import * as styles from './styles.css'
const fieldVariants = cva('flex', {
variants: {
labelLocation: {
top: ['flex-col', 'items-stretch', 'gap-3'],
left: ['flex-row', 'items-center', 'gap-3', 'grid-cols-[1fr_2fr]'],
right: ['flex-row', 'items-center', 'gap-3', 'grid-cols-[2fr_1fr]'],
hidden: ['gap-0'],
},
},
})

export interface FieldProps {
export interface FieldProps extends VariantProps<typeof fieldVariants> {
id?: string
label?: string | ReactNode
description?: string | ReactNode
labelLocation?: 'left' | 'right' | 'top' | 'hidden'
disabled?: boolean
required?: boolean // TODO
error?: string // TODO
className?: string
children?: ReactNode
}

// TODO: handle error text and secondary description label
// TODO: handle isRequired in label?

export const Field: PolymorphicComponent<FieldProps, 'div'> = <
T extends ElementType,
>(
props: PolymorphicProps<FieldProps, T>
) => {
export const Field = (props: FieldProps) => {
const {
id,
label,
Expand All @@ -36,7 +42,7 @@ export const Field: PolymorphicComponent<FieldProps, 'div'> = <

const renderLabel = () =>
label || description ? (
<Box flexDirection="column" gap="0.5">
<div className="flex flex-col gap-0.5">
{label && (
<Text
variant="small"
Expand All @@ -56,19 +62,18 @@ export const Field: PolymorphicComponent<FieldProps, 'div'> = <
{description}
</Text>
)}
</Box>
</div>
) : null

return (
<Box
as="label"
className={clsx(styles.labelVariants({ labelLocation }), className)}
<label
className={cn(fieldVariants({ labelLocation }), className)}
htmlFor={id}
{...rest}
>
{['left', 'top', 'hidden'].includes(labelLocation) && renderLabel()}
{['left', 'top', 'hidden'].includes(labelLocation!) && renderLabel()}
{children}
{labelLocation === 'right' && renderLabel()}
</Box>
</label>
)
}

0 comments on commit ec4b56e

Please sign in to comment.