UNPKG

7.53 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 _DuoyunPickElement_isContain, _DuoyunPickElement_genMenu, _DuoyunPickElement_onOpen;
13import { connectStore, adoptedStyle, customElement, attribute, globalemitter, property, boolattribute, state, } from '@mantou/gem/lib/decorators';
14import { GemElement, html } from '@mantou/gem/lib/element';
15import { createCSSSheet, css, classMap } from '@mantou/gem/lib/utils';
16import { theme } from '../lib/theme';
17import { icons } from '../lib/icons';
18import { commonHandle } from '../lib/hotkeys';
19import { focusStyle } from '../lib/styles';
20import { ContextMenu } from './menu';
21import './use';
22export const pickerStyle = createCSSSheet(css `
23 :host(:where(:not([hidden]))) {
24 cursor: pointer;
25 display: inline-flex;
26 align-items: center;
27 border: 1px solid ${theme.borderColor};
28 border-radius: ${theme.normalRound};
29 line-height: 2;
30 padding: 0.1em 0.5em;
31 gap: 0.5em;
32 font-size: 0.875em;
33 box-sizing: border-box;
34 }
35 :host(:where([data-active], :state(active))) {
36 background: ${theme.lightBackgroundColor};
37 }
38 :host([disabled]) {
39 cursor: not-allowed;
40 border-color: transparent;
41 background: ${theme.disabledColor};
42 }
43 :host([disabled]) dy-use {
44 pointer-events: none;
45 }
46 :host([borderless]) {
47 width: auto;
48 border-color: transparent;
49 }
50 dy-use {
51 flex-shrink: 0;
52 padding-block: 0.4em;
53 width: 1.2em;
54 color: ${theme.borderColor};
55 }
56 :host(:not([disabled]):where(:hover, [data-active], :state(active))) dy-use {
57 color: ${theme.textColor};
58 }
59`);
60export class BasePickerElement {
61}
62const style = createCSSSheet(css `
63 :host {
64 width: 12em;
65 }
66 .value {
67 flex-grow: 1;
68 overflow: hidden;
69 text-overflow: ellipsis;
70 white-space: nowrap;
71 }
72 .placeholder {
73 color: ${theme.describeColor};
74 }
75`);
76/**
77 * @customElement dy-picker
78 * @attr disabled
79 * @attr borderless
80 * @attr selectmode
81 * @attr fit
82 * @attr multiple
83 */
84let DuoyunPickElement = class DuoyunPickElement extends GemElement {
85 constructor() {
86 super();
87 _DuoyunPickElement_isContain.set(this, (value) => {
88 return this.multiple ? this.value.includes(value) : this.value === value;
89 });
90 _DuoyunPickElement_genMenu.set(this, ({ label, description, value, children }) => {
91 const v = value !== null && value !== void 0 ? value : label;
92 return {
93 text: label,
94 description,
95 selected: !this.selectmode && !children && __classPrivateFieldGet(this, _DuoyunPickElement_isContain, "f").call(this, v),
96 handle: children
97 ? undefined
98 : () => {
99 if (this.multiple) {
100 const set = new Set(this.value);
101 set.has(v) ? set.delete(v) : set.add(v);
102 this.change([...set]);
103 }
104 else if (!__classPrivateFieldGet(this, _DuoyunPickElement_isContain, "f").call(this, v)) {
105 this.change(v);
106 }
107 },
108 menu: children === null || children === void 0 ? void 0 : children.map(__classPrivateFieldGet(this, _DuoyunPickElement_genMenu, "f")),
109 };
110 });
111 _DuoyunPickElement_onOpen.set(this, async () => {
112 if (this.disabled || !this.options || !this.options.length)
113 return;
114 await ContextMenu.open(this.options.map(__classPrivateFieldGet(this, _DuoyunPickElement_genMenu, "f")), {
115 activeElement: this,
116 width: this.fit ? `${this.getBoundingClientRect().width}px` : undefined,
117 });
118 });
119 this.updated = () => {
120 if (this.active) {
121 __classPrivateFieldGet(this, _DuoyunPickElement_onOpen, "f").call(this);
122 }
123 };
124 this.mounted = () => {
125 return () => this.active && ContextMenu.close();
126 };
127 this.render = () => {
128 var _a;
129 const currents = (_a = this.options) === null || _a === void 0 ? void 0 : _a.map((e) => (e.children ? [e, ...e.children] : e)).flat().filter((e) => { var _a; return __classPrivateFieldGet(this, _DuoyunPickElement_isContain, "f").call(this, (_a = e.value) !== null && _a !== void 0 ? _a : e.label); });
130 const currentLabels = currents === null || currents === void 0 ? void 0 : currents.map((e) => e.label);
131 const isEmpty = !(currentLabels === null || currentLabels === void 0 ? void 0 : currentLabels.length);
132 return html `
133 <div class=${classMap({ value: true, placeholder: isEmpty })}>
134 ${isEmpty ? this.placeholder : typeof currentLabels[0] === 'object' ? currentLabels : currentLabels.join()}
135 </div>
136 <dy-use class="icon" .element=${icons.expand}></dy-use>
137 `;
138 };
139 this.addEventListener('click', __classPrivateFieldGet(this, _DuoyunPickElement_onOpen, "f"));
140 this.addEventListener('keydown', commonHandle);
141 this.tabIndex = 0;
142 this.internals.role = 'combobox';
143 }
144 showPicker() {
145 __classPrivateFieldGet(this, _DuoyunPickElement_onOpen, "f").call(this);
146 }
147};
148_DuoyunPickElement_isContain = new WeakMap(), _DuoyunPickElement_genMenu = new WeakMap(), _DuoyunPickElement_onOpen = new WeakMap();
149__decorate([
150 attribute
151], DuoyunPickElement.prototype, "placeholder", void 0);
152__decorate([
153 boolattribute
154], DuoyunPickElement.prototype, "disabled", void 0);
155__decorate([
156 boolattribute
157], DuoyunPickElement.prototype, "borderless", void 0);
158__decorate([
159 boolattribute
160], DuoyunPickElement.prototype, "selectmode", void 0);
161__decorate([
162 boolattribute
163], DuoyunPickElement.prototype, "fit", void 0);
164__decorate([
165 boolattribute
166], DuoyunPickElement.prototype, "multiple", void 0);
167__decorate([
168 state
169], DuoyunPickElement.prototype, "active", void 0);
170__decorate([
171 property
172], DuoyunPickElement.prototype, "options", void 0);
173__decorate([
174 property
175], DuoyunPickElement.prototype, "value", void 0);
176__decorate([
177 globalemitter
178], DuoyunPickElement.prototype, "change", void 0);
179DuoyunPickElement = __decorate([
180 customElement('dy-picker'),
181 adoptedStyle(style),
182 adoptedStyle(pickerStyle),
183 adoptedStyle(focusStyle),
184 connectStore(icons)
185], DuoyunPickElement);
186export { DuoyunPickElement };
187//# sourceMappingURL=picker.js.map
\No newline at end of file