Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TextArea): add warning state #13293

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7833,6 +7833,8 @@ Map {
"onClick": [Function],
"placeholder": "",
"rows": 4,
"warn": false,
"warnText": "",
},
"propTypes": Object {
"className": Object {
Expand Down Expand Up @@ -7911,6 +7913,12 @@ Map {
],
"type": "oneOfType",
},
"warn": Object {
"type": "bool",
},
"warnText": Object {
"type": "node",
},
},
"render": [Function],
},
Expand Down Expand Up @@ -9971,6 +9979,12 @@ Map {
],
"type": "oneOfType",
},
"warn": Object {
"type": "bool",
},
"warnText": Object {
"type": "node",
},
},
},
"unstable__FluidTextAreaSkeleton" => Object {
Expand Down
10 changes: 10 additions & 0 deletions packages/react/src/components/FluidTextArea/FluidTextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ FluidTextArea.propTypes = {
* Provide the current value of the `<textarea>`
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/**
* Specify whether the control is currently in warning state
*/
warn: PropTypes.bool,

/**
* Provide the text that is displayed when the control is in warning state
*/
warnText: PropTypes.node,
};

export default FluidTextArea;
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,16 @@ Playground.argTypes = {
},
defaultValue: '500',
},
warn: {
control: {
type: 'boolean',
},
defaultValue: false,
},
warnText: {
control: {
type: 'text',
},
defaultValue: 'This is a warning message.',
},
};
12 changes: 12 additions & 0 deletions packages/react/src/components/TextArea/TextArea.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ Playground.argTypes = {
},
defaultValue: 4,
},
warn: {
control: {
type: 'boolean',
},
defaultValue: false,
},
warnText: {
control: {
type: 'text',
},
defaultValue: 'This is a warning message.',
},
};

Playground.args = {
Expand Down
49 changes: 47 additions & 2 deletions packages/react/src/components/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import PropTypes, { ReactNodeLike } from 'prop-types';
import React, { useState, useContext, useRef } from 'react';
import classNames from 'classnames';
import deprecate from '../../prop-types/deprecate';
import { WarningFilled } from '@carbon/icons-react';
import { WarningFilled, WarningAltFilled } from '@carbon/icons-react';
import { useFeatureFlag } from '../FeatureFlags';
import { usePrefix } from '../../internal/usePrefix';
import { FormContext } from '../FluidForm';
Expand Down Expand Up @@ -123,6 +123,16 @@ export interface TextAreaProps
* Provide the current value of the `<textarea>`
*/
value?: string | number;

/**
* Specify whether the control is currently in warning state
*/
warn?: boolean;

/**
* Provide the text that is displayed when the control is in warning state
*/
warnText?: ReactNodeLike;
}

