Skip to content

Commit b3d9288

Browse files
authored
Merge pull request #466 from mashmatrix/support-slds-2-form-element-field-set
Update `FormElement` and `FieldSet` for SLDS2
2 parents bdeedbc + 2182d63 commit b3d9288

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

src/scripts/FieldSet.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,19 @@ export const FieldSet = createFC<
7575
{ isFormElement: boolean; Row: typeof FieldSetRow }
7676
>(
7777
({ className, label, children, ...props }) => {
78-
const fsClassNames = classnames(className, 'slds-form_compound');
78+
const fsClassNames = classnames(
79+
className,
80+
'slds-form-element',
81+
'slds-form-element_compound'
82+
);
83+
const legendClassNames = classnames(
84+
'slds-form-element__legend',
85+
'slds-form-element__label'
86+
);
7987
return (
8088
<fieldset className={fsClassNames} {...props}>
81-
{label ? (
82-
<legend className='slds-form-element__label'>{label}</legend>
83-
) : null}
84-
<div className='form-element__group'>{children}</div>
89+
{label ? <legend className={legendClassNames}>{label}</legend> : null}
90+
<div className='slds-form-element__control'>{children}</div>
8591
</fieldset>
8692
);
8793
},

src/scripts/FormElement.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import { TooltipContent } from './TooltipContent';
1818
export 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

Comments
 (0)