Skip to content

Commit 7430dee

Browse files
authored
Merge pull request #553 from devtron-labs/fix/text-area
fix: text area controlled/uncontrolled issue
2 parents 100c634 + 540db35 commit 7430dee

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.6.2",
3+
"version": "1.6.3",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Shared/Components/Textarea/Textarea.component.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TextareaHTMLAttributes, useRef } from 'react'
1+
import { TextareaHTMLAttributes, useEffect, useRef, useState } from 'react'
22
import {
33
COMPONENT_SIZE_TYPE_TO_FONT_AND_BLOCK_PADDING_MAP,
44
COMPONENT_SIZE_TYPE_TO_INLINE_PADDING_MAP,
@@ -27,9 +27,13 @@ const Textarea = ({
2727
size = ComponentSizeType.large,
2828
ariaLabel,
2929
borderRadiusConfig,
30+
value,
3031
...props
3132
}: TextareaProps) => {
3233
const textareaRef = useRef<HTMLTextAreaElement>(null)
34+
// If the passed value remains the same, the component will behave as un-controlled
35+
// else, it behaves as controlled
36+
const [text, setText] = useState('')
3337

3438
const updateRefsHeight = (height: number) => {
3539
const refElement = textareaRef.current
@@ -57,7 +61,17 @@ const Textarea = ({
5761
updateRefsHeight(nextHeight)
5862
}
5963

60-
useThrottledEffect(reInitHeight, 300, [props.value])
64+
useEffect(() => {
65+
setText(value)
66+
}, [value])
67+
68+
useThrottledEffect(reInitHeight, 300, [text])
69+
70+
const handleChange: TextareaProps['onChange'] = (e) => {
71+
setText(e.target.value)
72+
73+
props.onChange?.(e)
74+
}
6175

6276
const handleBlur: TextareaProps['onBlur'] = (event) => {
6377
// NOTE: This is to prevent the input from being trimmed when the user do not want to trim the input
@@ -113,6 +127,8 @@ const Textarea = ({
113127
spellCheck={false}
114128
data-testid={name}
115129
required={required}
130+
value={text}
131+
onChange={handleChange}
116132
onBlur={handleBlur}
117133
onKeyDown={handleKeyDown}
118134
className={`${COMPONENT_SIZE_TYPE_TO_FONT_AND_BLOCK_PADDING_MAP[size]} ${COMPONENT_SIZE_TYPE_TO_INLINE_PADDING_MAP[size]} ${getFormFieldBorderClassName(borderRadiusConfig)} w-100 dc__overflow-auto textarea`}

src/Shared/Components/Textarea/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FormFieldWrapperProps } from '../FormFieldWrapper'
55
export interface TextareaProps
66
extends Omit<FormFieldWrapperProps, 'children' | 'inputId'>,
77
Pick<TextareaHTMLAttributes<HTMLTextAreaElement>, 'onBlur' | 'disabled' | 'autoFocus' | 'onFocus'>,
8-
Required<Pick<TextareaHTMLAttributes<HTMLTextAreaElement>, 'placeholder' | 'onChange' | 'value' | 'name'>> {
8+
Required<Pick<TextareaHTMLAttributes<HTMLTextAreaElement>, 'placeholder' | 'onChange' | 'name'>> {
99
/**
1010
* If false, the input is not trimmed on blur
1111
*
@@ -18,4 +18,8 @@ export interface TextareaProps
1818
* @default ComponentSizeType.large
1919
*/
2020
size?: Extract<ComponentSizeType, ComponentSizeType.medium | ComponentSizeType.large>
21+
/**
22+
* Value of the textarea
23+
*/
24+
value: string
2125
}

0 commit comments

Comments
 (0)