1- import React , { Ref , TextareaHTMLAttributes , useContext } from 'react' ;
1+ import React , {
2+ ChangeEvent ,
3+ Ref ,
4+ TextareaHTMLAttributes ,
5+ useContext ,
6+ useRef ,
7+ } from 'react' ;
28import classnames from 'classnames' ;
39import { FormElement , FormElementProps } from './FormElement' ;
410import { FieldSetColumnContext } from './FieldSet' ;
5- import { useFormElementId } from './hooks' ;
11+ import { useEventCallback , useFormElementId } from './hooks' ;
612import { 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 */
2330export 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 } ,
0 commit comments