const TextArea = React.forwardRef((props: TextAreaProps, forwardRef) => {
Expand All @@ -140,6 +150,8 @@ const TextArea = React.forwardRef((props: TextAreaProps, forwardRef) => {
placeholder,
enableCounter,
maxCount,
warn = false,
warnText,
...other
} = props;
const prefix = usePrefix();
Expand Down Expand Up @@ -214,12 +226,24 @@ const TextArea = React.forwardRef((props: TextAreaProps, forwardRef) => {
</div>
) : null;

const warning = warn ? (
<div role="alert" className={`${prefix}--form-requirement`}>
{warnText}
{isFluid && (
<WarningAltFilled
className={`${prefix}--text-area__invalid-icon ${prefix}--text-area__invalid-icon--warning`}
/>
)}
</div>
) : null;

const textareaClasses = classNames(
`${prefix}--text-area`,
[enabled ? null : className],
{
[`${prefix}--text-area--light`]: light,
[`${prefix}--text-area--invalid`]: invalid,
[`${prefix}--text-area--warn`]: warn,
}
);

Expand Down Expand Up @@ -265,19 +289,28 @@ const TextArea = React.forwardRef((props: TextAreaProps, forwardRef) => {
<div
className={classNames(`${prefix}--text-area__wrapper`, {
[`${prefix}--text-area__wrapper--readonly`]: other.readOnly,
[`${prefix}--text-area__wrapper--warn`]: warn,
})}
data-invalid={invalid || null}>
{invalid && !isFluid && (
<WarningFilled className={`${prefix}--text-area__invalid-icon`} />
)}
{warn && !invalid && !isFluid && (
<WarningAltFilled
className={`${prefix}--text-area__invalid-icon ${prefix}--text-area__invalid-icon--warning`}
/>
)}
{input}
<span className={`${prefix}--text-area__counter-alert`} role="alert">
{ariaAnnouncement}
</span>
{isFluid && <hr className={`${prefix}--text-area__divider`} />}
{isFluid && invalid ? error : null}
{isFluid && warn && !invalid ? warning : null}
</div>
{invalid && !isFluid ? error : helper}
{!invalid && !warn && !isFluid ? helper : null}
{invalid && !isFluid ? error : null}
{warn && !invalid && !isFluid ? warning : null}
</div>
);
});
Expand Down Expand Up @@ -387,6 +420,16 @@ TextArea.propTypes = {
* Provide the current value of the `<textarea>`
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/**
* Specify whether the control is currently in warning state
*/
warn: PropTypes.bool,

/**
* Provide the text that is displayed when the control is in warning state
*/
warnText: PropTypes.node,
};

TextArea.defaultProps = {
Expand All @@ -400,6 +443,8 @@ TextArea.defaultProps = {
helperText: '',
enableCounter: false,
maxCount: undefined,
warn: false,
warnText: '',
};

export default TextArea;
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,55 @@
}

.#{$prefix}--text-area--fluid
.#{$prefix}--text-area--invalid
+ .#{$prefix}--text-area__divider {
.#{$prefix}--text-area__wrapper[data-invalid]
.#{$prefix}--text-area__divider,
.#{$prefix}--text-area--fluid
.#{$prefix}--text-area__wrapper--warn
.#{$prefix}--text-area__divider {
display: block;
border-style: solid;
border-color: $border-subtle;
border-bottom: none;
margin: 0 1rem;
}

// invalid error message container
// invalid & warning error message container
.#{$prefix}--text-area--fluid
.#{$prefix}--text-area--invalid
+ .#{$prefix}--text-area__divider
+ .#{$prefix}--form-requirement {
.#{$prefix}--text-area__wrapper[data-invalid]
.#{$prefix}--form-requirement.#{$prefix}--form-requirement,
.#{$prefix}--text-area--fluid
.#{$prefix}--text-area__wrapper--warn
.#{$prefix}--form-requirement.#{$prefix}--form-requirement {
position: relative;
display: block;
overflow: visible;
max-height: 12.5rem;
padding: 0.5rem 2.5rem 0.5rem 1rem;
margin: 0;
background: $field;
}

.#{$prefix}--text-area--fluid
.#{$prefix}--text-area__wrapper--warn
.#{$prefix}--form-requirement.#{$prefix}--form-requirement {
border-bottom: 1px solid $border-strong;
color: $text-primary;
}

.#{$prefix}--text-area--fluid
.#{$prefix}--text-area__wrapper[data-invalid]
.#{$prefix}--form-requirement.#{$prefix}--form-requirement {
color: $text-error;
}

.#{$prefix}--text-area--fluid .#{$prefix}--text-area--warn {
border-bottom: none;
}

.#{$prefix}--modal
.#{$prefix}--text-area--fluid
.#{$prefix}--text-area--invalid
+ .#{$prefix}--text-area__divider
.#{$prefix}--text-area__wrapper[data-invalid]
.#{$prefix}--text-area__divider
+ .#{$prefix}--form-requirement {
background: $field-02;
}
Expand Down
1 change: 1 addition & 0 deletions packages/styles/scss/components/form/_form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ $input-label-weight: 400 !default;
.#{$prefix}--text-input__field-wrapper--warning,
.#{$prefix}--text-input__field-wrapper--warning > .#{$prefix}--text-input,
.#{$prefix}--text-area__wrapper[data-invalid],
.#{$prefix}--text-area__wrapper--warn,
.#{$prefix}--select-input__wrapper[data-invalid],
.#{$prefix}--select--warning .#{$prefix}--select-input__wrapper,
.#{$prefix}--time-picker[data-invalid],
Expand Down
10 changes: 10 additions & 0 deletions packages/styles/scss/components/text-area/_text-area.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

@use '../form';
@use '../../colors' as *;
@use '../../config' as *;
@use '../../motion' as *;
@use '../../spacing' as *;
Expand Down Expand Up @@ -71,6 +72,15 @@
fill: $support-error;
}

.#{$prefix}--text-area__invalid-icon--warning {
fill: $support-warning;
}

.#{$prefix}--text-area__invalid-icon--warning path[fill] {
fill: $black-100;
opacity: 1;
}

.#{$prefix}--text-area__counter-alert {
position: absolute;
overflow: hidden;
Expand Down