@@ -18,9 +18,11 @@ import { TooltipContent } from './TooltipContent';
1818export type FormElementProps = {
1919 id ?: string ;
2020 className ?: string ;
21+ controlId ?: string ;
2122 label ?: string ;
2223 required ?: boolean ;
2324 error ?: boolean | string | { message : string } ;
25+ errorId ?: string ;
2426 readOnly ?: boolean ;
2527 cols ?: number ;
2628 dropdown ?: JSX . Element ;
@@ -42,11 +44,13 @@ export const FormElement = createFC<
4244 const {
4345 id,
4446 className,
47+ controlId,
4548 cols = 1 ,
4649 elementRef,
4750 label,
4851 required,
4952 error,
53+ errorId,
5054 dropdown,
5155 children,
5256 readOnly,
@@ -66,13 +70,9 @@ export const FormElement = createFC<
6670 : undefined
6771 : undefined ;
6872
69- const formElementControlClassNames = classnames (
70- 'slds-form-element__control' ,
71- { 'slds-has-divider_bottom' : readOnly }
72- ) ;
73-
7473 const formElementClassNames = classnames (
7574 'slds-form-element' ,
75+ readOnly ? 'slds-form-element_readonly' : null ,
7676 error ? 'slds-has-error' : null ,
7777 typeof totalCols === 'number'
7878 ? `slds-size_${ cols } -of-${ totalCols } `
@@ -91,31 +91,47 @@ export const FormElement = createFC<
9191
9292 const emptyCtx = useMemo ( ( ) => ( { } ) , [ ] ) ;
9393
94+ const LabelTag = readOnly ? 'span' : 'label' ;
95+
9496 return (
9597 < FieldSetColumnContext . Provider value = { emptyCtx } >
9698 < div ref = { elementRef } className = { formElementClassNames } >
9799 { label ? (
98- < label
100+ < LabelTag
101+ id = { id }
99102 className = 'slds-form-element__label'
100- htmlFor = { id }
103+ { ... ( LabelTag === 'label' ? { htmlFor : controlId } : { } ) }
101104 onClick = { id ? undefined : onClickLabel }
102105 >
103106 { required ? (
104- < abbr className = 'slds-required' title = 'required' >
107+ < abbr
108+ className = 'slds-required'
109+ title = 'required'
110+ aria-hidden = 'true'
111+ >
105112 *
106113 </ abbr >
107114 ) : undefined }
108115 { label }
109- </ label >
116+ </ LabelTag >
110117 ) : null }
111118 { tooltip ? (
112119 < TooltipContent icon = { tooltipIcon } > { tooltip } </ TooltipContent >
113120 ) : null }
114- < div ref = { controlElRef } className = { formElementControlClassNames } >
115- { children }
121+ < div ref = { controlElRef } className = 'slds-form-element__control' >
122+ { readOnly ? (
123+ < div className = 'slds-form-element__static' > { children } </ div >
124+ ) : (
125+ children
126+ ) }
116127 { dropdown }
117128 { errorMessage ? (
118- < span className = 'slds-form-element__help' > { errorMessage } </ span >
129+ < span
130+ className = 'slds-form-element__help'
131+ id = { error ? errorId : undefined }
132+ >
133+ { errorMessage }
134+ </ span >
119135 ) : undefined }
120136 </ div >
121137 </ div >
0 commit comments