Skip to content

Commit d209140

Browse files
committed
add onValueChange handler on Textarea component
1 parent cf00999 commit d209140

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/scripts/Textarea.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import React, { Ref, TextareaHTMLAttributes, useContext } from 'react';
1+
import React, {
2+
ChangeEvent,
3+
Ref,
4+
TextareaHTMLAttributes,
5+
useContext,
6+
useRef,
7+
} from 'react';
28
import classnames from 'classnames';
39
import { FormElement, FormElementProps } from './FormElement';
410
import { FieldSetColumnContext } from './FieldSet';
5-
import { useFormElementId } from './hooks';
11+
import { useEventCallback, useFormElementId } from './hooks';
612
import { createFC } from './common';
713

814
/**
@@ -15,13 +21,21 @@ export type TextareaProps = {
1521
cols?: number;
1622
elementRef?: Ref<HTMLDivElement>;
1723
textareaRef?: Ref<HTMLTextAreaElement>;
24+
onValueChange?: (value: string, prevValue?: string) => void;
1825
} & TextareaHTMLAttributes<HTMLTextAreaElement>;
1926

2027
/**
2128
*
2229
*/
2330
export const Textarea = createFC<TextareaProps, { isFormElement: boolean }>(
2431
(props) => {
32+
const { onChange: onChange_, onValueChange } = props;
33+
const prevValueRef = useRef<string>();
34+
const onChange = useEventCallback((e: ChangeEvent<HTMLTextAreaElement>) => {
35+
onChange_?.(e);
36+
onValueChange?.(e.target.value, prevValueRef.current);
37+
prevValueRef.current = e.target.value;
38+
});
2539
const {
2640
id: id_,
2741
label,
@@ -44,18 +58,27 @@ export const Textarea = createFC<TextareaProps, { isFormElement: boolean }>(
4458
};
4559
return (
4660
<FormElement {...formElemProps}>
47-
<Textarea {...{ ...rprops, id }} />
61+
<Textarea {...{ ...rprops, id, onChange }} />
4862
</FormElement>
4963
);
5064
}
51-
const { className, textareaRef, ...rprops2 } = rprops;
65+
const {
66+
className,
67+
textareaRef,
68+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
69+
onChange: _unused_1,
70+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
71+
onValueChange: _unused_2,
72+
...rprops2
73+
} = rprops;
5274
const taClassNames = classnames(className, 'slds-input');
5375
return (
5476
<textarea
5577
id={id}
5678
ref={textareaRef}
5779
className={taClassNames}
5880
{...rprops2}
81+
onChange={onChange}
5982
/>
6083
);
6184
},

stories/Textarea.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const meta: ComponentMeta<typeof Textarea> = {
99
component: Textarea,
1010
argTypes: {
1111
onChange: { action: 'change' },
12+
onValueChange: { aciton: 'valueChange' },
1213
onBlur: { action: 'blur' },
1314
},
1415
};

0 commit comments

Comments
 (0)