diff --git a/cypress/component/SelectPreview.spec.tsx b/cypress/component/SelectPreview.spec.tsx new file mode 100644 index 0000000000..76c20a2cdb --- /dev/null +++ b/cypress/component/SelectPreview.spec.tsx @@ -0,0 +1,948 @@ +import * as React from 'react'; +import * as h from '../helpers'; +// Left Examples +import { + DefaultLeft, + AlertLeft, + ErrorLeft, + DisabledLeft, + ScrollableLeft, +} from '../../modules/preview-react/select/stories/examples/Left Label'; +// Top Examples +import { + Default, + Alert, + Error, + Disabled, + Scrollable, +} from '../../modules/preview-react/select/stories/examples/Top Label'; +import { + AccessibilityTest, + DisabledOptionsTest, + PortalTest, +} from '../../modules/preview-react/select/stories/stories_CypressTesting'; + +function getAssistiveFocus($menu: JQuery): JQuery { + const activeId = $menu.attr('aria-activedescendant'); + return $menu.find(`[id="${activeId}"]`); +} + +function assertOptionInView($option: JQuery) { + const option = $option[0]; + const $menu = $option.parent(); + const menu = $menu[0]; + + const menuBorderTopWidth = parseInt($menu.css('borderTopWidth'), 10); + + const menuViewTop = menu.scrollTop + menuBorderTopWidth; + const menuViewBottom = menuViewTop + menu.clientHeight; + const optionTop = option.offsetTop; + const optionBottom = optionTop + option.offsetHeight; + + const optionInView = optionTop >= menuViewTop && optionBottom <= menuViewBottom; + + expect(optionInView).to.equal(true); +} + +function assertOptionCenteredInView($option: JQuery) { + const option = $option[0]; + const $menu = $option.parent(); + const menu = $menu[0]; + + const menuBorderTopWidth = parseInt($menu.css('borderTopWidth'), 10); + + const expectedMenuScrollTop = Math.floor( + option.offsetTop - menu.clientHeight / 2 - menuBorderTopWidth + option.clientHeight / 2 + ); + + expect(menu.scrollTop).to.equal(expectedMenuScrollTop); +} + +describe('Select', () => { + [Default, Alert, Error, DefaultLeft, AlertLeft, ErrorLeft].forEach(Story => { + context(`given the "${Story.name}" story is rendered`, () => { + beforeEach(() => { + cy.mount(); + }); + + it('should not have any axe errors', () => { + cy.checkA11y(); + }); + + context('when the select button is clicked', () => { + beforeEach(() => { + cy.findByLabelText('Label').click(); + }); + + it('should not have any axe errors', () => { + cy.checkA11y('[role="listbox"]', { + rules: { + 'aria-input-field-name': {enabled: false}, + 'scrollable-region-focusable': {enabled: false}, + }, + }); + }); + + context('the select button', () => { + it('should have an aria-expanded attribute set to "true"', () => { + cy.findByLabelText('Label').should('have.attr', 'aria-expanded', 'true'); + }); + }); + + context('the menu', () => { + it('should be visible', () => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).should('be.visible'); + }); + + it('should have focus', () => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).should('be.focused'); + }); + + it('should have an aria-activedescendant attribute with the same value as the id of the first option ("E-mail")', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .should($menu => { + const menuAD = $menu.attr('aria-activedescendant'); + const optionId = $menu.find(`[role=option]:eq(0)`).attr('id'); + + expect(menuAD).to.equal(optionId); + }); + }); + + it('should set assistive focus to the first option ("E-mail")', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'E-mail'); + }); + }); + + context('the first option ("Mail")', () => { + it('should have an aria-selected attribute set to "true"', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getOption(0)) + .should('have.attr', 'aria-selected', 'true'); + }); + }); + + context(`when the "Phone" option (with the value "phone") is clicked`, () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.getOption('Phone')).click(); + }); + + context('the select button', () => { + it(`should read "Phone"`, () => { + cy.findByLabelText('Label').should('have.text', 'Phone'); + }); + + it(`should have a value of "phone"`, () => { + cy.findByLabelText('Label').should('have.value', 'phone'); + }); + + it(`should re-acquire focus`, () => { + cy.findByLabelText('Label').should('be.focused'); + }); + }); + + context('the menu', () => { + it('should not be visible', () => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).should('not.exist'); + }); + }); + + context('when the menu is opened again', () => { + beforeEach(() => { + cy.findByLabelText('Label').click(); + }); + + context('the menu', () => { + it('should set assistive focus to the "Phone" option', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Phone'); + }); + }); + + context('the "Phone" option', () => { + it('should have an aria-selected attribute set to "true"', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getOption('Phone')) + .should('have.attr', 'aria-selected', 'true'); + }); + }); + }); + }); + }); + + context('when the select button is focused', () => { + beforeEach(() => { + cy.findByLabelText('Label').focus(); + }); + + context('when the down arrow key is pressed', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}'); + }); + + context('the select button', () => { + it('should have an aria-expanded attribute set to "true"', () => { + cy.findByLabelText('Label').should('have.attr', 'aria-expanded', 'true'); + }); + }); + + context('the menu', () => { + it('should be visible', () => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).should('be.visible'); + }); + + it('should have focus', () => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).should('be.focused'); + }); + }); + + context('when the down arrow key is pressed for a second time', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to the "Phone" option', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Phone'); + }); + }); + + context('when the down arrow key is pressed for a third time', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to the "Mail" option', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Mail'); + }); + }); + + context('when the enter key is pressed', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).realType('{enter}'); + }); + + context('the select button', () => { + it(`should read "Mail"`, () => { + cy.findByLabelText('Label').should('have.text', 'Mail'); + }); + + it(`should have a value of "mail"`, () => { + cy.findByLabelText('Label').should('have.value', 'mail'); + }); + + it(`should re-acquire focus`, () => { + cy.findByLabelText('Label').should('be.focused'); + }); + }); + + context('the menu', () => { + it('should not be visible', () => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).should('not.exist'); + }); + }); + + context('when the menu is expanded again', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to the "Mail" option', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Mail'); + }); + }); + + context('the "Mail" option', () => { + it('should have an aria-selected attribute set to "true"', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getOption('Mail')) + .should('have.attr', 'aria-selected', 'true'); + }); + }); + }); + }); + }); + + context('when the up arrow key is pressed', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).realType('{uparrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to the "E-mail" option', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'E-mail'); + }); + }); + }); + }); + }); + + context('when the enter key is pressed', () => { + beforeEach(() => { + cy.findByLabelText('Label').realType('{enter}'); + }); + + context('the select button', () => { + it('should have an aria-expanded attribute set to "true"', () => { + cy.findByLabelText('Label').should('have.attr', 'aria-expanded', 'true'); + }); + }); + }); + + context('when the space key is pressed', () => { + beforeEach(() => { + cy.findByLabelText('Label').type(' ', {force: true}); // disable event.preventDefault checks + }); + + context('the select button', () => { + it('should have an aria-expanded attribute set to "true"', () => { + cy.findByLabelText('Label').should('have.attr', 'aria-expanded', 'true'); + }); + }); + }); + }); + + context('when the "select" helper is used to select "Mail"', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.select('Mail')); + }); + + it('should have a value of "mail"', () => { + cy.findByLabelText('Label').should('have.value', 'mail'); + }); + }); + + context('when the "select" helper is used to select /^Mail$/', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.select(/^Mail$/)); + }); + + it('should have a value of "mail"', () => { + cy.findByLabelText('Label').should('have.value', 'mail'); + }); + }); + }); + }); + + context(`given the "Default" story is rendered`, () => { + beforeEach(() => { + cy.mount(); + }); + + context('when the menu is opened', () => { + beforeEach(() => { + cy.findByLabelText('Label').focus().realType('{downarrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to the first option ("E-mail")', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'E-mail'); + }); + }); + + context('when focus is advanced to the second option ("Phone")', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to the second option ("Phone")', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Phone'); + }); + }); + + context( + 'when the menu is closed WITHOUT selecting the newly focused option ("Phone")', + () => { + beforeEach(() => { + cy.focused().realType('{esc}'); + }); + + context('when the menu is re-opened AFTER it has fully closed', () => { + beforeEach(() => { + // Wait for menu to fully close before we open it again (so we + // don't interrupt the menu's closing animation and cause it to + // re-open while it's in the middle of closing) + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).should('not.exist'); + cy.findByLabelText('Label').focus().realType('{downarrow}'); + }); + + context('the menu', () => { + it('should have reset assistive focus to the first option ("E-mail")', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'E-mail'); + }); + }); + }); + + context('when the menu is re-opened BEFORE it has fully closed', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}'); + }); + + context('the menu', () => { + it('should still have assistive focus set to the second option ("Phone")', () => { + // Focus is shifting between the button and menu as we close + // and open the menu. It's important that we use getMenu rather + // than cy.focused() to ensure we obtain a reference to the menu. + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Phone'); + }); + }); + }); + } + ); + }); + }); + }); + + [Disabled, DisabledLeft].forEach(Story => { + context(`given the "${Story.name}" story is rendered`, () => { + beforeEach(() => { + cy.mount(); + }); + + it('should not have any axe errors', () => { + cy.checkA11y(); + }); + + context('the select button', () => { + it('should be disabled', () => { + cy.findByLabelText('Label').should('be.disabled'); + }); + }); + }); + }); + + context('given the "Disabled Options Test" story is rendered', () => { + beforeEach(() => { + cy.mount(); + }); + + context('when the menu is opened', () => { + beforeEach(() => { + cy.findByLabelText('Label (Disabled Options)').focus().realType('{downarrow}'); + }); + + context('the "Carrier Pigeon" option', () => { + it('should have an aria-disabled attribute set to "true"', () => { + cy.findByLabelText('Label (Disabled Options)') + .pipe(h.selectPreview.getOption('Carrier Pigeon')) + .should('have.attr', 'aria-disabled', 'true'); + }); + }); + + context('when the down arrow key is pressed', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to first enabled option ("E-mail")', () => { + cy.findByLabelText('Label (Disabled Options)') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'E-mail'); + }); + }); + + context('when the up arrow key is pressed', () => { + beforeEach(() => { + cy.focused().realType('{uparrow}'); + }); + + context('the menu', () => { + it('should retain assistive focus on the "E-mail" option since the previous option ("Carrier Pigeon", which also happens to be the first option) is disabled', () => { + cy.findByLabelText('Label (Disabled Options)') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'E-mail'); + }); + }); + }); + + context('when the down arrow key is pressed 2 more times', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}{downarrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to the third option down ("Mail") since focus will have skipped one disabled option ("Fax")', () => { + cy.findByLabelText('Label (Disabled Options)') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Mail'); + }); + }); + + context('when the down arrow key is pressed 2 more times', () => { + beforeEach(() => { + cy.focused().realType('{downarrow}{downarrow}'); + }); + + context('the menu', () => { + it('should set assistive focus to the first option down ("Mobile Phone") since the second option down ("Telegram", which also happens to be the last option) is disabled', () => { + cy.findByLabelText('Label (Disabled Options)') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Mobile Phone'); + }); + }); + }); + }); + }); + + context('when the Home key is pressed', () => { + beforeEach(() => { + cy.focused().realType('{home}'); + }); + + context('the menu', () => { + it('should set assistive focus to the first enabled option ("E-mail")', () => { + cy.findByLabelText('Label (Disabled Options)') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'E-mail'); + }); + }); + }); + + context('when the End key is pressed', () => { + beforeEach(() => { + cy.focused().realType('{end}'); + }); + + context('the menu', () => { + it('should set assistive focus to the last enabled option ("Mobile Phone")', () => { + cy.findByLabelText('Label (Disabled Options)') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Mobile Phone'); + }); + }); + }); + }); + }); + + [Scrollable, ScrollableLeft].forEach(Story => { + context(`given the "${Story.name}" story is rendered`, () => { + beforeEach(() => { + cy.mount(); + }); + + context('when the select button is focused', () => { + beforeEach(() => { + cy.findByLabelText('Label').focus(); + }); + + context( + 'when a character is typed (provided no other characters have been typed in the last 500ms), the select should select the first matching option beyond the currently selected option (cycling back to the beginning of the options if necessary)', + () => { + context('when "s" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').realType('s'); + }); + + context('the select button', () => { + it('should read the first option beginning with "s" ("San Francisco (United States)")', () => { + cy.findByLabelText('Label').should('have.text', 'San Francisco (United States)'); + }); + + it(`should have a value of "san-francisco"`, () => { + cy.findByLabelText('Label').should('have.value', 'san-francisco'); + }); + }); + }); + + context('when "s{500ms delay}s" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').realType('ss', {delay: 500}); + }); + + context('the select button', () => { + it('should read the second option beginning with "s" ("San Mateo (United States)")', () => { + cy.findByLabelText('Label').should('have.text', 'San Mateo (United States)'); + }); + }); + }); + + context('when "s{500ms delay}d" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').realType('sd', {delay: 500}); + }); + + context('the select button', () => { + it('should read the first option beginning with "d" ("Dallas (United States)")', () => { + cy.findByLabelText('Label').should('have.text', 'Dallas (United States)'); + }); + }); + }); + } + ); + + context( + 'when multiple characters are typed in rapid succession (<500ms between keystrokes), thus forming a string, and multiple options begin with that string, the select should retain the currently selected option for as long as possible (instead of cycling selection between matching options with each keystroke)', + () => { + context('when "sa" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').realType('sa'); + }); + + context('the select button', () => { + it('should read "San Francisco (United States)"', () => { + cy.findByLabelText('Label').should('have.text', 'San Francisco (United States)'); + }); + }); + }); + + context('when "san " is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').realType('san '); + }); + + context('the select button', () => { + it('should read "San Francisco (United States)"', () => { + cy.findByLabelText('Label').should('have.text', 'San Francisco (United States)'); + }); + }); + }); + + context('when "san m" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').realType('san m'); + }); + + context('the select button', () => { + it('should read "San Mateo (United States)"', () => { + cy.findByLabelText('Label').should('have.text', 'San Mateo (United States)'); + }); + }); + }); + } + ); + + // TODO: Figure out why this test doesn't open the menu when the + // space key is pressed when using Firefox with Cypress (pressing + // the space key in the middle of a typeahead string in normal + // usage of Firefox opens the menu, see SelectBase) + // Ensure Firefox doesn't display the menu if there's a space in the + // typeahead string + // context('when "san " is typed', () => { + // beforeEach(() => { + // cy.findByLabelText('Label').realType('san '); + // }); + + // context('the menu', () => { + // it('should not be visible', () => { + // cy.findByLabelText('Label') + // .pipe(h.selectPreview.getMenu) + // .should('not.exist'); + // }); + // }); + // }); + }); + + context('when the menu is opened', () => { + beforeEach(() => { + cy.findByLabelText('Label').click(); + }); + + context( + 'when a character is typed (provided no other characters have been typed in the last 500ms), the select should advance assistive focus to the first matching option beyond the currently selected option (cycling back to the beginning of the options if necessary) and scroll that option into view', + () => { + context('when "s" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).realType('s'); + }); + + context('the menu', () => { + it('should set assistive focus to the first option beginning with "s" ("San Francisco (United States)")', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'San Francisco (United States)'); + }); + + it('should scroll so that the "San Francisco (United States)" option is fully visible', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should(assertOptionInView); + }); + }); + }); + + context('when "s{500ms delay}s" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .realType('ss', {delay: 500}); + }); + + context('the menu', () => { + it('should set assistive focus to the second option beginning with "s" ("San Mateo (United States)")', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'San Mateo (United States)'); + }); + + it('should scroll so that the "San Mateo (United States)" option is fully visible', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should(assertOptionInView); + }); + }); + }); + + context('when "s{500ms delay}d" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .realType('sd', {delay: 500}); + }); + + context('the menu', () => { + it('should set assistive focus to the first option beginning with "d" ("Dallas (United States)")', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'Dallas (United States)'); + }); + + it('should scroll so that the "Dallas (United States)" option is fully visible', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should(assertOptionInView); + }); + }); + }); + + context('when "the onto" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).realType('the onto'); + }); + + context('the menu', () => { + it('should set assistive focus to "The Ontologically..."', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should( + 'have.text', + 'The Ontologically Anthropocentric Sensory Immersive Simulation (Virtual Reality)' + ); + }); + + it('should scroll so that the "The Ontologically..." (text wrapped) option is fully visible', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should(assertOptionInView); + }); + }); + }); + } + ); + + context( + 'when multiple characters are typed in rapid succession (<500ms between keystrokes), thus forming a string, and multiple options begin with that string, the select should retain assistive focus on the currently focused option for as long as possible (instead of cycling focus between matching options with each keystroke)', + () => { + context('when "sa" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).realType('sa'); + }); + + context('the menu', () => { + it('should set assistive focus to the "San Francisco (United States)" option', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'San Francisco (United States)'); + }); + }); + }); + + context('when "san " is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).realType('san '); + }); + + context('the menu', () => { + it('should set assistive focus to the "San Francisco (United States)" option', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'San Francisco (United States)'); + }); + }); + }); + + context('when "san m" is typed', () => { + beforeEach(() => { + cy.findByLabelText('Label').pipe(h.selectPreview.getMenu).realType('san m'); + }); + + context('the menu', () => { + it('should set assistive focus to the "San Mateo (United States)" option', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should('have.text', 'San Mateo (United States)'); + }); + }); + }); + } + ); + }); + + context( + 'when the menu is opened and the selected option is initially out of view, the menu should scroll the selected option into view and center it if possible', + () => { + context('when "Dallas (United States)" is selected and the menu is opened', () => { + beforeEach(() => { + cy.findByLabelText('Label').focus().type('d').click(); + }); + + context('the menu', () => { + it('should scroll so that the "Dallas (United States)" option is centered in view', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should(assertOptionCenteredInView); + }); + }); + }); + + context( + 'when "The Ontologically..." (text wrapped) is selected and the menu is opened', + () => { + beforeEach(() => { + cy.findByLabelText('Label').focus().type('the onto').click(); + }); + + context('the menu', () => { + it('should scroll so that the "The Ontologically..." option is centered in view', () => { + cy.findByLabelText('Label') + .pipe(h.selectPreview.getMenu) + .pipe(getAssistiveFocus) + .should(assertOptionCenteredInView); + }); + }); + } + ); + } + ); + }); + + context(`given the "Portal Test" story is rendered`, () => { + beforeEach(() => { + cy.mount(); + }); + + context('when the page is scrolled to the bottom', () => { + beforeEach(() => { + cy.scrollTo('bottom'); + cy.window().its('scrollY').as('originalWindowScrollY'); + }); + + context('when the bottommost select button is clicked', () => { + beforeEach(() => { + cy.findByLabelText('Label (Bottom)').click(); + }); + + context('the page', () => { + it('should not scroll', () => { + cy.window().then($window => { + cy.get('@originalWindowScrollY').should('equal', Math.floor($window.scrollY)); + }); + }); + }); + }); + + context( + `when the blur test button is clicked and then the bottommost select button (which is re-rendered by the test button's blur handler) is clicked`, + () => { + beforeEach(() => { + cy.findByTestId('blur-test-button').click(); + cy.findByLabelText('Label (Bottom)').click(); + }); + + context('the page', () => { + it('should not scroll', () => { + cy.window().then($window => { + cy.get('@originalWindowScrollY').should('equal', Math.floor($window.scrollY)); + }); + }); + }); + } + ); + }); + }); + + context(`given the "Accessibility Test" story is rendered`, () => { + beforeEach(() => { + cy.mount(); + }); + + context('when the select button with aria-required set to true is clicked', () => { + beforeEach(() => { + cy.findByLabelText(/Label \(aria-required\)/).click(); + }); + + context('the menu', () => { + it('should have an aria-required attribute set to "true"', () => { + cy.findByLabelText(/Label \(aria-required\)/) + .pipe(h.selectPreview.getMenu) + .should('have.attr', 'aria-required', 'true'); + }); + }); + }); + + context('when the select button with required set to true is clicked', () => { + beforeEach(() => { + cy.findByLabelText(/Label \(required\)/).click(); + }); + + context('the menu', () => { + it('should have an aria-required attribute set to "true"', () => { + cy.findByLabelText(/Label \(required\)/) + .pipe(h.selectPreview.getMenu) + .should('have.attr', 'aria-required', 'true'); + }); + }); + }); + }); + }); +}); diff --git a/modules/docs/mdx/12.0-UPGRADE-GUIDE.mdx b/modules/docs/mdx/12.0-UPGRADE-GUIDE.mdx index c4cd5af1fe..b4fcc0a656 100644 --- a/modules/docs/mdx/12.0-UPGRADE-GUIDE.mdx +++ b/modules/docs/mdx/12.0-UPGRADE-GUIDE.mdx @@ -42,7 +42,6 @@ A note to the reader: - [Deprecations](#deprecations) - [Removals](#removals) - [InputIconContainer](#inputiconcontainer) - - [Select (Preview)](#select-preview) - [Component Updates](#component-updates) - [Styling API and CSS Tokens](#styling-api-and-css-tokens) - [Avatar](#avatar) @@ -80,13 +79,6 @@ deprecated code is removed. Removals are deletions from our codebase and you can no longer consume this component. We've either promoted or replaced a component or utility. -### Select (Preview) - -**PR:** [#2796](https://github.com/Workday/canvas-kit/pull/2796) - -We've removed the `Select` component that was in Preview. Please use the compound -[Select component from Main](https://workday.github.io/canvas-kit/?path=/docs/components-inputs-select--basic). - ### InputIconContainer **PR:** [#2838](https://github.com/Workday/canvas-kit/pull/2838) diff --git a/modules/preview-react/index.ts b/modules/preview-react/index.ts index 3388de841d..d0a5f54083 100644 --- a/modules/preview-react/index.ts +++ b/modules/preview-react/index.ts @@ -6,6 +6,7 @@ export * from './menu'; export * from './pill'; export * from './radio'; export * from './segmented-control'; +export * from './select'; export * from './side-panel'; export * from './status-indicator'; export * from './text-area'; diff --git a/modules/preview-react/select/LICENSE b/modules/preview-react/select/LICENSE new file mode 100644 index 0000000000..2bb61ad23f --- /dev/null +++ b/modules/preview-react/select/LICENSE @@ -0,0 +1,126 @@ +Apache License, Version 2.0 Apache License Version 2.0, January 2004 + +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and + distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the + copyright owner or entity authorized by the copyright owner that is granting the License. "Legal + Entity" shall mean the union of the acting entity and all other entities that control, are + controlled by, or are under common control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the direction or management of such + entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an + individual or Legal Entity exercising permissions granted by this License. "Source" form shall + mean the preferred form for making modifications, including but not limited to software source + code, documentation source, and configuration files. "Object" form shall mean any form resulting + from mechanical transformation or translation of a Source form, including but not limited to + compiled object code, generated documentation, and conversions to other media types. "Work" shall + mean the work of authorship, whether in Source or Object form, made available under the License, + as indicated by a copyright notice that is included in or attached to the work (an example is + provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or + Object form, that is based on (or derived from) the Work and for which the editorial revisions, + annotations, elaborations, or other modifications represent, as a whole, an original work of + authorship. For the purposes of this License, Derivative Works shall not include works that + remain separable from, or merely link (or bind by name) to the interfaces of, the Work and + Derivative Works thereof. "Contribution" shall mean any work of authorship, including the + original version of the Work and any modifications or additions to that Work or Derivative Works + thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright + owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. + For the purposes of this definition, "submitted" means any form of electronic, verbal, or written + communication sent to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, and issue tracking + systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and + improving the Work, but excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean + Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by + Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor + hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, + sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor + hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, use, offer to sell, sell, + import, and otherwise transfer the Work, where such license applies only to those patent claims + licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or + by combination of their Contribution(s) with the Work to which such Contribution(s) was + submitted. If You institute patent litigation against any entity (including a cross-claim or + counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work + constitutes direct or contributory patent infringement, then any patent licenses granted to You + under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof + in any medium, with or without modifications, and in Source or Object form, provided that You + meet the following conditions: You must give any other recipients of the Work or Derivative Works + a copy of this License; and You must cause any modified files to carry prominent notices stating + that You changed the files; and You must retain, in the Source form of any Derivative Works that + You distribute, all copyright, patent, trademark, and attribution notices from the Source form of + the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If + the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works + that You distribute must include a readable copy of the attribution notices contained within such + NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in + at least one of the following places: within a NOTICE text file distributed as part of the + Derivative Works; within the Source form or documentation, if provided along with the Derivative + Works; or, within a display generated by the Derivative Works, if and wherever such third-party + notices normally appear. The contents of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution notices within Derivative Works that + You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such + additional attribution notices cannot be construed as modifying the License. You may add Your own + copyright statement to Your modifications and may provide additional or different license terms + and conditions for use, reproduction, or distribution of Your modifications, or for any such + Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work + otherwise complies with the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution + intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms + and conditions of this License, without any additional terms or conditions. Notwithstanding the + above, nothing herein shall supersede or modify the terms of any separate license agreement you + may have executed with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade names, trademarks, service + marks, or product names of the Licensor, except as required for reasonable and customary use in + describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor + provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT + WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, + any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or + redistributing the Work and assume any risks associated with Your exercise of permissions under + this License. + +8. Limitation of Liability. In no event and under no legal theory, whether in tort (including + negligence), contract, or otherwise, unless required by applicable law (such as deliberate and + grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for + damages, including any direct, indirect, special, incidental, or consequential damages of any + character arising as a result of this License or out of the use or inability to use the Work + (including but not limited to damages for loss of goodwill, work stoppage, computer failure or + malfunction, or any and all other commercial damages or losses), even if such Contributor has + been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works + thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, + indemnity, or other liability obligations and/or rights consistent with this License. However, in + accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, + not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each + Contributor harmless for any liability incurred by, or claims asserted against, such Contributor + by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +©2020. Workday, Inc. All rights reserved. Workday and the Workday logo are registered trademarks of +Workday, Inc. All other brand and product names are trademarks or registered trademarks of their +respective holders. + +Licensed 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 CONDITIONS OF ANY KIND, either express or +implied. See the License for the specific language governing permissions and limitations under the +License. diff --git a/modules/preview-react/select/README.MD b/modules/preview-react/select/README.MD new file mode 100644 index 0000000000..dc196ee7e1 --- /dev/null +++ b/modules/preview-react/select/README.MD @@ -0,0 +1,236 @@ +# Canvas Kit Select (with Canvas-styled Menu) + + + PREVIEW: Beta + This component is in Preview because it has not been converted to a [Compound Component](../../docs/mdx/COMPOUND_COMPONENTS.mdx) yet. + +A Canvas-styled Select with a Canvas-styled menu. This is a +[_controlled_](https://reactjs.org/docs/forms.html#controlled-components) component. + +Undocumented props (`disabled`, `name`, etc.) should behave similarly to how you would expect from a +standard `; +} +``` + +#### Accessible Example + +```tsx +import * as React from 'react'; +import {Select} from '@workday/canvas-kit-preview-react/select'; +import {FormField} from '@workday/canvas-kit-react/form-field'; + +function Example() { + const options = [ + {label: 'E-mail', value: 'email'}, + {label: 'Phone', value: 'phone'}, + {label: 'Fax (disabled)', value: 'fax', disabled: true}, + {label: 'Mail', value: 'mail'}, + ]; + + const [value, setValue] = React.useState('email'); + + const handleChange = event => { + setValue(event.currentTarget.value); + }; + + return ( + + + + ); +} +``` + +#### Example with Custom Options Data + +Each option in `options` may contain a `data` object with any number of key/value pairs. This `data` +object is carried over to the `option` passed into the `renderOption` function where it may then be +used to customize how each option is rendered. + +```tsx +import * as React from 'react'; +import {Select} from '@workday/canvas-kit-preview-react/select'; +import {FormField} from '@workday/canvas-kit-react/form-field'; +import {colors, typeColors} from '@workday/canvas-kit-react/tokens'; +import { + activityStreamIcon, + avatarIcon, + uploadCloudIcon, + userIcon, +} from '@workday/canvas-system-icons-web'; +import {SystemIcon} from '@workday/canvas-kit-react/icon'; + +function Example() { + const options = [ + {value: 'Activity Stream', data: {icon: activityStreamIcon}}, + {value: 'Avatar', data: {icon: avatarIcon}}, + {value: 'Upload Cloud', data: {icon: uploadCloudIcon}}, + {value: 'User', data: {icon: userIcon}}, + ]; + + const renderOption = option => { + const iconColor = option.focused ? typeColors.inverse : colors.blackPepper100; + return ( +
+ +
{option.value}
+
+ ); + }; + + const [value, setValue] = React.useState('Activity Stream'); + + const handleChange = event => { + setValue(event.currentTarget.value); + }; + + return ( + + +``` + +## Component Props + +### Required + +#### `options: (Option | string)[]` + +> The options of the Select. `options` may be an array of objects or an array of strings. +> +> If `options` is an array of objects, each object must adhere to the `Option` interface: +> +> - `data: object` (optional) +> - `disabled: boolean` (optional) +> - `id: string` (optional, a random `id` will be assigned to the object if one isn't provided) +> - `label: string` (optional, analagous to the text content of an `