Skip to content

Commit c92f98e

Browse files
authored
Merge pull request #472 from mashmatrix/support-slds-2-textarea
Update `Textarea` for SLDS2
2 parents 2014326 + a6aab95 commit c92f98e

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/scripts/Textarea.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, {
2+
useId,
23
ChangeEvent,
34
ReactNode,
45
Ref,
@@ -7,6 +8,7 @@ import React, {
78
useRef,
89
} from 'react';
910
import classnames from 'classnames';
11+
import { Text } from './Text';
1012
import { FormElement, FormElementProps } from './FormElement';
1113
import { FieldSetColumnContext } from './FieldSet';
1214
import { useEventCallback } from './hooks';
@@ -25,6 +27,8 @@ export type TextareaProps = {
2527
elementRef?: Ref<HTMLDivElement>;
2628
textareaRef?: Ref<HTMLTextAreaElement>;
2729
onValueChange?: (value: string, prevValue?: string) => void;
30+
readOnly?: boolean;
31+
htmlReadOnly?: boolean;
2832
} & TextareaHTMLAttributes<HTMLTextAreaElement>;
2933

3034
/**
@@ -45,6 +49,8 @@ export const Textarea = createFC<TextareaProps, { isFormElement: boolean }>(
4549
textareaRef,
4650
onChange: onChange_,
4751
onValueChange,
52+
readOnly,
53+
htmlReadOnly,
4854
...rprops
4955
} = props;
5056
const prevValueRef = useRef<string>();
@@ -54,26 +60,40 @@ export const Textarea = createFC<TextareaProps, { isFormElement: boolean }>(
5460
prevValueRef.current = e.target.value;
5561
});
5662
const { isFieldSetColumn } = useContext(FieldSetColumnContext);
57-
const taClassNames = classnames(className, 'slds-input');
58-
const textareaElem = (
63+
const errorId = useId();
64+
const taClassNames = classnames(className, 'slds-textarea');
65+
const textareaElem = readOnly ? (
66+
<Text
67+
id={id}
68+
type='regular'
69+
category='body'
70+
className='slds-form-element__static'
71+
>
72+
{rprops.value}
73+
</Text>
74+
) : (
5975
<textarea
6076
id={id}
6177
ref={textareaRef}
6278
className={taClassNames}
79+
readOnly={htmlReadOnly}
6380
{...rprops}
6481
onChange={onChange}
82+
aria-describedby={error ? errorId : undefined}
6583
/>
6684
);
6785
if (isFieldSetColumn || label || required || error || cols) {
6886
const formElemProps = {
69-
id,
87+
controlId: id,
7088
label,
7189
required,
7290
error,
91+
errorId,
7392
cols,
7493
tooltip,
7594
tooltipIcon,
7695
elementRef,
96+
readOnly,
7797
};
7898
return <FormElement {...formElemProps}>{textareaElem}</FormElement>;
7999
}

stories/Textarea.stories.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ export const ReadOnly: ComponentStoryObj<typeof Textarea> = {
123123
},
124124
};
125125

126+
/**
127+
*
128+
*/
129+
export const ReadOnlyHtml: ComponentStoryObj<typeof Textarea> = {
130+
name: 'Read only (HTML)',
131+
args: {
132+
label: 'Input Label',
133+
value: 'Textarea Only',
134+
htmlReadOnly: true,
135+
},
136+
parameters: {
137+
docs: {
138+
description: {
139+
story:
140+
'Textarea control with readOnly status (passsed to HTML &lt;input&gt; element)',
141+
},
142+
},
143+
},
144+
};
145+
126146
/**
127147
*
128148
*/

0 commit comments

Comments
 (0)