@19h47/checkbox 中文文档教程

发布于 3年前 浏览 27 项目主页 更新于 3年前

@19h47/checkbox

Install

yarn add @19h47/checkbox

HTML

<div role="checkbox" aria-checked="false">
    <button type="button" tabindex="-1">Do you want to click me?</button>
    <div style="display: none;">
        <input id="option" name="option" value="false" type="checkbox" />
    </div>
</div>

JavaScript

import Checkbox from '@19h47/checkbox';

const $checkbox = document.querySelector('[role="checkbox"]');
const checkbox = new Checkbox($checkbox);

checkbox.init();

Keyboard Support

KeyFunction
TabMoves keyboard focus to the checkbox.
SpaceToggles checkbox between checked and unchecked states.

Role, Property, State, and Tabindex Attributes

RoleAttributeElementUsage
h3Provides a grouping label for the group of checkboxes.
groupdivIdentifies the div element as a group container for the checkboxes.
aria-labelledbydivThe aria-labelledby attribute references the id attribute of the h3 element to define the accessible name for the group of checkboxes.
checkboxdiv
  • Identifies the div element as a checkbox.
  • The child text content of this div provides the accessible name of the checkbox.
tabindex="0"divIncludes the checkbox in the page tab sequence.
aria-checked="false"divIndicates the checkbox is not checked.
aria-checked="true"divIndicates the checkbox is checked.

Methods

MethodDescriptionArguments
activate()Activate the checkboxtrigger (optional) Whether or not the event should be trigger. Default to true
deactivate()Deactivate the checkboxtrigger (optional) Whether or not the event should be trigger. Default to true
import Checkbox from '@19h47/checkbox';

const $checkbox = document.querySelector('[role="checkbox"]');
const checkbox = new Checkbox($checkbox);

checkbox.init();

checkbox.activate();
checkbox.deactivate();

Event

Activate

import Checkbox from '@19h47/checkbox';

const $checkbox = document.querySelectorAll('[role="checkbox"]');
const checkbox = new Checkbox($checkbox);

checkbox.init();

checkbox.$input.addEventListener('activate', event => {
    const {
        target: { value },
    } = event;

    console.log(value); // Current activate value
});

Deactivate

import Checkbox from '@19h47/checkbox';

const $checkbox = document.querySelectorAll('[role="checkbox"]');
const checkbox = new Checkbox($checkbox);

checkbox.init();

checkbox.$input.addEventListener('deactivate', event => {
    const {
        target: { value },
    } = event;

    console.log(value); // Current deactivate value
});

CheckboxGroup

CheckboxGroupCheckbox 的包装类。

当用户单击一个复选框,按住 Shift 键,然后单击向下几行的另一个复选框时,应选中这两个复选框之间的所有复选框。

<div role="group">
    <div tabindex="0" role="checkbox" aria-checked="false">
        <button type="button" tabindex="-1"></button>
        Curst
        <div style="display: none;">
            <input id="curst" name="curst" value="Curst" type="checkbox" />
        </div>
    </div>

    <div tabindex="0" role="checkbox" aria-checked="false">
        <button type="button" tabindex="-1"></button>
        Doppelganger, Greater
        <div style="display: none;">
            <input
                id="doppelganger-greater"
                name="city-of-splendors[]"
                value="Doppelganger, greater"
                type="checkbox"
            />
        </div>
    </div>

    <div tabindex="0" role="checkbox" aria-checked="false">
        <button type="button" tabindex="-1"></button>
        Duhlarkin
        <div style="display: none;">
            <input id="duhlarkin" name="city-of-splendors[]" value="Duhlarkin" type="checkbox" />
        </div>
    </div>
</div>
import { CheckboxGroup } from '@19h47/checkbox';

const $element = document.querySelector('[role="group"]');
const checkboxgroup = new CheckboxGroup($element);

checkbox.init();

Example

示例位于此处,请参阅来源

References

@19h47/checkbox

Install

yarn add @19h47/checkbox

HTML

<div role="checkbox" aria-checked="false">
    <button type="button" tabindex="-1">Do you want to click me?</button>
    <div style="display: none;">
        <input id="option" name="option" value="false" type="checkbox" />
    </div>
</div>

JavaScript

import Checkbox from '@19h47/checkbox';

const $checkbox = document.querySelector('[role="checkbox"]');
const checkbox = new Checkbox($checkbox);

checkbox.init();

Keyboard Support

KeyFunction
TabMoves keyboard focus to the checkbox.
SpaceToggles checkbox between checked and unchecked states.

Role, Property, State, and Tabindex Attributes

RoleAttributeElementUsage
h3Provides a grouping label for the group of checkboxes.
groupdivIdentifies the div element as a group container for the checkboxes.
aria-labelledbydivThe aria-labelledby attribute references the id attribute of the h3 element to define the accessible name for the group of checkboxes.
checkboxdiv
  • Identifies the div element as a checkbox.
  • The child text content of this div provides the accessible name of the checkbox.
tabindex="0"divIncludes the checkbox in the page tab sequence.
aria-checked="false"divIndicates the checkbox is not checked.
aria-checked="true"divIndicates the checkbox is checked.

Methods

MethodDescriptionArguments
activate()Activate the checkboxtrigger (optional) Whether or not the event should be trigger. Default to true
deactivate()Deactivate the checkboxtrigger (optional) Whether or not the event should be trigger. Default to true
import Checkbox from '@19h47/checkbox';

const $checkbox = document.querySelector('[role="checkbox"]');
const checkbox = new Checkbox($checkbox);

checkbox.init();

checkbox.activate();
checkbox.deactivate();

Event

Activate

import Checkbox from '@19h47/checkbox';

const $checkbox = document.querySelectorAll('[role="checkbox"]');
const checkbox = new Checkbox($checkbox);

checkbox.init();

checkbox.$input.addEventListener('activate', event => {
    const {
        target: { value },
    } = event;

    console.log(value); // Current activate value
});

Deactivate

import Checkbox from '@19h47/checkbox';

const $checkbox = document.querySelectorAll('[role="checkbox"]');
const checkbox = new Checkbox($checkbox);

checkbox.init();

checkbox.$input.addEventListener('deactivate', event => {
    const {
        target: { value },
    } = event;

    console.log(value); // Current deactivate value
});

CheckboxGroup

The CheckboxGroup is a wrapper class around Checkbox.

When a user clicks a checkbox, holds Shift, and then clicks another checkbox a few rows down, all the checkboxes inbetween those two checkboxes should be checked.

<div role="group">
    <div tabindex="0" role="checkbox" aria-checked="false">
        <button type="button" tabindex="-1"></button>
        Curst
        <div style="display: none;">
            <input id="curst" name="curst" value="Curst" type="checkbox" />
        </div>
    </div>

    <div tabindex="0" role="checkbox" aria-checked="false">
        <button type="button" tabindex="-1"></button>
        Doppelganger, Greater
        <div style="display: none;">
            <input
                id="doppelganger-greater"
                name="city-of-splendors[]"
                value="Doppelganger, greater"
                type="checkbox"
            />
        </div>
    </div>

    <div tabindex="0" role="checkbox" aria-checked="false">
        <button type="button" tabindex="-1"></button>
        Duhlarkin
        <div style="display: none;">
            <input id="duhlarkin" name="city-of-splendors[]" value="Duhlarkin" type="checkbox" />
        </div>
    </div>
</div>
import { CheckboxGroup } from '@19h47/checkbox';

const $element = document.querySelector('[role="group"]');
const checkboxgroup = new CheckboxGroup($element);

checkbox.init();

Example

An example is located right here, see sources.

References

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文