UNPKG

@ionic/core

Version:

Base components for Ionic

308 lines (307 loc) • 9.13 kB
import { Component, Element, Event, Host, Prop, Watch, h } from '@stencil/core'; import { getIonMode } from '../../global/ionic-global'; import { getAriaLabel, renderHiddenInput } from '../../utils/helpers'; import { createColorClasses, hostContext } from '../../utils/theme'; /** * @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use. * * @part container - The container for the checkbox mark. * @part mark - The checkmark used to indicate the checked state. */ export class Checkbox { constructor() { this.inputId = `ion-cb-${checkboxIds++}`; /** * The name of the control, which is submitted with the form data. */ this.name = this.inputId; /** * If `true`, the checkbox is selected. */ this.checked = false; /** * If `true`, the checkbox will visually appear as indeterminate. */ this.indeterminate = false; /** * If `true`, the user cannot interact with the checkbox. */ this.disabled = false; /** * The value of the checkbox does not mean if it's checked or not, use the `checked` * property for that. * * The value of a checkbox is analogous to the value of an `<input type="checkbox">`, * it's only used when the checkbox participates in a native `<form>`. */ this.value = 'on'; this.onClick = (ev) => { ev.preventDefault(); this.setFocus(); this.checked = !this.checked; this.indeterminate = false; }; this.onFocus = () => { this.ionFocus.emit(); }; this.onBlur = () => { this.ionBlur.emit(); }; } componentWillLoad() { this.emitStyle(); } checkedChanged(isChecked) { this.ionChange.emit({ checked: isChecked, value: this.value }); this.emitStyle(); } disabledChanged() { this.emitStyle(); } emitStyle() { this.ionStyle.emit({ 'checkbox-checked': this.checked, 'interactive-disabled': this.disabled, }); } setFocus() { if (this.focusEl) { this.focusEl.focus(); } } render() { const { color, checked, disabled, el, indeterminate, inputId, name, value } = this; const mode = getIonMode(this); const { label, labelId, labelText } = getAriaLabel(el, inputId); renderHiddenInput(true, el, name, (checked ? value : ''), disabled); let path = indeterminate ? h("path", { d: "M6 12L18 12", part: "mark" }) : h("path", { d: "M5.9,12.5l3.8,3.8l8.8-8.8", part: "mark" }); if (mode === 'md') { path = indeterminate ? h("path", { d: "M2 12H22", part: "mark" }) : h("path", { d: "M1.73,12.91 8.1,19.28 22.79,4.59", part: "mark" }); } return (h(Host, { onClick: this.onClick, "aria-labelledby": label ? labelId : null, "aria-checked": `${checked}`, "aria-hidden": disabled ? 'true' : null, role: "checkbox", class: createColorClasses(color, { [mode]: true, 'in-item': hostContext('ion-item', el), 'checkbox-checked': checked, 'checkbox-disabled': disabled, 'checkbox-indeterminate': indeterminate, 'interactive': true }) }, h("svg", { class: "checkbox-icon", viewBox: "0 0 24 24", part: "container" }, path), h("label", { htmlFor: inputId }, labelText), h("input", { type: "checkbox", "aria-checked": `${checked}`, disabled: disabled, id: inputId, onFocus: () => this.onFocus(), onBlur: () => this.onBlur(), ref: focusEl => this.focusEl = focusEl }))); } static get is() { return "ion-checkbox"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "ios": ["checkbox.ios.scss"], "md": ["checkbox.md.scss"] }; } static get styleUrls() { return { "ios": ["checkbox.ios.css"], "md": ["checkbox.md.css"] }; } static get properties() { return { "color": { "type": "string", "mutable": false, "complexType": { "original": "Color", "resolved": "string | undefined", "references": { "Color": { "location": "import", "path": "../../interface" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "The color to use from your application's color palette.\nDefault options are: `\"primary\"`, `\"secondary\"`, `\"tertiary\"`, `\"success\"`, `\"warning\"`, `\"danger\"`, `\"light\"`, `\"medium\"`, and `\"dark\"`.\nFor more information on colors, see [theming](/docs/theming/basics)." }, "attribute": "color", "reflect": true }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The name of the control, which is submitted with the form data." }, "attribute": "name", "reflect": false, "defaultValue": "this.inputId" }, "checked": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "If `true`, the checkbox is selected." }, "attribute": "checked", "reflect": false, "defaultValue": "false" }, "indeterminate": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "If `true`, the checkbox will visually appear as indeterminate." }, "attribute": "indeterminate", "reflect": false, "defaultValue": "false" }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "If `true`, the user cannot interact with the checkbox." }, "attribute": "disabled", "reflect": false, "defaultValue": "false" }, "value": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The value of the checkbox does not mean if it's checked or not, use the `checked`\nproperty for that.\n\nThe value of a checkbox is analogous to the value of an `<input type=\"checkbox\">`,\nit's only used when the checkbox participates in a native `<form>`." }, "attribute": "value", "reflect": false, "defaultValue": "'on'" } }; } static get events() { return [{ "method": "ionChange", "name": "ionChange", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when the checked property has changed." }, "complexType": { "original": "CheckboxChangeEventDetail", "resolved": "CheckboxChangeEventDetail<any>", "references": { "CheckboxChangeEventDetail": { "location": "import", "path": "../../interface" } } } }, { "method": "ionFocus", "name": "ionFocus", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when the checkbox has focus." }, "complexType": { "original": "void", "resolved": "void", "references": {} } }, { "method": "ionBlur", "name": "ionBlur", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when the checkbox loses focus." }, "complexType": { "original": "void", "resolved": "void", "references": {} } }, { "method": "ionStyle", "name": "ionStyle", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [{ "text": undefined, "name": "internal" }], "text": "Emitted when the styles change." }, "complexType": { "original": "StyleEventDetail", "resolved": "StyleEventDetail", "references": { "StyleEventDetail": { "location": "import", "path": "../../interface" } } } }]; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "checked", "methodName": "checkedChanged" }, { "propName": "disabled", "methodName": "disabledChanged" }]; } } let checkboxIds = 0;