fix: 本地 patch MDUI 以解决 tabindex = 0 导致的一系列玄学问题
This commit is contained in:
1
client/mdui_patched/components/radio/radio-group-style.d.ts
vendored
Normal file
1
client/mdui_patched/components/radio/radio-group-style.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export declare const radioGroupStyle: import("lit").CSSResult;
|
||||
@@ -0,0 +1,2 @@
|
||||
import { css } from 'lit';
|
||||
export const radioGroupStyle = css `:host{display:inline-block}fieldset{border:none;padding:0;margin:0;min-width:0}input{position:absolute;padding:0;opacity:0;pointer-events:none;width:1.25rem;height:1.25rem;margin:0 0 0 .625rem}`;
|
||||
110
client/mdui_patched/components/radio/radio-group.d.ts
vendored
Normal file
110
client/mdui_patched/components/radio/radio-group.d.ts
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
import '@mdui/jq/methods/closest.js';
|
||||
import '@mdui/jq/methods/find.js';
|
||||
import '@mdui/jq/methods/get.js';
|
||||
import { MduiElement } from '@mdui/shared/base/mdui-element.js';
|
||||
import type { FormControl } from '@mdui/jq/shared/form.js';
|
||||
import type { CSSResultGroup, TemplateResult } from 'lit';
|
||||
/**
|
||||
* @summary 单选框组组件。需配合 `<mdui-radio>` 组件使用
|
||||
*
|
||||
* ```html
|
||||
* <mdui-radio-group value="chinese">
|
||||
* ..<mdui-radio value="chinese">Chinese</mdui-radio>
|
||||
* ..<mdui-radio value="english">English</mdui-radio>
|
||||
* </mdui-radio-group>
|
||||
* ```
|
||||
*
|
||||
* @event change - 选中值变化时触发
|
||||
* @event input - 选中值变化时触发
|
||||
* @event invalid - 表单字段验证未通过时触发
|
||||
*
|
||||
* @slot - `<mdui-radio>` 元素
|
||||
*/
|
||||
export declare class RadioGroup extends MduiElement<RadioGroupEventMap> implements FormControl {
|
||||
static styles: CSSResultGroup;
|
||||
/**
|
||||
* 是否禁用此组件
|
||||
*/
|
||||
disabled: boolean;
|
||||
/**
|
||||
* 关联的 `<form>` 元素。此属性值应为同一页面中的一个 `<form>` 元素的 `id`。
|
||||
*
|
||||
* 如果未指定此属性,则该元素必须是 `<form>` 元素的子元素。通过此属性,你可以将元素放置在页面的任何位置,而不仅仅是 `<form>` 元素的子元素。
|
||||
*/
|
||||
form?: string;
|
||||
/**
|
||||
* 单选框组的名称,将与表单数据一起提交
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* 单选框组的名称,将于表单数据一起提交
|
||||
*/
|
||||
value: string;
|
||||
/**
|
||||
* 默认选中的值。在重置表单时,将重置为该默认值。该属性只能通过 JavaScript 属性设置
|
||||
*/
|
||||
defaultValue: string;
|
||||
/**
|
||||
* 提交表单时,是否必须选中其中一个单选框
|
||||
*/
|
||||
required: boolean;
|
||||
/**
|
||||
* 是否验证未通过
|
||||
*/
|
||||
private invalid;
|
||||
private isInitial;
|
||||
private readonly inputRef;
|
||||
private readonly formController;
|
||||
private readonly definedController;
|
||||
/**
|
||||
* 表单验证状态对象,具体参见 [`ValidityState`](https://developer.mozilla.org/zh-CN/docs/Web/API/ValidityState)
|
||||
*/
|
||||
get validity(): ValidityState;
|
||||
/**
|
||||
* 如果表单验证未通过,此属性将包含提示信息。如果验证通过,此属性将为空字符串
|
||||
*/
|
||||
get validationMessage(): string;
|
||||
private get items();
|
||||
private get itemsEnabled();
|
||||
private onValueChange;
|
||||
private onInvalidChange;
|
||||
/**
|
||||
* 检查表单字段是否通过验证。如果未通过,返回 `false` 并触发 `invalid` 事件;如果通过,返回 `true`
|
||||
*/
|
||||
checkValidity(): boolean;
|
||||
/**
|
||||
* 检查表单字段是否通过验证。如果未通过,返回 `false` 并触发 `invalid` 事件;如果通过,返回 `true`。
|
||||
*
|
||||
* 如果验证未通过,还会在组件上显示验证失败的提示。
|
||||
*/
|
||||
reportValidity(): boolean;
|
||||
/**
|
||||
* 设置自定义的错误提示文本。只要这个文本不为空,就表示字段未通过验证
|
||||
*
|
||||
* @param message 自定义的错误提示文本
|
||||
*/
|
||||
setCustomValidity(message: string): void;
|
||||
protected render(): TemplateResult;
|
||||
private updateRadioFocusable;
|
||||
private onClick;
|
||||
/**
|
||||
* 在内部的 `<mdui-radio>` 上按下按键时,在 `<mdui-radio>` 之间切换焦点
|
||||
*/
|
||||
private onKeyDown;
|
||||
private onSlotChange;
|
||||
/**
|
||||
* slot 中的 mdui-radio 的 checked 变更时触发的事件
|
||||
*/
|
||||
private onCheckedChange;
|
||||
private updateItems;
|
||||
}
|
||||
export interface RadioGroupEventMap {
|
||||
change: CustomEvent<void>;
|
||||
input: CustomEvent<void>;
|
||||
invalid: CustomEvent<void>;
|
||||
}
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
'mdui-radio-group': RadioGroup;
|
||||
}
|
||||
}
|
||||
278
client/mdui_patched/components/radio/radio-group.js
Normal file
278
client/mdui_patched/components/radio/radio-group.js
Normal file
@@ -0,0 +1,278 @@
|
||||
import { __decorate } from "tslib";
|
||||
import { html } from 'lit';
|
||||
import { customElement, property, state } from 'lit/decorators.js';
|
||||
import { ifDefined } from 'lit/directives/if-defined.js';
|
||||
import { createRef, ref } from 'lit/directives/ref.js';
|
||||
import { $ } from '@mdui/jq/$.js';
|
||||
import '@mdui/jq/methods/closest.js';
|
||||
import '@mdui/jq/methods/find.js';
|
||||
import '@mdui/jq/methods/get.js';
|
||||
import { MduiElement } from '@mdui/shared/base/mdui-element.js';
|
||||
import { DefinedController } from '@mdui/shared/controllers/defined.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 { componentStyle } from '@mdui/shared/lit-styles/component-style.js';
|
||||
import { radioGroupStyle } from './radio-group-style.js';
|
||||
/**
|
||||
* @summary 单选框组组件。需配合 `<mdui-radio>` 组件使用
|
||||
*
|
||||
* ```html
|
||||
* <mdui-radio-group value="chinese">
|
||||
* ..<mdui-radio value="chinese">Chinese</mdui-radio>
|
||||
* ..<mdui-radio value="english">English</mdui-radio>
|
||||
* </mdui-radio-group>
|
||||
* ```
|
||||
*
|
||||
* @event change - 选中值变化时触发
|
||||
* @event input - 选中值变化时触发
|
||||
* @event invalid - 表单字段验证未通过时触发
|
||||
*
|
||||
* @slot - `<mdui-radio>` 元素
|
||||
*/
|
||||
let RadioGroup = class RadioGroup extends MduiElement {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
/**
|
||||
* 是否禁用此组件
|
||||
*/
|
||||
this.disabled = false;
|
||||
/**
|
||||
* 单选框组的名称,将与表单数据一起提交
|
||||
*/
|
||||
this.name = '';
|
||||
/**
|
||||
* 单选框组的名称,将于表单数据一起提交
|
||||
*/
|
||||
this.value = '';
|
||||
/**
|
||||
* 默认选中的值。在重置表单时,将重置为该默认值。该属性只能通过 JavaScript 属性设置
|
||||
*/
|
||||
this.defaultValue = '';
|
||||
/**
|
||||
* 提交表单时,是否必须选中其中一个单选框
|
||||
*/
|
||||
this.required = false;
|
||||
/**
|
||||
* 是否验证未通过
|
||||
*/
|
||||
this.invalid = false;
|
||||
// 是否是初始状态,初始状态不显示动画
|
||||
this.isInitial = true;
|
||||
this.inputRef = createRef();
|
||||
this.formController = new FormController(this);
|
||||
this.definedController = new DefinedController(this, {
|
||||
relatedElements: ['mdui-radio'],
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 表单验证状态对象,具体参见 [`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;
|
||||
}
|
||||
// 为了使 <mdui-radio> 可以不是该组件的直接子元素,这里不用 @queryAssignedElements()
|
||||
get items() {
|
||||
return $(this).find('mdui-radio').get();
|
||||
}
|
||||
get itemsEnabled() {
|
||||
return $(this)
|
||||
.find('mdui-radio:not([disabled])')
|
||||
.get();
|
||||
}
|
||||
async onValueChange() {
|
||||
this.isInitial = false;
|
||||
await this.definedController.whenDefined();
|
||||
this.emit('input');
|
||||
this.emit('change');
|
||||
this.updateItems();
|
||||
this.updateRadioFocusable();
|
||||
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();
|
||||
}
|
||||
}
|
||||
async onInvalidChange() {
|
||||
await this.definedController.whenDefined();
|
||||
this.updateItems();
|
||||
}
|
||||
/**
|
||||
* 检查表单字段是否通过验证。如果未通过,返回 `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,
|
||||
});
|
||||
if (!eventProceeded) {
|
||||
// 调用了 preventDefault() 时,隐藏默认的表单错误提示
|
||||
this.inputRef.value.blur();
|
||||
this.inputRef.value.focus();
|
||||
}
|
||||
}
|
||||
return !this.invalid;
|
||||
}
|
||||
/**
|
||||
* 设置自定义的错误提示文本。只要这个文本不为空,就表示字段未通过验证
|
||||
*
|
||||
* @param message 自定义的错误提示文本
|
||||
*/
|
||||
setCustomValidity(message) {
|
||||
this.inputRef.value.setCustomValidity(message);
|
||||
this.invalid = !this.inputRef.value.checkValidity();
|
||||
}
|
||||
render() {
|
||||
return html `<fieldset><input ${ref(this.inputRef)} type="radio" class="input" name="${ifDefined(this.name)}" value="${ifDefined(this.value)}" .checked="${!!this.value}" .required="${this.required}" tabindex="-1" @keydown="${this.onKeyDown}"><slot @click="${this.onClick}" @keydown="${this.onKeyDown}" @slotchange="${this.onSlotChange}" @change="${this.onCheckedChange}"></slot></fieldset>`;
|
||||
}
|
||||
// 更新 mdui-radio 的 checked 后,需要更新可聚焦状态
|
||||
// 同一个 mdui-radio-group 中的多个 mdui-radio,仅有一个可聚焦
|
||||
// 若有已选中的,则已选中的可聚焦;若没有已选中的,则第一个可聚焦
|
||||
updateRadioFocusable() {
|
||||
const items = this.items;
|
||||
const itemChecked = items.find((item) => item.checked);
|
||||
if (itemChecked) {
|
||||
items.forEach((item) => {
|
||||
item.focusable = item === itemChecked;
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.itemsEnabled.forEach((item, index) => {
|
||||
item.focusable = !index;
|
||||
});
|
||||
}
|
||||
}
|
||||
async onClick(event) {
|
||||
await this.definedController.whenDefined();
|
||||
const target = event.target;
|
||||
const item = target.closest('mdui-radio');
|
||||
if (!item || item.disabled) {
|
||||
return;
|
||||
}
|
||||
this.value = item.value;
|
||||
await this.updateComplete;
|
||||
item.focus();
|
||||
}
|
||||
/**
|
||||
* 在内部的 `<mdui-radio>` 上按下按键时,在 `<mdui-radio>` 之间切换焦点
|
||||
*/
|
||||
async onKeyDown(event) {
|
||||
if (!['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' '].includes(event.key)) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
await this.definedController.whenDefined();
|
||||
const items = this.itemsEnabled;
|
||||
const itemChecked = items.find((item) => item.checked) ?? items[0];
|
||||
const incr = event.key === ' '
|
||||
? 0
|
||||
: ['ArrowUp', 'ArrowLeft'].includes(event.key)
|
||||
? -1
|
||||
: 1;
|
||||
let index = items.indexOf(itemChecked) + incr;
|
||||
if (index < 0) {
|
||||
index = items.length - 1;
|
||||
}
|
||||
if (index > items.length - 1) {
|
||||
index = 0;
|
||||
}
|
||||
this.value = items[index].value;
|
||||
await this.updateComplete;
|
||||
items[index].focus();
|
||||
}
|
||||
async onSlotChange() {
|
||||
await this.definedController.whenDefined();
|
||||
this.updateItems();
|
||||
this.updateRadioFocusable();
|
||||
}
|
||||
/**
|
||||
* slot 中的 mdui-radio 的 checked 变更时触发的事件
|
||||
*/
|
||||
onCheckedChange(event) {
|
||||
event.stopPropagation();
|
||||
}
|
||||
// 更新 <mdui-radio> 的状态
|
||||
updateItems() {
|
||||
this.items.forEach((item) => {
|
||||
item.checked = item.value === this.value;
|
||||
item.invalid = this.invalid;
|
||||
item.groupDisabled = this.disabled;
|
||||
item.isInitial = this.isInitial;
|
||||
});
|
||||
}
|
||||
};
|
||||
RadioGroup.styles = [
|
||||
componentStyle,
|
||||
radioGroupStyle,
|
||||
];
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
})
|
||||
], RadioGroup.prototype, "disabled", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true })
|
||||
], RadioGroup.prototype, "form", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true })
|
||||
], RadioGroup.prototype, "name", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true })
|
||||
], RadioGroup.prototype, "value", void 0);
|
||||
__decorate([
|
||||
defaultValue()
|
||||
], RadioGroup.prototype, "defaultValue", void 0);
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
})
|
||||
], RadioGroup.prototype, "required", void 0);
|
||||
__decorate([
|
||||
state()
|
||||
], RadioGroup.prototype, "invalid", void 0);
|
||||
__decorate([
|
||||
watch('value', true)
|
||||
], RadioGroup.prototype, "onValueChange", null);
|
||||
__decorate([
|
||||
watch('invalid', true),
|
||||
watch('disabled')
|
||||
], RadioGroup.prototype, "onInvalidChange", null);
|
||||
RadioGroup = __decorate([
|
||||
customElement('mdui-radio-group')
|
||||
], RadioGroup);
|
||||
export { RadioGroup };
|
||||
1
client/mdui_patched/components/radio/radio-style.d.ts
vendored
Normal file
1
client/mdui_patched/components/radio/radio-style.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export declare const radioStyle: import("lit").CSSResult;
|
||||
2
client/mdui_patched/components/radio/radio-style.js
Normal file
2
client/mdui_patched/components/radio/radio-style.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import { css } from 'lit';
|
||||
export const radioStyle = css `:host{position:relative;display:inline-flex;align-items:center;cursor:pointer;-webkit-tap-highlight-color:transparent;-webkit-user-select:none;user-select:none;touch-action:manipulation;zoom:1;-webkit-user-drag:none;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)}.icon{display:flex;position:absolute;font-size:1.5rem}:not(.initial) .icon{transition-duration:var(--mdui-motion-duration-short4);transition-timing-function:var(--mdui-motion-easing-standard)}.unchecked-icon{transition-property:color;color:rgb(var(--mdui-color-on-surface-variant))}:host([focused]) .unchecked-icon,:host([hover]) .unchecked-icon,:host([pressed]) .unchecked-icon{color:rgb(var(--mdui-color-on-surface))}.checked-icon{opacity:0;transform:scale(.2);transition-property:color,opacity,transform;color:rgb(var(--mdui-color-primary))}.icon .i,::slotted([slot=checked-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))}.label:not(.initial){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])) .checked-icon{opacity:1;transform:scale(.5)}i.invalid{--mdui-comp-ripple-state-layer-color:var(--mdui-color-error)}i.invalid .icon{color:rgb(var(--mdui-color-error))}.label.invalid{color:rgb(var(--mdui-color-error))}:host([disabled]:not([disabled=false i])),:host([group-disabled]){cursor:default;pointer-events:none}:host([disabled]:not([disabled=false i])) .icon,:host([group-disabled]) .icon{color:rgba(var(--mdui-color-on-surface),38%)}:host([disabled]:not([disabled=false i])) .label,:host([group-disabled]) .label{color:rgba(var(--mdui-color-on-surface),38%)}`;
|
||||
77
client/mdui_patched/components/radio/radio.d.ts
vendored
Normal file
77
client/mdui_patched/components/radio/radio.d.ts
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
import { MduiElement } from '@mdui/shared/base/mdui-element.js';
|
||||
import '@mdui/shared/icons/circle.js';
|
||||
import '@mdui/shared/icons/radio-button-unchecked.js';
|
||||
import '../icon.js';
|
||||
import type { Ripple } from '../ripple/index.js';
|
||||
import type { CSSResultGroup, PropertyValues, TemplateResult } from 'lit';
|
||||
declare const Radio_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 单选框组件。需配合 `<mdui-radio-group>` 组件使用
|
||||
*
|
||||
* ```html
|
||||
* <mdui-radio-group value="chinese">
|
||||
* ..<mdui-radio value="chinese">Chinese</mdui-radio>
|
||||
* ..<mdui-radio value="english">English</mdui-radio>
|
||||
* </mdui-radio-group>
|
||||
* ```
|
||||
*
|
||||
* @event focus - 获得焦点时触发
|
||||
* @event blur - 失去焦点时触发
|
||||
* @event change - 选中该单选项时触发
|
||||
*
|
||||
* @slot - 文本内容
|
||||
* @slot unchecked-icon - 未选中状态的图标
|
||||
* @slot checked-icon - 选中状态的图标
|
||||
*
|
||||
* @csspart control - 左侧图标容器
|
||||
* @csspart unchecked-icon 未选中状态的图标
|
||||
* @csspart checked-icon 选中状态的图标
|
||||
* @csspart label - 文本内容
|
||||
*/
|
||||
export declare class Radio extends Radio_base<RadioEventMap> {
|
||||
static styles: CSSResultGroup;
|
||||
/**
|
||||
* 当前单选项的值
|
||||
*/
|
||||
value: string;
|
||||
/**
|
||||
* 是否禁用当前单选项
|
||||
*/
|
||||
disabled: boolean;
|
||||
/**
|
||||
* 当前单选项是否已选中
|
||||
*/
|
||||
checked: boolean;
|
||||
/**
|
||||
* 未选中状态的 Material Icons 图标名。也可以通过 `slot="unchecked-icon"` 设置
|
||||
*/
|
||||
uncheckedIcon?: string;
|
||||
/**
|
||||
* 选中状态的 Material Icons 图标名。也可以通过 `slot="checked-icon"` 设置
|
||||
*/
|
||||
checkedIcon?: string;
|
||||
protected invalid: boolean;
|
||||
protected groupDisabled: boolean;
|
||||
protected focusable: boolean;
|
||||
protected isInitial: boolean;
|
||||
private readonly rippleRef;
|
||||
protected get rippleElement(): Ripple;
|
||||
protected get rippleDisabled(): boolean;
|
||||
protected get focusElement(): HTMLElement;
|
||||
protected get focusDisabled(): boolean;
|
||||
private onCheckedChange;
|
||||
protected firstUpdated(_changedProperties: PropertyValues): void;
|
||||
protected render(): TemplateResult;
|
||||
private isDisabled;
|
||||
}
|
||||
export interface RadioEventMap {
|
||||
focus: FocusEvent;
|
||||
blur: FocusEvent;
|
||||
change: CustomEvent<void>;
|
||||
}
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
'mdui-radio': Radio;
|
||||
}
|
||||
}
|
||||
export {};
|
||||
151
client/mdui_patched/components/radio/radio.js
Normal file
151
client/mdui_patched/components/radio/radio.js
Normal file
@@ -0,0 +1,151 @@
|
||||
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 { createRef, ref } from 'lit/directives/ref.js';
|
||||
import { MduiElement } from '@mdui/shared/base/mdui-element.js';
|
||||
import { watch } from '@mdui/shared/decorators/watch.js';
|
||||
import { booleanConverter } from '@mdui/shared/helpers/decorator.js';
|
||||
import '@mdui/shared/icons/circle.js';
|
||||
import '@mdui/shared/icons/radio-button-unchecked.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 { radioStyle } from './radio-style.js';
|
||||
/**
|
||||
* @summary 单选框组件。需配合 `<mdui-radio-group>` 组件使用
|
||||
*
|
||||
* ```html
|
||||
* <mdui-radio-group value="chinese">
|
||||
* ..<mdui-radio value="chinese">Chinese</mdui-radio>
|
||||
* ..<mdui-radio value="english">English</mdui-radio>
|
||||
* </mdui-radio-group>
|
||||
* ```
|
||||
*
|
||||
* @event focus - 获得焦点时触发
|
||||
* @event blur - 失去焦点时触发
|
||||
* @event change - 选中该单选项时触发
|
||||
*
|
||||
* @slot - 文本内容
|
||||
* @slot unchecked-icon - 未选中状态的图标
|
||||
* @slot checked-icon - 选中状态的图标
|
||||
*
|
||||
* @csspart control - 左侧图标容器
|
||||
* @csspart unchecked-icon 未选中状态的图标
|
||||
* @csspart checked-icon 选中状态的图标
|
||||
* @csspart label - 文本内容
|
||||
*/
|
||||
let Radio = class Radio extends RippleMixin(FocusableMixin(MduiElement)) {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
/**
|
||||
* 当前单选项的值
|
||||
*/
|
||||
this.value = '';
|
||||
/**
|
||||
* 是否禁用当前单选项
|
||||
*/
|
||||
this.disabled = false;
|
||||
/**
|
||||
* 当前单选项是否已选中
|
||||
*/
|
||||
this.checked = false;
|
||||
// 是否验证未通过。由 <mdui-radio-group> 控制该参数
|
||||
this.invalid = false;
|
||||
// 父组件中是否设置了禁用。由 <mdui-radio-group> 控制该参数
|
||||
this.groupDisabled = false;
|
||||
// 是否可聚焦。
|
||||
// 单独使用该组件时,默认可聚焦。
|
||||
// 如果放在 <mdui-radio-group> 组件中使用,则由 <mdui-radio-group> 控制该参数
|
||||
this.focusable = true;
|
||||
// 是否是初始状态,不显示动画。由 <mdui-radio-group> 组件控制该参数
|
||||
this.isInitial = true;
|
||||
this.rippleRef = createRef();
|
||||
}
|
||||
get rippleElement() {
|
||||
return this.rippleRef.value;
|
||||
}
|
||||
get rippleDisabled() {
|
||||
return this.isDisabled();
|
||||
}
|
||||
get focusElement() {
|
||||
return this;
|
||||
}
|
||||
get focusDisabled() {
|
||||
return this.isDisabled() || !this.focusable;
|
||||
}
|
||||
onCheckedChange() {
|
||||
this.emit('change');
|
||||
}
|
||||
firstUpdated(_changedProperties) {
|
||||
super.firstUpdated(_changedProperties);
|
||||
this.addEventListener('click', () => {
|
||||
if (!this.isDisabled()) {
|
||||
this.checked = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
render() {
|
||||
const className = classMap({
|
||||
invalid: this.invalid,
|
||||
initial: this.isInitial,
|
||||
});
|
||||
return html `<i part="control" class="${className}"><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-radio-button-unchecked class="i"></mdui-icon-radio-button-unchecked>`}</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-circle class="i"></mdui-icon-circle>`}</slot></i><slot part="label" class="label ${className}"></slot>`;
|
||||
}
|
||||
isDisabled() {
|
||||
return this.disabled || this.groupDisabled;
|
||||
}
|
||||
};
|
||||
Radio.styles = [componentStyle, radioStyle];
|
||||
__decorate([
|
||||
property({ reflect: true })
|
||||
], Radio.prototype, "value", void 0);
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
})
|
||||
], Radio.prototype, "disabled", void 0);
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
})
|
||||
], Radio.prototype, "checked", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true, attribute: 'unchecked-icon' })
|
||||
], Radio.prototype, "uncheckedIcon", void 0);
|
||||
__decorate([
|
||||
property({ reflect: true, attribute: 'checked-icon' })
|
||||
], Radio.prototype, "checkedIcon", void 0);
|
||||
__decorate([
|
||||
state()
|
||||
], Radio.prototype, "invalid", void 0);
|
||||
__decorate([
|
||||
property({
|
||||
type: Boolean,
|
||||
reflect: true,
|
||||
converter: booleanConverter,
|
||||
attribute: 'group-disabled',
|
||||
})
|
||||
], Radio.prototype, "groupDisabled", void 0);
|
||||
__decorate([
|
||||
state()
|
||||
], Radio.prototype, "focusable", void 0);
|
||||
__decorate([
|
||||
state()
|
||||
], Radio.prototype, "isInitial", void 0);
|
||||
__decorate([
|
||||
watch('checked', true)
|
||||
], Radio.prototype, "onCheckedChange", null);
|
||||
Radio = __decorate([
|
||||
customElement('mdui-radio')
|
||||
], Radio);
|
||||
export { Radio };
|
||||
Reference in New Issue
Block a user