Skip to content

Commit

Permalink
feat: add readonly combo box (#12445)
Browse files Browse the repository at this point in the history
* feat: add readonly combo box

* fix: cursor following review

Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 6, 2022
1 parent a2f32cf commit 69ca3d6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Expand Up @@ -1179,6 +1179,9 @@ Map {
"placeholder": Object {
"type": "string",
},
"readOnly": Object {
"type": "bool",
},
"selectedItem": Object {
"args": Array [
Array [
Expand Down
20 changes: 20 additions & 0 deletions packages/react/src/components/ComboBox/ComboBox-test.js
Expand Up @@ -164,6 +164,26 @@ describe('ComboBox', () => {
});
});

describe('when readonly', () => {
it('should not let the user edit the input node', () => {
render(<ComboBox {...mockProps} readOnly={true} />);

expect(findInputNode()).toHaveAttribute('readonly');

expect(findInputNode()).toHaveDisplayValue('');

userEvent.type(findInputNode(), 'o');

expect(findInputNode()).toHaveDisplayValue('');
});

it('should not let the user expand the menu', () => {
render(<ComboBox {...mockProps} disabled={true} />);
openMenu();
expect(findListBoxNode()).not.toHaveClass('cds--list-box--expanded');
});
});

describe('downshift quirks', () => {
it('should set `inputValue` to an empty string if a false-y value is given', () => {
render(<ComboBox {...mockProps} />);
Expand Down
24 changes: 22 additions & 2 deletions packages/react/src/components/ComboBox/ComboBox.js
Expand Up @@ -91,6 +91,7 @@ const ComboBox = React.forwardRef((props, ref) => {
onInputChange,
onToggleClick, // eslint-disable-line no-unused-vars
placeholder,
readOnly,
selectedItem,
shouldFilterItem,
size,
Expand Down Expand Up @@ -203,6 +204,7 @@ const ComboBox = React.forwardRef((props, ref) => {
{
[`${prefix}--list-box--up`]: direction === 'top',
[`${prefix}--combo-box--warning`]: showWarning,
[`${prefix}--combo-box--readonly`]: readOnly,
}
);
const titleClasses = cx(`${prefix}--label`, {
Expand Down Expand Up @@ -264,7 +266,7 @@ const ComboBox = React.forwardRef((props, ref) => {
);
const labelProps = getLabelProps();
const buttonProps = getToggleButtonProps({
disabled,
disabled: disabled || readOnly,
onClick: handleToggleClick(isOpen),
// When we moved the "root node" of Downshift to the <input> for
// ARIA 1.2 compliance, we unfortunately hit this branch for the
Expand Down Expand Up @@ -308,6 +310,17 @@ const ComboBox = React.forwardRef((props, ref) => {
}
};

const readOnlyEventHandlers = readOnly
? {
onKeyDown: (evt) => {
// This prevents the select from opening for the above keys
if (evt.key !== 'Tab') {
evt.preventDefault();
}
},
}
: {};

return (
<div className={wrapperClasses}>
{titleText && (
Expand Down Expand Up @@ -341,6 +354,8 @@ const ComboBox = React.forwardRef((props, ref) => {
title={textInput?.current?.value}
{...inputProps}
{...rest}
{...readOnlyEventHandlers}
readOnly={readOnly}
ref={mergeRefs(textInput, ref)}
/>
{invalid && (
Expand All @@ -357,7 +372,7 @@ const ComboBox = React.forwardRef((props, ref) => {
<ListBoxSelection
clearSelection={clearSelection}
translateWithId={translateWithId}
disabled={disabled}
disabled={disabled || readOnly}
onClearSelection={handleSelectionClear}
/>
)}
Expand Down Expand Up @@ -540,6 +555,11 @@ ComboBox.propTypes = {
*/
placeholder: PropTypes.string,

/**
* Is the ComboBox readonly?
*/
readOnly: PropTypes.bool,

/**
* For full control of the selection
*/
Expand Down
Expand Up @@ -92,6 +92,7 @@ function ListBoxSelection({
{...rest}
aria-label={description}
className={className}
disabled={disabled}
onClick={onClick}
onKeyDown={onKeyDown}
tabIndex={disabled ? -1 : 0}
Expand Down
16 changes: 16 additions & 0 deletions packages/styles/scss/components/combo-box/_combo-box.scss
Expand Up @@ -42,4 +42,20 @@
.#{$prefix}--list-box__field {
padding: 0;
}

// readonly
.#{$prefix}--combo-box--readonly,
.#{$prefix}--combo-box--readonly:hover {
background-color: transparent;
}

.#{$prefix}--combo-box--readonly .#{$prefix}--list-box__menu-icon,
.#{$prefix}--combo-box--readonly .#{$prefix}--list-box__selection {
cursor: default;
}

.#{$prefix}--combo-box--readonly .#{$prefix}--list-box__menu-icon svg,
.#{$prefix}--combo-box--readonly .#{$prefix}--list-box__selection svg {
fill: $icon-disabled;
}
}

0 comments on commit 69ca3d6

Please sign in to comment.