-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
626 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useRef, useState } from 'react'; | ||
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'; | ||
import { Button } from '../ui/button'; | ||
import { cn } from '~/lib/utils'; | ||
import { CalendarIcon } from 'lucide-react'; | ||
import { format } from 'date-fns'; | ||
import { Calendar } from '../ui/calendar'; | ||
import { sv } from 'date-fns/locale'; | ||
import { useField } from './form'; | ||
|
||
interface Props | ||
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'defaultValue'> { | ||
defaultValue?: Date; | ||
} | ||
export function FormDatePicker({ name, defaultValue, ...props }: Props) { | ||
const triggerRef = useRef<HTMLButtonElement>(null); | ||
const { id, errorId } = useField(); | ||
const [value, setValue] = useState(defaultValue); | ||
|
||
return ( | ||
<div> | ||
<input | ||
id={id} | ||
className='sr-only' | ||
aria-hidden | ||
aria-describedby={errorId} | ||
tabIndex={-1} | ||
name={name} | ||
defaultValue={defaultValue?.toISOString() ?? ''} | ||
onFocus={() => { | ||
triggerRef.current?.focus(); | ||
}} | ||
/> | ||
<Popover> | ||
<PopoverTrigger asChild> | ||
<Button | ||
ref={triggerRef} | ||
variant={'outline'} | ||
className={cn( | ||
'flex w-full justify-start text-left font-normal focus:ring-2 focus:ring-stone-950 focus:ring-offset-2', | ||
!value && 'text-muted-foreground' | ||
)} | ||
> | ||
<CalendarIcon className='mr-2 h-4 w-4' /> | ||
{value ? ( | ||
format(value, 'PPP', { locale: sv }) | ||
) : ( | ||
<span>Pick a date</span> | ||
)} | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className='w-auto p-0'> | ||
<Calendar | ||
mode='single' | ||
selected={value} | ||
onSelect={setValue} | ||
initialFocus | ||
/> | ||
</PopoverContent> | ||
</Popover> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, { createContext, useContext, useId } from 'react'; | ||
import { cn } from '~/lib/utils'; | ||
import { Label } from '../ui/label'; | ||
|
||
const FieldContext = createContext<{ id?: string; errorId?: string }>({}); | ||
export const useField = () => useContext(FieldContext); | ||
|
||
interface ErrorProps extends React.HTMLAttributes<HTMLDivElement> {} | ||
export function FormError({ className, ...props }: ErrorProps) { | ||
const { id } = useField(); | ||
return ( | ||
<div | ||
id={id} | ||
className={cn('text-sm font-medium text-destructive', className)} | ||
{...props} | ||
></div> | ||
); | ||
} | ||
|
||
interface LabelProps extends React.ComponentProps<typeof Label> {} | ||
export function FormLabel({ ...props }: LabelProps) { | ||
const { id } = useField(); | ||
return <Label htmlFor={id} {...props}></Label>; | ||
} | ||
|
||
interface DescriptionProps extends React.HTMLAttributes<HTMLParagraphElement> {} | ||
export function FormDescription({ className, ...props }: DescriptionProps) { | ||
const { errorId } = useField(); | ||
return ( | ||
<p | ||
id={errorId} | ||
className={cn('text-sm text-muted-foreground', className)} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
export function FormField({ | ||
className, | ||
...props | ||
}: React.HTMLAttributes<HTMLParagraphElement>) { | ||
const id = useId(); | ||
const errorId = useId(); | ||
return ( | ||
<FieldContext.Provider value={{ id, errorId }}> | ||
<div className={cn('space-y-2', className)} {...props}></div> | ||
</FieldContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Input } from '../ui/input'; | ||
import type { ComponentProps } from 'react'; | ||
import { useField } from './form'; | ||
|
||
interface Props extends ComponentProps<typeof Input> {} | ||
export function FormInput({ ...props }: Props) { | ||
const { id, errorId } = useField(); | ||
return <Input id={id} aria-describedby={errorId} {...props} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Textarea } from '~/components/ui/textarea'; | ||
import type { ComponentProps } from 'react'; | ||
import { useField } from './form'; | ||
|
||
interface Props extends ComponentProps<typeof Textarea> {} | ||
export function FormTextarea({ ...props }: Props) { | ||
const { id, errorId } = useField(); | ||
return <Textarea id={id} aria-describedby={errorId} {...props} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.