Skip to content

Commit 1af6b84

Browse files
authored
Merge pull request #471 from mashmatrix/support-slds-2-input
Update `Input` for SLDS2
2 parents 399692e + 9742d12 commit 1af6b84

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/scripts/Input.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, {
2+
useId,
23
ReactElement,
34
InputHTMLAttributes,
45
KeyboardEvent,
@@ -155,32 +156,37 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
155156
prevValueRef.current = e.target.value;
156157
});
157158

159+
const prefix = useId();
160+
161+
const rawTextId = id ?? `${prefix}-raw-text-id`;
162+
const inputId = id ?? `${prefix}-input-id`;
163+
const labelForId = readOnly ? rawTextId : inputId;
164+
165+
const errorId = `${prefix}-error-id`;
166+
158167
const { isFieldSetColumn } = useContext(FieldSetColumnContext);
159168
const inputClassNames = classnames(
160169
className,
161170
bare ? 'slds-input_bare' : 'slds-input'
162171
);
163172
const inputElem = readOnly ? (
164-
<Text
165-
id={id}
166-
type='regular'
167-
category='body'
168-
className='slds-form-element__static'
169-
>
173+
<Text id={rawTextId} type='regular' category='body'>
170174
{value}
171175
</Text>
172176
) : (
173177
<input
174178
ref={inputRef}
175179
className={inputClassNames}
176-
id={id}
180+
id={inputId}
177181
type={type}
178182
value={value}
179183
defaultValue={defaultValue}
180184
readOnly={htmlReadOnly}
181185
{...rprops}
182186
onChange={onChange}
183187
onKeyDown={onKeyDown}
188+
aria-describedby={error ? errorId : undefined}
189+
aria-invalid={error ? true : undefined}
184190
/>
185191
);
186192

@@ -206,10 +212,11 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
206212
}
207213
if (isFieldSetColumn || label || required || error || cols) {
208214
const formElemProps = {
209-
id,
215+
controlId: labelForId,
210216
label,
211217
required,
212218
error,
219+
errorId,
213220
readOnly,
214221
cols,
215222
tooltip,

0 commit comments

Comments
 (0)