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(combobox): add pending state #4462

Merged
merged 12 commits into from
Jun 5, 2024
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ executors:
parameters:
current_golden_images_hash:
type: string
default: c5a1a42567a3c179a5e89f57c99e12c1058d682a
default: 3630c6386d7406d2884c1fd0bf2b9093832a6e67
wireit_cache_name:
type: string
default: wireit
Expand Down
22 changes: 22 additions & 0 deletions packages/combobox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ The popup menu items are filtered to only those completing the currently-input v
</sp-combobox>
```

### Quiet

```html
<sp-field-label for="color">Color</sp-field-label>
<sp-combobox id="color" quiet>
<sp-menu-item value="red">Red</sp-menu-item>
<sp-menu-item value="green">Green</sp-menu-item>
<sp-menu-item value="blue">Blue</sp-menu-item>
</sp-combobox>
```

### Pending

```html
<sp-field-label for="color">Color</sp-field-label>
<sp-combobox id="color" pending>
<sp-menu-item value="red">Red</sp-menu-item>
<sp-menu-item value="green">Green</sp-menu-item>
<sp-menu-item value="blue">Blue</sp-menu-item>
</sp-combobox>
```

## Focus and Accessibility

The combobox supports both mouse and keyboard navigation.
Expand Down
1 change: 1 addition & 0 deletions packages/combobox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@spectrum-web-components/overlay": "^0.42.4",
"@spectrum-web-components/picker-button": "^0.42.4",
"@spectrum-web-components/popover": "^0.42.4",
"@spectrum-web-components/progress-circle": "^0.42.4",
"@spectrum-web-components/textfield": "^0.42.4"
},
"devDependencies": {
Expand Down
56 changes: 49 additions & 7 deletions packages/combobox/src/Combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ governing permissions and limitations under the License.
import {
CSSResultArray,
html,
nothing,
PropertyValues,
type SpectrumElement,
TemplateResult,
Expand All @@ -26,6 +27,7 @@ import {
ifDefined,
live,
repeat,
when,
} from '@spectrum-web-components/base/src/directives.js';
import '@spectrum-web-components/overlay/sp-overlay.js';
import '@spectrum-web-components/icons-ui/icons/sp-icon-chevron100.js';
Expand Down Expand Up @@ -73,6 +75,14 @@ export class Combobox extends Textfield {
@property({ type: Boolean, reflect: true })
public open = false;

/** Whether the items are currently loading. */
@property({ type: Boolean, reflect: true })
public pending = false;

/** Defines a string value that labels the Combobox while it is in pending state. */
@property({ type: String, attribute: 'pending-label' })
public pendingLabel = 'Pending';

@query('slot:not([name])')
private optionSlot!: HTMLSlotElement;

Expand Down Expand Up @@ -120,7 +130,7 @@ export class Combobox extends Textfield {
}

public handleComboboxKeydown(event: KeyboardEvent): void {
if (this.readonly) {
if (this.readonly || this.pending) {
return;
}
if (event.altKey && event.code === 'ArrowDown') {
Expand Down Expand Up @@ -223,7 +233,7 @@ export class Combobox extends Textfield {
}

public filterAvailableOptions(): void {
if (this.autocomplete === 'none') {
if (this.autocomplete === 'none' || this.pending) {
return;
}
const valueLowerCase = this.value.toLowerCase();
Expand All @@ -237,8 +247,10 @@ export class Combobox extends Textfield {

public override handleInput(event: Event): void {
super.handleInput(event);
this.activeDescendant = undefined;
this.open = true;
if (!this.pending) {
this.activeDescendant = undefined;
this.open = true;
}
}

protected handleMenuChange(event: PointerEvent & { target: Menu }): void {
Expand All @@ -264,7 +276,7 @@ export class Combobox extends Textfield {
}

public toggleOpen(): void {
if (this.readonly) {
if (this.readonly || this.pending) {
this.open = false;
return;
}
Expand Down Expand Up @@ -311,6 +323,17 @@ export class Combobox extends Textfield {
const appliedLabel = this.label || this.appliedLabel;

return html`
${this.pending
? html`
<span
aria-hidden="true"
class="visually-hidden"
id="pending-label"
>
${this.pendingLabel}
</span>
`
: nothing}
${this.value
? html`
<span
Expand All @@ -332,6 +355,20 @@ export class Combobox extends Textfield {
`;
}

protected renderLoader(): TemplateResult {
import(
'@spectrum-web-components/progress-circle/sp-progress-circle.js'
);
return html`
<sp-progress-circle
size="s"
indeterminate
aria-hidden="true"
class="progress-circle"
></sp-progress-circle>
`;
}

protected override renderField(): TemplateResult {
return html`
${this.renderStateIcons()}
Expand All @@ -350,7 +387,7 @@ export class Combobox extends Textfield {
aria-describedby="${this.helpTextId} tooltip"
aria-expanded="${this.open ? 'true' : 'false'}"
aria-label=${ifDefined(this.label || this.appliedLabel)}
aria-labelledby="applied-label label"
aria-labelledby="pending-label applied-label label"
aria-invalid=${ifDefined(this.invalid || undefined)}
autocomplete="off"
@click=${this.toggleOpen}
Expand Down Expand Up @@ -378,6 +415,7 @@ export class Combobox extends Textfield {
?required=${this.required}
?readonly=${this.readonly}
/>
${when(this.pending, () => this.renderLoader())}
Rocss marked this conversation as resolved.
Show resolved Hide resolved
`;
}

