| 38 | * @param props - The props for labels and fields. |
| 39 | */ |
| 40 | export function useLabel(props: LabelAriaProps): LabelAria { |
| 41 | let { |
| 42 | id, |
| 43 | label, |
| 44 | 'aria-labelledby': ariaLabelledby, |
| 45 | 'aria-label': ariaLabel, |
| 46 | labelElementType = 'label' |
| 47 | } = props; |
| 48 | |
| 49 | id = useId(id); |
| 50 | let labelId = useId(); |
| 51 | let labelProps = {}; |
| 52 | if (label) { |
| 53 | ariaLabelledby = ariaLabelledby ? `${labelId} ${ariaLabelledby}` : labelId; |
| 54 | labelProps = { |
| 55 | id: labelId, |
| 56 | htmlFor: labelElementType === 'label' ? id : undefined |
| 57 | }; |
| 58 | } else if (!ariaLabelledby && !ariaLabel && process.env.NODE_ENV !== 'production') { |
| 59 | console.warn( |
| 60 | 'If you do not provide a visible label, you must specify an aria-label or aria-labelledby attribute for accessibility' |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | let fieldProps = useLabels({ |
| 65 | id, |
| 66 | 'aria-label': ariaLabel, |
| 67 | 'aria-labelledby': ariaLabelledby |
| 68 | }); |
| 69 | |
| 70 | return { |
| 71 | labelProps, |
| 72 | fieldProps |
| 73 | }; |
| 74 | } |