Skip to content

Commit 579c3da

Browse files
committed
feat: add error status
1 parent 9cc3653 commit 579c3da

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

packages/ui/src/components/form/FormField.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22

33
import { useId } from 'react';
4-
import { Field, useFieldErrors } from 'skyroc-form';
4+
import { Field, useFieldError } from 'skyroc-form';
55

66
import { cn } from '@/lib/utils';
77

@@ -15,7 +15,13 @@ const FormField = <Values = any,>(props: FormFieldProps<Values>) => {
1515

1616
const id = useId();
1717

18-
const errors = useFieldErrors<Values>(name);
18+
const errors = useFieldError<Values>(name);
19+
20+
const hasError = errors.length > 0;
21+
22+
const formItemId = `${id}-form-item`;
23+
const formDescriptionId = `${id}-form-item-description`;
24+
const formMessageId = `${id}-form-item-message`;
1925

2026
const {
2127
description: descriptionCls,
@@ -35,6 +41,8 @@ const FormField = <Values = any,>(props: FormFieldProps<Values>) => {
3541
<div className={mergedCls.item}>
3642
<FormLabel
3743
className={mergedCls.label}
44+
data-error={hasError}
45+
data-slot="form-label"
3846
htmlFor={id}
3947
size={size}
4048
>
@@ -43,6 +51,8 @@ const FormField = <Values = any,>(props: FormFieldProps<Values>) => {
4351

4452
<Field
4553
{...rest}
54+
aria-describedby={!hasError ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`}
55+
aria-invalid={hasError}
4656
id={id}
4757
name={name}
4858
>
@@ -52,16 +62,16 @@ const FormField = <Values = any,>(props: FormFieldProps<Values>) => {
5262
{description && (
5363
<p
5464
className={mergedCls.description}
55-
id={id}
65+
id={formDescriptionId}
5666
>
5767
{description}
5868
</p>
5969
)}
6070

61-
{errors.length > 0 && (
71+
{hasError && (
6272
<p
6373
className={mergedCls.message}
64-
id={id}
74+
id={formMessageId}
6575
>
6676
{errors[0]}
6777
</p>

packages/ui/src/components/form/form-variants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const formVariants = tv({
88
slots: {
99
description: `text-muted-foreground`,
1010
item: `form-item`,
11-
label: 'flex items-center',
11+
label: 'flex items-center data-[error=true]:text-destructive',
1212
message: `font-medium text-destructive`
1313
},
1414
variants: {

packages/ui/src/components/form/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
export { Form, useForm } from 'skyroc-form';
3+
export { Form, useFieldError, useFieldErrors, useFieldState, useForm } from 'skyroc-form';
44

55
export { default as FormField } from './FormField';
66

packages/ui/src/components/input/input-variants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ export const inputVariants = tv({
44
base: [
55
`flex w-full rounded-md border border-solid border-input bg-background`,
66
`file:border-0 file:bg-transparent file:font-medium`,
7+
'placeholder:text-muted-foreground selection:bg-primary',
78
`focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-offset-background focus-visible:ring-primary`,
8-
`disabled:cursor-not-allowed disabled:opacity-50`
9+
`disabled:cursor-not-allowed disabled:opacity-50`,
10+
`aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive`
911
],
1012
defaultVariants: {
1113
size: 'md'

0 commit comments

Comments
 (0)