11'use client' ;
22
33import { useId } from 'react' ;
4- import { Field , useFieldErrors } from 'skyroc-form' ;
4+ import { Field , useFieldError } from 'skyroc-form' ;
55
66import { 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 >
0 commit comments