UNPKG

5.51 kBJavaScriptView Raw
1var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5 return c > 3 && r && Object.defineProperty(target, key, r), r;
6};
7var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11};
12var _DuoyunRadioElement_onClick, _DuoyunRadioGroupElement_onChange;
13import { adoptedStyle, customElement, attribute, globalemitter, property, boolattribute, } from '@mantou/gem/lib/decorators';
14import { GemElement, html } from '@mantou/gem/lib/element';
15import { createCSSSheet, css } from '@mantou/gem/lib/utils';
16import { theme } from '../lib/theme';
17import { commonHandle } from '../lib/hotkeys';
18import { focusStyle } from '../lib/styles';
19const style = createCSSSheet(css `
20 :host(:where(:not([hidden]))) {
21 cursor: default;
22 display: inline-flex;
23 align-items: center;
24 gap: 0.5em;
25 }
26 :host([disabled]) {
27 cursor: not-allowed;
28 opacity: 0.3;
29 }
30 .radio {
31 box-sizing: border-box;
32 width: 1em;
33 aspect-ratio: 1;
34 border: 2px solid currentColor;
35 color: ${theme.borderColor};
36 background-clip: content-box;
37 padding: 2px;
38 border-radius: 10em;
39 transform: scale(1.001);
40 }
41 :host(:where(:hover, [checked])) .radio {
42 color: ${theme.primaryColor};
43 }
44 :host([checked]) .radio {
45 background-color: currentColor;
46 }
47`);
48/**
49 * @customElement dy-radio
50 * @attr checked
51 * @attr value
52 * @attr disabled
53 */
54let DuoyunRadioElement = class DuoyunRadioElement extends GemElement {
55 constructor() {
56 super({ delegatesFocus: true });
57 _DuoyunRadioElement_onClick.set(this, () => {
58 if (this.disabled)
59 return;
60 if (!this.checked) {
61 this.change(this.value);
62 }
63 });
64 this.render = () => {
65 return html `
66 <div
67 role="radio"
68 tabindex="0"
69 @keydown=${commonHandle}
70 aria-labelledby="label"
71 aria-disabled=${this.disabled}
72 aria-checked=${this.checked}
73 class="radio"
74 ></div>
75 <slot id="label"></slot>
76 `;
77 };
78 this.addEventListener('click', __classPrivateFieldGet(this, _DuoyunRadioElement_onClick, "f"));
79 }
80};
81_DuoyunRadioElement_onClick = new WeakMap();
82__decorate([
83 boolattribute
84], DuoyunRadioElement.prototype, "checked", void 0);
85__decorate([
86 boolattribute
87], DuoyunRadioElement.prototype, "disabled", void 0);
88__decorate([
89 globalemitter
90], DuoyunRadioElement.prototype, "change", void 0);
91__decorate([
92 attribute
93], DuoyunRadioElement.prototype, "value", void 0);
94DuoyunRadioElement = __decorate([
95 customElement('dy-radio'),
96 adoptedStyle(style),
97 adoptedStyle(focusStyle)
98], DuoyunRadioElement);
99export { DuoyunRadioElement };
100export const groupStyle = createCSSSheet(css `
101 :host(:where(:not([hidden]))) {
102 display: flex;
103 align-items: center;
104 flex-wrap: wrap;
105 }
106 :host([orientation='horizontal']) {
107 gap: 1em;
108 }
109 :host([orientation='vertical']) {
110 flex-direction: column;
111 align-items: flex-start;
112 }
113`);
114/**
115 * @customElement dy-radio-group
116 * @attr disabled
117 */
118let DuoyunRadioGroupElement = class DuoyunRadioGroupElement extends GemElement {
119 constructor() {
120 super({ delegatesFocus: true });
121 _DuoyunRadioGroupElement_onChange.set(this, (evt) => {
122 evt.stopPropagation();
123 this.change(evt.detail);
124 });
125 this.render = () => {
126 if (!this.options)
127 return null;
128 return html `${this.options.map(({ label, value }) => html `
129 <dy-radio
130 ?disabled=${this.disabled}
131 .value=${value !== null && value !== void 0 ? value : label}
132 ?checked=${(value !== null && value !== void 0 ? value : label) === this.value}
133 @change=${__classPrivateFieldGet(this, _DuoyunRadioGroupElement_onChange, "f")}
134 >
135 ${label}
136 </dy-radio>
137 `)}`;
138 };
139 this.internals.role = 'radiogroup';
140 }
141};
142_DuoyunRadioGroupElement_onChange = new WeakMap();
143__decorate([
144 attribute
145], DuoyunRadioGroupElement.prototype, "orientation", void 0);
146__decorate([
147 boolattribute
148], DuoyunRadioGroupElement.prototype, "disabled", void 0);
149__decorate([
150 globalemitter
151], DuoyunRadioGroupElement.prototype, "change", void 0);
152__decorate([
153 property
154], DuoyunRadioGroupElement.prototype, "value", void 0);
155__decorate([
156 property
157], DuoyunRadioGroupElement.prototype, "options", void 0);
158DuoyunRadioGroupElement = __decorate([
159 customElement('dy-radio-group'),
160 adoptedStyle(groupStyle)
161], DuoyunRadioGroupElement);
162export { DuoyunRadioGroupElement };
163//# sourceMappingURL=radio.js.map
\No newline at end of file