-
Notifications
You must be signed in to change notification settings - Fork 284
feat(ui5-select): add ui5-option-group component for grouped options #13913
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
Open
plamenivanov91
wants to merge
3
commits into
main
Choose a base branch
from
select-grouping-final
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import Option from "../../src/Option.js"; | ||
| import OptionGroup from "../../src/OptionGroup.js"; | ||
| import Select from "../../src/Select.js"; | ||
|
|
||
| describe("Select - OptionGroup rendering", () => { | ||
| it("renders group headers with correct text", () => { | ||
| cy.mount( | ||
| <Select id="sel"> | ||
| <OptionGroup id="g1" headerText="Oceania"> | ||
| <Option value="au">Australia</Option> | ||
| <Option value="nz">New Zealand</Option> | ||
| </OptionGroup> | ||
| <OptionGroup id="g2" headerText="Europe"> | ||
| <Option value="de">Germany</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").realClick(); | ||
| cy.get("#g1").shadow().find(".ui5-option-group-header").should("have.text", "Oceania"); | ||
| cy.get("#g2").shadow().find(".ui5-option-group-header").should("have.text", "Europe"); | ||
| }); | ||
|
|
||
| it("renders group container with role=group and aria-label", () => { | ||
| cy.mount( | ||
| <Select id="sel"> | ||
| <OptionGroup id="g1" headerText="Oceania"> | ||
| <Option value="au">Australia</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").realClick(); | ||
| cy.get("#g1").shadow().find(".ui5-option-group-root") | ||
| .should("have.attr", "role", "group") | ||
| .and("have.attr", "aria-label", "Oceania"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Select - OptionGroup selection", () => { | ||
| it("selects option inside a group by value", () => { | ||
| cy.mount( | ||
| <Select id="sel" value="de"> | ||
| <OptionGroup headerText="Oceania"> | ||
| <Option id="au" value="au">Australia</Option> | ||
| </OptionGroup> | ||
| <OptionGroup headerText="Europe"> | ||
| <Option id="de" value="de">Germany</Option> | ||
| <Option id="fr" value="fr">France</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#de").should("have.attr", "selected"); | ||
| cy.get("#au").should("not.have.attr", "selected"); | ||
| }); | ||
|
|
||
| it("selects option inside group via click", () => { | ||
| cy.mount( | ||
| <Select id="sel"> | ||
| <OptionGroup headerText="Oceania"> | ||
| <Option id="au" value="au">Australia</Option> | ||
| <Option id="nz" value="nz">New Zealand</Option> | ||
| </OptionGroup> | ||
| <OptionGroup headerText="Europe"> | ||
| <Option id="de" value="de">Germany</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").realClick(); | ||
| cy.get("#de").realClick(); | ||
| cy.get("#sel").should("have.prop", "value", "de"); | ||
| }); | ||
|
|
||
| it("fires change event when grouped option is selected", () => { | ||
| const changeSpy = cy.stub().as("changeSpy"); | ||
|
|
||
| cy.mount( | ||
| <Select id="sel" onChange={changeSpy}> | ||
| <OptionGroup headerText="Europe"> | ||
| <Option id="de" value="de">Germany</Option> | ||
| <Option id="fr" value="fr">France</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").realClick(); | ||
| cy.get("#fr").realClick(); | ||
| cy.get("@changeSpy").should("have.been.calledOnce"); | ||
| }); | ||
|
|
||
| it("arrow navigation moves through options across groups", () => { | ||
| cy.mount( | ||
| <Select id="sel"> | ||
| <OptionGroup headerText="Oceania"> | ||
| <Option id="au" value="au" selected={true}>Australia</Option> | ||
| <Option id="nz" value="nz">New Zealand</Option> | ||
| </OptionGroup> | ||
| <OptionGroup headerText="Europe"> | ||
| <Option id="de" value="de">Germany</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").shadow().find("[data-sap-focus-ref]").realClick(); | ||
| cy.get("#sel").shadow().find("[data-sap-focus-ref]").realPress("ArrowDown"); | ||
| cy.get("#nz").should("have.attr", "selected"); | ||
| cy.get("#sel").shadow().find("[data-sap-focus-ref]").realPress("ArrowDown"); | ||
| cy.get("#de").should("have.attr", "selected"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Select - OptionGroup ARIA", () => { | ||
| it("options inside groups have per-group aria-setsize and aria-posinset", () => { | ||
| cy.mount( | ||
| <Select id="sel"> | ||
| <OptionGroup headerText="Oceania"> | ||
| <Option id="au" value="au">Australia</Option> | ||
| <Option id="nz" value="nz">New Zealand</Option> | ||
| </OptionGroup> | ||
| <OptionGroup headerText="Europe"> | ||
| <Option id="de" value="de">Germany</Option> | ||
| <Option id="fr" value="fr">France</Option> | ||
| <Option id="es" value="es">Spain</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").realClick(); | ||
|
|
||
| // Oceania group: setsize=2 | ||
| cy.get("#au").shadow().find("[role='option']") | ||
| .should("have.attr", "aria-setsize", "2") | ||
| .and("have.attr", "aria-posinset", "1"); | ||
| cy.get("#nz").shadow().find("[role='option']") | ||
| .should("have.attr", "aria-setsize", "2") | ||
| .and("have.attr", "aria-posinset", "2"); | ||
|
|
||
| // Europe group: setsize=3 | ||
| cy.get("#de").shadow().find("[role='option']") | ||
| .should("have.attr", "aria-setsize", "3") | ||
| .and("have.attr", "aria-posinset", "1"); | ||
| cy.get("#es").shadow().find("[role='option']") | ||
| .should("have.attr", "aria-setsize", "3") | ||
| .and("have.attr", "aria-posinset", "3"); | ||
| }); | ||
|
|
||
| it("trigger has aria-describedby pointing to group count message when groups present", () => { | ||
| cy.mount( | ||
| <Select id="sel"> | ||
| <OptionGroup headerText="Oceania"> | ||
| <Option value="au">Australia</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").shadow().find("[role='combobox']").then($trigger => { | ||
| const describedBy = $trigger.attr("aria-describedby"); | ||
| expect(describedBy).to.include("groupCountDesc"); | ||
| }); | ||
| }); | ||
|
|
||
| it("trigger has no aria-describedby group count message when no groups", () => { | ||
| cy.mount( | ||
| <Select id="sel"> | ||
| <Option value="a">Option A</Option> | ||
| <Option value="b">Option B</Option> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").shadow().find("[role='combobox']").then($trigger => { | ||
| const describedBy = $trigger.attr("aria-describedby") ?? ""; | ||
| expect(describedBy).not.to.include("groupCountDesc"); | ||
| }); | ||
| }); | ||
|
|
||
| it("group count hidden span has correct text", () => { | ||
| cy.mount( | ||
| <Select id="sel"> | ||
| <OptionGroup headerText="Oceania"> | ||
| <Option value="au">Australia</Option> | ||
| <Option value="nz">New Zealand</Option> | ||
| </OptionGroup> | ||
| <OptionGroup headerText="Europe"> | ||
| <Option value="de">Germany</Option> | ||
| </OptionGroup> | ||
| </Select> | ||
| ); | ||
|
|
||
| cy.get("#sel").shadow().find("[id$='groupCountDesc']") | ||
| .should("contain.text", "3") | ||
| .and("contain.text", "2"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js"; | ||
| import slot from "@ui5/webcomponents-base/dist/decorators/slot-strict.js"; | ||
| import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js"; | ||
| import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js"; | ||
| import type { DefaultSlot } from "@ui5/webcomponents-base/dist/UI5Element.js"; | ||
| import createInstanceChecker from "@ui5/webcomponents-base/dist/util/createInstanceChecker.js"; | ||
| import ListItemGroup from "./ListItemGroup.js"; | ||
| import type Option from "./Option.js"; | ||
| import OptionGroupTemplate from "./OptionGroupTemplate.js"; | ||
| import { LIST_ITEM_GROUP_HEADER } from "./generated/i18n/i18n-defaults.js"; | ||
| import OptionGroupCss from "./generated/themes/OptionGroup.css.js"; | ||
|
|
||
| /** | ||
| * @class | ||
| * | ||
| * ### Overview | ||
| * | ||
| * The `ui5-option-group` component is used to group options within a `ui5-select`. | ||
| * | ||
| * ### ES6 Module Import | ||
| * | ||
| * `import "@ui5/webcomponents/dist/OptionGroup.js";` | ||
| * @constructor | ||
| * @extends ListItemGroup | ||
| * @public | ||
| * @since 2.10.0 | ||
| */ | ||
| @customElement({ | ||
| tag: "ui5-option-group", | ||
| languageAware: true, | ||
| template: OptionGroupTemplate, | ||
| styles: [OptionGroupCss], | ||
| }) | ||
| class OptionGroup extends ListItemGroup { | ||
| eventDetails!: ListItemGroup["eventDetails"]; | ||
|
|
||
| @i18n("@ui5/webcomponents") | ||
| static i18nBundle: I18nBundle; | ||
|
|
||
| /** | ||
| * Defines the options of the group. | ||
| * | ||
| * **Note:** Use the `ui5-option` component to define the desired options. | ||
| * @public | ||
| */ | ||
| @slot({ | ||
| "default": true, | ||
| invalidateOnChildChange: true, | ||
| individualSlots: true, | ||
| type: HTMLElement, | ||
| }) | ||
| items!: DefaultSlot<Option>; | ||
|
|
||
| get isOptionGroup(): boolean { | ||
| return true; | ||
| } | ||
|
|
||
| get _groupHeaderRoleDescription(): string { | ||
| return OptionGroup.i18nBundle.getText(LIST_ITEM_GROUP_HEADER); | ||
| } | ||
| } | ||
|
|
||
| OptionGroup.define(); | ||
|
|
||
| export const isInstanceOfOptionGroup = createInstanceChecker<OptionGroup>("isOptionGroup"); | ||
| export default OptionGroup; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import type OptionGroup from "./OptionGroup.js"; | ||
| import type Option from "./Option.js"; | ||
|
|
||
| export default function OptionGroupTemplate(this: OptionGroup) { | ||
| return ( | ||
| <div | ||
| class="ui5-option-group-root" | ||
| role="group" | ||
| aria-label={this.headerText} | ||
| aria-roledescription={this._groupHeaderRoleDescription} | ||
| > | ||
| {this.headerText && | ||
| <div class="ui5-option-group-header" aria-hidden="true"> | ||
| {this.headerText} | ||
| </div> | ||
| } | ||
| {this.items.map((item: Option) => <slot name={item._individualSlot}></slot>)} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be 2.26.0