Expand All @@ -402,6 +440,7 @@ export class Combobox extends Textfield {
: ''}"
?disabled=${this.disabled}
?focused=${this.focused}
?quiet=${this.quiet}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really related to this PR but a very straight-forward fix for the quiet state

size=${this.size}
></sp-picker-button>
<sp-overlay
Expand Down Expand Up @@ -509,12 +548,15 @@ export class Combobox extends Textfield {
this & { optionEls: MenuItem[]; activeDescendant: MenuItem }
>
): void {
if (changed.has('open')) {
if (changed.has('open') && !this.pending) {
this.manageListOverlay();
}
if (!this.focused && this.open) {
this.open = false;
}
if (changed.has('pending') && this.pending) {
this.open = false;
}
if (changed.has('activeDescendant')) {
const previouslyActiveDescendant = changed.get(
'activeDescendant'
Expand Down
16 changes: 8 additions & 8 deletions packages/combobox/src/spectrum-combobox.css
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ governing permissions and limitations under the License.
);
}

.spectrum-Combobox-progress-circle {
.progress-circle {
position: absolute;
inset-block-start: var(
--mod-combobox-block-spacing-edge-to-progress-circle,
Expand All @@ -415,8 +415,8 @@ governing permissions and limitations under the License.
);
}

.spectrum-Combobox-progress-circle:dir(rtl),
:host([dir='rtl']) .spectrum-Combobox-progress-circle {
.progress-circle:dir(rtl),
:host([dir='rtl']) .progress-circle {
inset-inline-start: calc(
var(
--mod-combobox-spacing-inline-icon-to-button,
Expand Down Expand Up @@ -708,7 +708,7 @@ governing permissions and limitations under the License.
}

:host([invalid]) #textfield #input,
#textfield.is-loading #input {
:host([pending]) #textfield #input {
padding-inline-end: calc(
var(--mod-combobox-button-width, var(--spectrum-combobox-button-width)) +
var(
Expand Down Expand Up @@ -760,9 +760,9 @@ governing permissions and limitations under the License.
);
}

:host([disabled]) .spectrum-Textfield .icon,
.spectrum-Textfield.is-loading .icon,
.spectrum-Textfield.is-readOnly .icon {
:host([disabled]) #textfield .icon,
:host([pending]) #textfield .icon,
#textfield.is-readOnly .icon {
display: none;
}

Expand Down Expand Up @@ -814,7 +814,7 @@ governing permissions and limitations under the License.
}

:host([quiet][invalid]) #textfield #input,
:host([quiet]) #textfield.is-loading #input {
:host([quiet][pending]) #textfield #input {
padding-inline-end: calc(
var(--mod-combobox-button-width, var(--spectrum-combobox-button-width)) +
var(
Expand Down
3 changes: 3 additions & 0 deletions packages/combobox/src/spectrum-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ const config = {
'size'
),
converter.classToAttribute('spectrum-Combobox--quiet'),
converter.classToClass('spectrum-Combobox-progress-circle'),
converter.classToClass('spectrum-Combobox-button'),
converter.classToAttribute('is-focused', 'focused'),
converter.classToAttribute('is-invalid', 'invalid'),
converter.classToAttribute('is-loading', 'pending'),
converter.classToAttribute(
'is-keyboardFocused',
'keyboard-focused'
Expand Down Expand Up @@ -102,6 +104,7 @@ const config = {
},
converter.classToId('spectrum-Combobox-input'),
converter.classToId('spectrum-Combobox-textfield'),
converter.classToId('spectrum-Textfield', 'textfield'),
converter.classToClass(
'spectrum-Textfield-validationIcon',
'icon'
Expand Down
88 changes: 88 additions & 0 deletions packages/combobox/stories/args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

export const argTypes = {
size: {
name: 'size',
type: { name: 'string', required: false },
table: {
defaultValue: { summary: 'm' },
},
control: {
labels: {
s: 'Small',
m: 'Medium',
l: 'Large',
xl: 'Extra large',
},
type: 'select',
},
options: ['s', 'm', 'l', 'xl'],
},
quiet: {
name: 'quiet',
type: { name: 'boolean', required: false },
table: {
type: { summary: 'boolean' },
defaultValue: { summary: false },
},
control: {
type: 'boolean',
},
},
disabled: {
name: 'disabled',
type: { name: 'boolean', required: false },
description:
'Disable this control. It will not receive focus or events.',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: false },
},
control: {
type: 'boolean',
},
},
invalid: {
name: 'invalid',
type: { name: 'boolean', required: false },
table: {
type: { summary: 'boolean' },
defaultValue: { summary: false },
},
control: {
type: 'boolean',
},
},
pending: {
name: 'pending',
type: { name: 'boolean', required: false },
table: {
type: { summary: 'boolean' },
defaultValue: { summary: false },
},
control: {
type: 'boolean',
},
},
readonly: {
name: 'readonly',
type: { name: 'boolean', required: false },
table: {
type: { summary: 'boolean' },
defaultValue: { summary: false },
},
control: {
type: 'boolean',
},
},
};
Loading
Loading