From 2ccf0475ee25519c5b405ac843707bc24953e15d Mon Sep 17 00:00:00 2001 From: Ilya Evstafeev Date: Thu, 13 Jul 2023 16:39:23 +0300 Subject: [PATCH 01/10] feat(TextInput, TextArea): update props, add an option to display error via tooltip for TextInput --- src/components/controls/TextArea/TextArea.tsx | 17 ++- .../TextArea/__stories__/TextAreaShowcase.tsx | 2 +- .../TextArea/__tests__/TextArea.test.tsx | 74 ++++++++-- .../controls/TextInput/TextInput.scss | 20 +++ .../controls/TextInput/TextInput.tsx | 33 ++++- .../__stories__/TextInputShowcase.scss | 14 ++ .../__stories__/TextInputShowcase.tsx | 136 +++++++++++++++++- .../__tests__/TextInput.input.test.tsx | 103 +++++++++++-- src/components/controls/types.ts | 12 +- src/components/controls/utils.ts | 33 ++++- 10 files changed, 399 insertions(+), 45 deletions(-) diff --git a/src/components/controls/TextArea/TextArea.tsx b/src/components/controls/TextArea/TextArea.tsx index bea492e9e..82f80b369 100644 --- a/src/components/controls/TextArea/TextArea.tsx +++ b/src/components/controls/TextArea/TextArea.tsx @@ -10,7 +10,7 @@ import type { InputControlSize, InputControlView, } from '../types'; -import {getInputControlState, prepareAutoComplete} from '../utils'; +import {errorPropsMapper, getInputControlState, prepareAutoComplete} from '../utils'; import {TextAreaControl} from './TextAreaControl'; @@ -47,6 +47,8 @@ export const TextArea = React.forwardRef(functio disabled = false, hasClear = false, error, + errorMessage, + validationState, autoComplete, id: originalId, tabIndex, @@ -57,16 +59,23 @@ export const TextArea = React.forwardRef(functio onUpdate, onChange, } = props; + + const {errorMessageProp, validationStateProp} = errorPropsMapper({ + error, + errorMessage, + validationState, + }); + const [uncontrolledValue, setUncontrolledValue] = React.useState(defaultValue ?? ''); const innerControlRef = React.useRef(null); const [hasVerticalScrollbar, setHasVerticalScrollbar] = React.useState(false); - const state = getInputControlState({error}); + const state = getInputControlState(validationStateProp); const handleRef = useForkRef(props.controlRef, innerControlRef); const innerId = useUniqId(); const isControlled = value !== undefined; const inputValue = isControlled ? value : uncontrolledValue; - const isErrorMsgVisible = typeof error === 'string'; + const isErrorMsgVisible = Boolean(errorMessageProp); const isClearControlVisible = Boolean(hasClear && !disabled && inputValue); const id = originalId || innerId; @@ -156,7 +165,7 @@ export const TextArea = React.forwardRef(functio /> )} - {isErrorMsgVisible &&
{error}
} + {isErrorMsgVisible &&
{errorMessageProp}
} ); }); diff --git a/src/components/controls/TextArea/__stories__/TextAreaShowcase.tsx b/src/components/controls/TextArea/__stories__/TextAreaShowcase.tsx index 9029b456c..0af4182cc 100644 --- a/src/components/controls/TextArea/__stories__/TextAreaShowcase.tsx +++ b/src/components/controls/TextArea/__stories__/TextAreaShowcase.tsx @@ -49,7 +49,7 @@ export function TextAreaShowcase() {