Skip to content

Commit

Permalink
feat(ComboBox): support warning state (#7733)
Browse files Browse the repository at this point in the history
* feat(ComboBox): support warning state

* docs(ComboBox): add warn prop

* chore: update snapshots

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
emyarod and kodiakhq[bot] committed Feb 9, 2021
1 parent f6ecfbf commit 70bf4b4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,12 @@ Map {
],
"type": "oneOf",
},
"warn": Object {
"type": "bool",
},
"warnText": Object {
"type": "node",
},
},
},
"ComposedModal" => Object {
Expand Down
5 changes: 5 additions & 0 deletions packages/react/src/components/ComboBox/ComboBox-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ const props = () => ({
direction: select('Dropdown direction (direction)', directions, 'bottom'),
onChange: action('onChange'),
onToggleClick: action('onClick'),
warn: boolean('Show warning state (warn)', false),
warnText: text(
'Warning state text (warnText)',
'This mode may perform worse on older machines'
),
});

export const Playground = () => (
Expand Down
31 changes: 28 additions & 3 deletions packages/react/src/components/ComboBox/ComboBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import Downshift from 'downshift';
import PropTypes from 'prop-types';
import React from 'react';
import { settings } from 'carbon-components';
import { Checkmark16, WarningFilled16 } from '@carbon/icons-react';
import {
Checkmark16,
WarningAltFilled16,
WarningFilled16,
} from '@carbon/icons-react';
import ListBox, { PropTypes as ListBoxPropTypes } from '../ListBox';
import { match, keys } from '../../internal/keyboard';
import setupGetInstanceId from '../../tools/setupGetInstanceId';
Expand Down Expand Up @@ -199,6 +203,16 @@ export default class ComboBox extends React.Component {
* Currently supports either the default type, or an inline variant
*/
type: ListBoxPropTypes.ListBoxType,

/**
* 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,
};

static defaultProps = {
Expand Down Expand Up @@ -331,10 +345,14 @@ export default class ComboBox extends React.Component {
onToggleClick, // eslint-disable-line no-unused-vars
downshiftProps,
direction,
warn,
warnText,
...rest
} = this.props;
const showWarning = !invalid && warn;
const className = cx(`${prefix}--combo-box`, containerClassName, {
[`${prefix}--list-box--up`]: direction === 'top',
[`${prefix}--combo-box--warning`]: showWarning,
});
const titleClasses = cx(`${prefix}--label`, {
[`${prefix}--label--disabled`]: disabled,
Expand Down Expand Up @@ -390,7 +408,9 @@ export default class ComboBox extends React.Component {
invalidText={invalidText}
isOpen={isOpen}
light={light}
size={size}>
size={size}
warn={warn}
warnText={warnText}>
<ListBox.Field
{...getToggleButtonProps({
disabled,
Expand Down Expand Up @@ -423,6 +443,11 @@ export default class ComboBox extends React.Component {
className={`${prefix}--list-box__invalid-icon`}
/>
)}
{showWarning && (
<WarningAltFilled16
className={`${prefix}--list-box__invalid-icon ${prefix}--list-box__invalid-icon--warning`}
/>
)}
{inputValue && (
<ListBox.Selection
clearSelection={clearSelection}
Expand Down Expand Up @@ -470,7 +495,7 @@ export default class ComboBox extends React.Component {
</ListBox.Menu>
)}
</ListBox>
{helperText && !invalid && (
{helperText && !invalid && !warn && (
<div id={comboBoxHelperId} className={helperClasses}>
{helperText}
</div>
Expand Down

0 comments on commit 70bf4b4

Please sign in to comment.