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: add readonly combo box #12445

Merged
merged 6 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}