移动目录
This commit is contained in:
137
mdui_patched/components/checkbox/index.d.ts
vendored
Normal file
137
mdui_patched/components/checkbox/index.d.ts
vendored
Normal file
@@ -0,0 +1,137 @@
|
||||
import { MduiElement } from '@mdui/shared/base/mdui-element.js';
|
||||
import '@mdui/shared/icons/check-box-outline-blank.js';
|
||||
import '@mdui/shared/icons/check-box.js';
|
||||
import '@mdui/shared/icons/indeterminate-check-box.js';
|
||||
import '../icon.js';
|
||||
import type { Ripple } from '../ripple/index.js';
|
||||
import type { FormControl } from '@mdui/jq/shared/form.js';
|
||||
import type { TemplateResult, CSSResultGroup } from 'lit';
|
||||
declare const Checkbox_base: import("@lit/reactive-element/decorators/base.js").Constructor<import("../ripple/ripple-mixin.js").RippleMixinInterface> & import("@lit/reactive-element/decorators/base.js").Constructor<import("@mdui/shared/mixins/focusable.js").FocusableMixinInterface> & typeof MduiElement;
|
||||
/**
|
||||
* @summary 复选框组件
|
||||
*
|
||||
* ```html
|
||||
* <mdui-checkbox>Checkbox</mdui-checkbox>
|
||||
* ```
|
||||
*
|
||||
* @event focus - 获得焦点时触发
|
||||
* @event blur - 失去焦点时触发
|
||||
* @event change - 选中状态变更时触发
|
||||
* @event input - 选中状态变更时触发
|
||||
* @event invalid - 表单字段验证未通过时触发
|
||||
*
|
||||
* @slot - 复选框文本
|
||||
* @slot unchecked-icon - 未选中状态的图标
|
||||
* @slot checked-icon - 选中状态的图标
|
||||
* @slot indeterminate-icon - 不确定状态的图标
|
||||
*
|
||||
* @csspart control - 左侧图标容器
|
||||
* @csspart unchecked-icon - 未选中状态的图标
|
||||
* @csspart checked-icon - 选中状态的图标
|
||||
* @csspart indeterminate-icon - 不确定状态的图标
|
||||
* @csspart label - 复选框文本
|
||||
*/
|
||||
export declare class Checkbox extends Checkbox_base<CheckboxEventMap> implements FormControl {
|
||||
static styles: CSSResultGroup;
|
||||
/**
|
||||
* 是否为禁用状态
|
||||
*/
|
||||
disabled: boolean;
|
||||
/**
|
||||
* 是否为选中状态
|
||||
*/
|
||||
checked: boolean;
|
||||
/**
|
||||
* 默认选中状态。在重置表单时,将恢复为此状态。此属性只能通过 JavaScript 属性设置
|
||||
*/
|
||||
defaultChecked: boolean;
|
||||
/**
|
||||
* 是否处于不确定状态
|
||||
*/
|
||||
indeterminate: boolean;
|
||||
/**
|
||||
* 提交表单时,是否必须选中此复选框
|
||||
*/
|
||||
required: boolean;
|
||||
/**
|
||||
* 关联的 `<form>` 元素。此属性值应为同一页面中的一个 `<form>` 元素的 `id`。
|
||||
*
|
||||
* 如果未指定此属性,则该元素必须是 `<form>` 元素的子元素。通过此属性,你可以将元素放置在页面的任何位置,而不仅仅是 `<form>` 元素的子元素。
|
||||
*/
|
||||
form?: string;
|
||||
/**
|
||||
* 复选框名称,将与表单数据一起提交
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* 复选框的值,将于表单数据一起提交
|
||||
*/
|
||||
value: string;
|
||||
/**
|
||||
* 未选中状态的 Material Icons 图标名。也可以通过 `slot="unchecked-icon"` 设置
|
||||
*/
|
||||
uncheckedIcon?: string;
|
||||
/**
|
||||
* 选中状态的 Material Icons 图标名。也可以通过 `slot="checked-icon"` 设置
|
||||
*/
|
||||
checkedIcon?: string;
|
||||
/**
|
||||
* 不确定状态的 Material Icons 图标名。也可以通过 `slot="indeterminate-icon"` 设置
|
||||
*/
|
||||
indeterminateIcon?: string;
|
||||
/**
|
||||
* 是否验证未通过
|
||||
*/
|
||||
private invalid;
|
||||
private readonly inputRef;
|
||||
private readonly rippleRef;
|
||||
private readonly formController;
|
||||
/**
|
||||
* 表单验证状态对象,具体参见 [`ValidityState`](https://developer.mozilla.org/zh-CN/docs/Web/API/ValidityState)
|
||||
*/
|
||||
get validity(): ValidityState;
|
||||
/**
|
||||
* 如果表单验证未通过,此属性将包含提示信息。如果验证通过,此属性将为空字符串
|
||||
*/
|
||||
get validationMessage(): string;
|
||||
protected get rippleElement(): Ripple;
|
||||
protected get rippleDisabled(): boolean;
|
||||
protected get focusElement(): HTMLElement | undefined;
|
||||
protected get focusDisabled(): boolean;
|
||||
private onDisabledChange;
|
||||
private onCheckedChange;
|
||||
/**
|
||||
* 检查表单字段是否通过验证。如果未通过,返回 `false` 并触发 `invalid` 事件;如果通过,返回 `true`
|
||||
*/
|
||||
checkValidity(): boolean;
|
||||
/**
|
||||
* 检查表单字段是否通过验证。如果未通过,返回 `false` 并触发 `invalid` 事件;如果通过,返回 `true`。
|
||||
*
|
||||
* 如果验证未通过,还会在组件上显示验证失败的提示。
|
||||
*/
|
||||
reportValidity(): boolean;
|
||||
/**
|
||||
* 设置自定义的错误提示文本。只要这个文本不为空,就表示字段未通过验证
|
||||
*
|
||||
* @param message 自定义的错误提示文本
|
||||
*/
|
||||
setCustomValidity(message: string): void;
|
||||
protected render(): TemplateResult;
|
||||
/**
|
||||
* input[type="checkbox"] 的 change 事件无法冒泡越过 shadow dom
|
||||
*/
|
||||
private onChange;
|
||||
}
|
||||
export interface CheckboxEventMap {
|
||||
focus: FocusEvent;
|
||||
blur: FocusEvent;
|
||||
change: CustomEvent<void>;
|
||||
input: Event;
|
||||
invalid: CustomEvent<void>;
|
||||
}
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
'mdui-checkbox': Checkbox;
|
||||
}
|
||||
}
|
||||
export {};
|
||||
254
mdui_patched/components/checkbox/index.js
Normal file
254
mdui_patched/components/checkbox/index.js
Normal file
@@ -0,0 +1,254 @@
|
||||
import { __decorate } from "tslib";
|
||||
import { html } from 'lit';
|
||||
import { customElement, property, state } from 'lit/decorators.js';
|
||||
import { classMap } from 'lit/directives/class-map.js';
|
||||
import { ifDefined } from 'lit/directives/if-defined.js';
|
||||
import { live } from 'lit/directives/live.js';
|
||||
import { createRef, ref } from 'lit/directives/ref.js';
|
||||
import { MduiElement } from '@mdui/shared/base/mdui-element.js';
|
||||
import { FormController, formResets } from '@mdui/shared/controllers/form.js';
|
||||
import { defaultValue } from '@mdui/shared/decorators/default-value.js';
|
||||
import { watch } from '@mdui/shared/decorators/watch.js';
|
||||
import { booleanConverter } from '@mdui/shared/helpers/decorator.js';
|
||||
import '@mdui/shared/icons/check-box-outline-blank.js';
|
||||
import '@mdui/shared/icons/check-box.js';
|
||||
import '@mdui/shared/icons/indeterminate-check-box.js';
|
||||
import { componentStyle } from '@mdui/shared/lit-styles/component-style.js';
|
||||
import { FocusableMixin } from '@mdui/shared/mixins/focusable.js';
|
||||
import '../icon.js';
|
||||
import { RippleMixin } from '../ripple/ripple-mixin.js';
|
||||
import { style } from './style.js';
|
||||
/**
|
||||
* @summary 复选框组件
|
||||
*
|
||||
* ```html
|
||||
* <mdui-checkbox>Checkbox</mdui-checkbox>
|
||||
* ```
|
||||
*
|
||||
* @event focus - 获得焦点时触发
|
||||
* @event blur - 失去焦点时触发
|
||||
* @event change - 选中状态变更时触发
|
||||
* @event input - 选中状态变更时触发
|
||||
* @event invalid - 表单字段验证未通过时触发
|
||||
*
|
||||
* @slot - 复选框文本
|
||||
* @slot unchecked-icon - 未选中状态的图标
|
||||
* @slot checked-icon - 选中状态的图标
|
||||
* @slot indeterminate-icon - 不确定状态的图标
|
||||
*
|
||||
* @csspart control - 左侧图标容器
|
||||
* @csspart unchecked-icon - 未选中状态的图标
|
||||
* @csspart checked-icon - 选中状态的图标
|
||||
* @csspart indeterminate-icon - 不确定状态的图标
|
||||
* @csspart label - 复选框文本
|
||||
*/
|
||||
let Checkbox = class Checkbox extends RippleMixin(FocusableMixin(MduiElement)) {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
/**
|
||||
* 是否为禁用状态
|
||||
*/
|
||||
this.disabled = false;
|
||||
/**
|
||||
* 是否为选中状态
|
||||
*/
|
||||
this.checked = false;
|
||||
/**
|
||||
* 默认选中状态。在重置表单时,将恢复为此状态。此属性只能通过 JavaScript 属性设置
|
||||
*/
|
||||
this.defaultChecked = false;
|
||||
/**
|
||||
* 是否处于不确定状态
|
||||
*/
|
||||
this.indeterminate = false;
|
||||
/**
|
||||
* 提交表单时,是否必须选中此复选框
|
||||
*/
|
||||
this.required = false;
|
||||
/**
|
||||
* 复选框名称,将与表单数据一起提交
|
||||
*/
|
||||
this.name = '';
|
||||
/**
|
||||
* 复选框的值,将于表单数据一起提交
|
||||
*/
|
||||
this.value = 'on';
|
||||
/**
|
||||
* 是否验证未通过
|
||||
*/
|
||||
this.invalid = false;
|
||||
this.inputRef = createRef();
|
||||
this.rippleRef = createRef();
|
||||
this.formController = new FormController(this, {
|
||||
value: (control) => (control.checked ? control.value : undefined),
|
||||
defaultValue: (control) => control.defaultChecked,
|
||||
setValue: (control, checked) => (control.checked = checked),
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 表单验证状态对象,具体参见 [`ValidityState`](https://developer.mozilla.org/zh-CN/docs/Web/API/ValidityState)
|
||||
*/
|
||||
get validity() {
|
||||
return this.inputRef.value.validity;
|
||||
}
|
||||
/**
|
||||
* 如果表单验证未通过,此属性将包含提示信息。如果验证通过,此属性将为空字符串
|
||||
*/
|
||||
get validationMessage() {
|
||||
return this.inputRef.value.validationMessage;
|
||||
}
|
||||
get rippleElement() {
|
||||
return this.rippleRef.value;
|
||||
}
|
||||
get rippleDisabled() {
|
||||
return this.disabled;
|
||||
}
|
||||
get focusElement() {
|
||||
return this.inputRef.value;
|
||||
}
|
||||
get focusDisabled() {
|
||||
return this.disabled;
|
||||
}
|
||||
async onDisabledChange() {
|
||||
await this.updateComplete;
|
||||
this.invalid = !this.inputRef.value.checkValidity();
|
||||
}
|
||||
async onCheckedChange() {
|
||||
await this.updateComplete;
|
||||
// reset 引起的值变更,不执行验证;直接修改值引起的变更,需要进行验证
|
||||
const form = this.formController.getForm();
|
||||
if (form && formResets.get(form)?.has(this)) {
|
||||
this.invalid = false;
|
||||
formResets.get(form).delete(this);
|
||||
}
|
||||
else {
|
||||
this.invalid = !this.inputRef.value.checkValidity();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 检查表单字段是否通过验证。如果未通过,返回 `false` 并触发 `invalid` 事件;如果通过,返回 `true`
|
||||
*/
|
||||
checkValidity() {
|
||||
const valid = this.inputRef.value.checkValidity();
|
||||
if (!valid) {
|
||||
this.emit('invalid', {
|
||||
bubbles: false,
|
||||
cancelable: true,
|
||||
composed: false,
|
||||
});
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
/**
|
||||
* 检查表单字段是否通过验证。如果未通过,返回 `false` 并触发 `invalid` 事件;如果通过,返回 `true`。
|
||||
*
|
||||
* 如果验证未通过,还会在组件上显示验证失败的提示。
|
||||
*/
|
||||
reportValidity() {
|
||||
this.invalid = !this.inputRef.value.reportValidity();
|
||||
if (this.invalid) {
|
||||
const eventProceeded = this.emit('invalid', {
|
||||
bubbles: false,
|
||||
cancelable: true,
|
||||
composed: false,
|
||||
});
|
||||
// 调用了 preventDefault() 时,隐藏默认的表单错误提示
|
||||
if (!eventProceeded) {
|
||||
this.blur();
|
||||
this.focus();
|
||||
}
|
||||
}
|
||||
return !this.invalid;
|
||||
}
|
||||
/**
|
||||
* 设置自定义的错误提示文本。只要这个文本不为空,就表示字段未通过验证
|
||||
*
|
||||
* @param message 自定义的错误提示文本
|
||||
*/
|
||||
setCustomValidity(message) {
|
||||
this.inputRef.value.setCustomValidity(message);
|
||||
this.invalid = !this.inputRef.value.checkValidity();
|
||||
}
|
||||
render() {
|
||||
return html `<label class="${classMap({ invalid: this.invalid })}"><input ${ref(this.inputRef)} type="checkbox" name="${ifDefined(this.name)}" value="${ifDefined(this.value)}" .indeterminate="${live(this.indeterminate)}" .disabled="${this.disabled}" .checked="${live(this.checked)}" .required="${this.required}" @change="${this.onChange}"> <i part="control"><mdui-ripple ${ref(this.rippleRef)} .noRipple="${this.noRipple}"></mdui-ripple><slot name="unchecked-icon" part="unchecked-icon" class="icon unchecked-icon">${this.uncheckedIcon
|
||||
? html `<mdui-icon name="${this.uncheckedIcon}" class="i"></mdui-icon>`
|
||||
: html `<mdui-icon-check-box-outline-blank class="i"></mdui-icon-check-box-outline-blank>`}</slot><slot name="checked-icon" part="checked-icon" class="icon checked-icon">${this.checkedIcon
|
||||
? html `<mdui-icon name="${this.checkedIcon}" class="i"></mdui-icon>`
|
||||
: html `<mdui-icon-check-box class="i"></mdui-icon-check-box>`}</slot><slot name="indeterminate-icon" part="indeterminate-icon" class="icon indeterminate-icon">${this.indeterminateIcon
|
||||
? html `<mdui-icon name="${this.indeterminateIcon}" class="i"></mdui-icon>`
|
||||
: html `<mdui-icon-indeterminate-check-box class="i"></mdui-icon-indeterminate-check-box>`}</slot></i><slot part="label" class="label"></slot></label>`;
|
||||
}
|
||||
/**
|
||||
* input[type="checkbox"] 的 change 事件无法冒泡越过 shadow dom
|
||||
*/
|
||||
onChange() {
|
||||
this.checked = this.inputRef.value.checked;
|
||||
this.indeterminate = false;
|
||||
this.emit('change');
|
||||
}
|
||||
};
|
||||
Checkbox.styles = [componentStyle, style];
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
})
|
||||
], Checkbox.prototype, "disabled", void 0);
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
})
|
||||
], Checkbox.prototype, "checked", void 0);
|
||||
__decorate([
|
||||
defaultValue('checked')
|
||||
], Checkbox.prototype, "defaultChecked", void 0);
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
})
|
||||
], Checkbox.prototype, "indeterminate", void 0);
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
})
|
||||
], Checkbox.prototype, "required", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true })
|
||||
], Checkbox.prototype, "form", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true })
|
||||
], Checkbox.prototype, "name", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true })
|
||||
], Checkbox.prototype, "value", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true, attribute: 'unchecked-icon' })
|
||||
], Checkbox.prototype, "uncheckedIcon", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true, attribute: 'checked-icon' })
|
||||
], Checkbox.prototype, "checkedIcon", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true, attribute: 'indeterminate-icon' })
|
||||
], Checkbox.prototype, "indeterminateIcon", void 0);
|
||||
__decorate([
|
||||
state()
|
||||
], Checkbox.prototype, "invalid", void 0);
|
||||
__decorate([
|
||||
watch('disabled', true),
|
||||
watch('indeterminate', true),
|
||||
watch('required', true)
|
||||
], Checkbox.prototype, "onDisabledChange", null);
|
||||
__decorate([
|
||||
watch('checked', true)
|
||||
], Checkbox.prototype, "onCheckedChange", null);
|
||||
Checkbox = __decorate([
|
||||
customElement('mdui-checkbox')
|
||||
], Checkbox);
|
||||
export { Checkbox };
|
||||
1
mdui_patched/components/checkbox/style.d.ts
vendored
Normal file
1
mdui_patched/components/checkbox/style.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export declare const style: import("lit").CSSResult;
|
||||
2
mdui_patched/components/checkbox/style.js
Normal file
2
mdui_patched/components/checkbox/style.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import { css } from 'lit';
|
||||
export const style = css `:host{position:relative;display:inline-flex;cursor:pointer;-webkit-tap-highlight-color:transparent;border-radius:.125rem;font-size:var(--mdui-typescale-label-large-size);font-weight:var(--mdui-typescale-label-large-weight);letter-spacing:var(--mdui-typescale-label-large-tracking);line-height:var(--mdui-typescale-label-large-line-height)}label{display:inline-flex;align-items:center;width:100%;cursor:inherit;-webkit-user-select:none;user-select:none;touch-action:manipulation;zoom:1;-webkit-user-drag:none}input{position:absolute;padding:0;opacity:0;pointer-events:none;width:1.125rem;height:1.125rem;margin:0 0 0 .6875rem}.icon{display:flex;position:absolute;opacity:1;transform:scale(1);color:rgb(var(--mdui-color-on-surface));font-size:1.5rem;transition:color var(--mdui-motion-duration-short4) var(--mdui-motion-easing-standard)}.checked-icon,.indeterminate-icon{opacity:0;transform:scale(.5);transition-property:color,opacity,transform;transition-duration:var(--mdui-motion-duration-short4);transition-timing-function:var(--mdui-motion-easing-standard)}.icon .i,::slotted([slot=checked-icon]),::slotted([slot=indeterminate-icon]),::slotted([slot=unchecked-icon]){color:inherit;font-size:inherit}i{position:relative;display:flex;align-items:center;justify-content:center;flex-shrink:0;overflow:hidden;border-radius:50%;width:2.5rem;height:2.5rem;--mdui-comp-ripple-state-layer-color:var(--mdui-color-on-surface)}.label{display:flex;width:100%;padding-top:.625rem;padding-bottom:.625rem;color:rgb(var(--mdui-color-on-surface));transition:color var(--mdui-motion-duration-short4) var(--mdui-motion-easing-standard)}:host([checked]:not([checked=false i])) i{--mdui-comp-ripple-state-layer-color:var(--mdui-color-primary)}:host([checked]:not([checked=false i])) .icon{color:rgb(var(--mdui-color-primary))}:host([checked]:not([checked=false i])) .indeterminate-icon{opacity:0;transform:scale(.5)}:host([checked]:not([checked=false i])) .checked-icon{opacity:1;transform:scale(1)}:host([indeterminate]:not([indeterminate=false i])) i{--mdui-comp-ripple-state-layer-color:var(--mdui-color-primary)}:host([indeterminate]:not([indeterminate=false i])) .icon{color:rgb(var(--mdui-color-primary))}:host([indeterminate]:not([indeterminate=false i])) .checked-icon{opacity:0;transform:scale(.5)}:host([indeterminate]:not([indeterminate=false i])) .indeterminate-icon{opacity:1;transform:scale(1)}.invalid i{--mdui-comp-ripple-state-layer-color:var(--mdui-color-error)}.invalid .icon{color:rgb(var(--mdui-color-error))}.invalid .label{color:rgb(var(--mdui-color-error))}:host([disabled]:not([disabled=false i])){cursor:default;pointer-events:none}:host([disabled]:not([disabled=false i])) .icon{color:rgba(var(--mdui-color-on-surface),38%)}:host([disabled]:not([disabled=false i])) .label{color:rgba(var(--mdui-color-on-surface),38%)}:host([disabled][checked]:not([disabled=false i],[checked=false i])) .unchecked-icon,:host([disabled][indeterminate]:not([disabled=false i],[indeterminate=false i])) .unchecked-icon{opacity:0}`;
|
||||
Reference in New Issue
Block a user