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: Toggles focusable from the keyboard #41

Merged
merged 3 commits into from
Jul 17, 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Project |Description
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Bootstrap 5 Toggle](#bootstrap-5-toggle)
- [Library Distributions](#library-distributions)
- [Demos](#demos)
- [Installation](#installation)
- [CDN](#cdn)
- [Download](#download)
Expand Down Expand Up @@ -127,6 +130,7 @@ Name |Type |Default |Description |
`style` |string | |Appends the value to the class attribute of the toggle. This can be used to apply custom styles. Refer to Custom Styles for reference.
`width` |integer |*null* |Sets the width of the toggle. if set to *null*, width will be auto-calculated.
`height` |integer |*null* |Sets the height of the toggle. if set to *null*, height will be auto-calculated.
`tabindex`|integer |0 |Sets the tabindex of the toggle.

## Methods
Methods can be used to control toggles directly.
Expand Down
26 changes: 26 additions & 0 deletions cypress/e2e/tabindex.spec.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference types="cypress" />
require('cypress-plugin-tab');
import PageModel from "../support/pagemodel";

describe("Layout feature", () => {
context("Given ECMAS bootstrap toggle interface",()=>{
testCase("ecmas");
});
context("Given jQuery bootstrap toggle interface",()=>{
testCase("jquery");
});
});

function testCase(bstInterface) {
context("When previous element is selected and tab is pressed", () => {
const data_test = 'layout';
it("Then toggle is focused", () => {
PageModel.load(bstInterface, data_test);
PageModel.getTests().each(($test) => {
cy.wrap($test)
.find("input.form-control")
.tab().should('have.class','toggle');
});
});
});
}
2 changes: 2 additions & 0 deletions cypress/support/togglemodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ToggleModel {
style: "",
width: null,
height: null,
tabindex: 0,
};

/**
Expand Down Expand Up @@ -77,6 +78,7 @@ class ToggleModel {
.should("have.class", "off")
.and("have.class",'btn-'+(options.offstyle || this.DEFAULTS.offstyle))
}
cy.wrap($toggle).should('have.attr','tabindex',(options.tabindex || this.DEFAULTS.tabindex))
if(options.width || this.DEFAULTS.width) cy.wrap($toggle).should('have.css', 'width', (options.width || this.DEFAULTS.width)+'px')
if(options.height || this.DEFAULTS.height) cy.wrap($toggle).should('have.css', 'height', (options.height || this.DEFAULTS.height)+'px')
if((options.style || this.DEFAULTS.style) != '') cy.wrap($toggle).should('have.class', options.style || this.DEFAULTS.style)
Expand Down
3 changes: 3 additions & 0 deletions js/bootstrap5-toggle.ecmas.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
style: '',
width: null,
height: null,
tabindex: 0
};
options = options || {};

Expand All @@ -42,6 +43,7 @@
style: this.element.getAttribute('data-style') || options.style || DEFAULTS.style,
width: this.element.getAttribute('data-width') || options.width || DEFAULTS.width,
height: this.element.getAttribute('data-height') || options.height || DEFAULTS.height,
tabindex: this.element.getAttribute('tabindex') || options.tabindex || DEFAULTS.tabindex,
};

// LAST: Render Toggle
Expand Down Expand Up @@ -105,6 +107,7 @@
let ecmasToggle = document.createElement('div');
ecmasToggle.setAttribute('class', 'toggle btn');
ecmasToggle.classList.add(this.element.checked ? 'btn-' + this.options.onstyle : 'btn-' + this.options.offstyle);
ecmasToggle.setAttribute('tabindex',this.options.tabindex);
if (!this.element.checked) ecmasToggle.classList.add('off');
if (this.options.size) ecmasToggle.classList.add(size);
if (this.options.style) {
Expand Down
2 changes: 1 addition & 1 deletion js/bootstrap5-toggle.ecmas.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading