From 1c95f75c12288c0b866b816045eef901bb5ad34b Mon Sep 17 00:00:00 2001 From: Olga Polikashina Date: Thu, 22 Aug 2024 12:42:16 +0300 Subject: [PATCH] feat(TextInput, TextArea): add `readOnly` property (#1747) --- src/components/controls/TextArea/README.md | 1 + src/components/controls/TextArea/TextArea.tsx | 11 +++++---- .../controls/TextArea/TextAreaControl.tsx | 2 ++ .../TextArea/__stories__/TextAreaShowcase.tsx | 21 ++++++++++++++++ src/components/controls/TextInput/README.md | 3 ++- .../controls/TextInput/TextInput.tsx | 21 +++++++++------- .../controls/TextInput/TextInputControl.tsx | 2 ++ .../TextInputCustomThemeShowcase.tsx | 12 ++++++++++ .../__stories__/TextInputShowcase.tsx | 24 +++++++++++++++++++ src/components/controls/types.ts | 2 ++ 10 files changed, 85 insertions(+), 14 deletions(-) diff --git a/src/components/controls/TextArea/README.md b/src/components/controls/TextArea/README.md index 7de9b89a05..f67f008172 100644 --- a/src/components/controls/TextArea/README.md +++ b/src/components/controls/TextArea/README.md @@ -239,6 +239,7 @@ LANDING_BLOCK--> | pin | The control's border view | `string` | `"round-round"` | | placeholder | Text that appears in the control when no value is set | `string` | | | qa | Test id attribute (`data-qa`) | `string` | | +| readOnly | Indicates that the user cannot change control's value | `boolean` | `false` | | rows | The number of visible text lines for the control. If unspecified, the hight will be calculated automatically based on the content | `number` | | | size | The control's size | `"s"` `"m"` `"l"` `"xl"` | `"m"` | | tabIndex | The control's `tabindex` attribute | `string` | | diff --git a/src/components/controls/TextArea/TextArea.tsx b/src/components/controls/TextArea/TextArea.tsx index ae86d559a5..e1e1bdf809 100644 --- a/src/components/controls/TextArea/TextArea.tsx +++ b/src/components/controls/TextArea/TextArea.tsx @@ -47,13 +47,14 @@ export const TextArea = React.forwardRef( name, value, defaultValue, - disabled = false, + disabled: disabledProp, + readOnly: readOnlyProp, hasClear = false, error, errorMessage: errorMessageProp, validationState: validationStateProp, autoComplete, - id: originalId, + id: idProp, tabIndex, style, className, @@ -63,6 +64,8 @@ export const TextArea = React.forwardRef( onUpdate, onChange, } = props; + const disabled = disabledProp ?? controlProps?.disabled; + const readOnly = readOnlyProp ?? controlProps?.readOnly; const {errorMessage, validationState} = errorPropsMapper({ error, @@ -79,8 +82,8 @@ export const TextArea = React.forwardRef( const innerId = useUniqId(); const isErrorMsgVisible = validationState === 'invalid' && Boolean(errorMessage); - const isClearControlVisible = Boolean(hasClear && !disabled && inputValue); - const id = originalId || innerId; + const isClearControlVisible = Boolean(hasClear && !disabled && !readOnly && inputValue); + const id = idProp || innerId; const errorMessageId = useUniqId(); const noteId = useUniqId(); diff --git a/src/components/controls/TextArea/TextAreaControl.tsx b/src/components/controls/TextArea/TextAreaControl.tsx index ad9bbfce6d..262337928f 100644 --- a/src/components/controls/TextArea/TextAreaControl.tsx +++ b/src/components/controls/TextArea/TextAreaControl.tsx @@ -45,6 +45,7 @@ export function TextAreaControl(props: Props) { maxRows, autoFocus, disabled, + readOnly, onChange, onFocus, onBlur, @@ -112,6 +113,7 @@ export function TextAreaControl(props: Props) { onKeyUp={onKeyUp} onKeyPress={onKeyPress} disabled={disabled ?? controlProps.disabled} + readOnly={readOnly ?? controlProps.readOnly} /> ); } diff --git a/src/components/controls/TextArea/__stories__/TextAreaShowcase.tsx b/src/components/controls/TextArea/__stories__/TextAreaShowcase.tsx index 3c0a2918d1..9a8e37acdc 100644 --- a/src/components/controls/TextArea/__stories__/TextAreaShowcase.tsx +++ b/src/components/controls/TextArea/__stories__/TextAreaShowcase.tsx @@ -62,6 +62,13 @@ export function TextAreaShowcase() { />