UNPKG

7.28 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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
13 if (kind === "m") throw new TypeError("Private method is not writable");
14 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
15 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
16 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
17};
18var _DuoyunCascaderPickElement_cascader, _DuoyunCascaderPickElement_onChange, _DuoyunCascaderPickElement_onOpen, _DuoyunCascaderPickElement_renderValue, _DuoyunCascaderPickElement_renderMultipleValue;
19import { adoptedStyle, customElement, attribute, globalemitter, property, boolattribute, state, } from '@mantou/gem/lib/decorators';
20import { GemElement, html } from '@mantou/gem/lib/element';
21import { createCSSSheet, css, classMap } from '@mantou/gem/lib/utils';
22import { theme } from '../lib/theme';
23import { icons } from '../lib/icons';
24import { getCascaderDeep } from '../lib/utils';
25import { commonHandle } from '../lib/hotkeys';
26import { focusStyle } from '../lib/styles';
27import { ContextMenu } from './menu';
28import { pickerStyle } from './picker';
29import './use';
30import './cascader';
31import './tag';
32const style = createCSSSheet(css `
33 :host {
34 width: 15em;
35 }
36 .value {
37 flex-grow: 1;
38 overflow: hidden;
39 text-overflow: ellipsis;
40 white-space: nowrap;
41 display: flex;
42 gap: 0.5em;
43 }
44 .placeholder {
45 color: ${theme.describeColor};
46 }
47`);
48/**
49 * @customElement dy-cascader-picker
50 */
51let DuoyunCascaderPickElement = class DuoyunCascaderPickElement extends GemElement {
52 constructor() {
53 super();
54 _DuoyunCascaderPickElement_cascader.set(this, void 0);
55 _DuoyunCascaderPickElement_onChange.set(this, ({ detail, target }) => {
56 __classPrivateFieldSet(this, _DuoyunCascaderPickElement_cascader, target, "f");
57 this.change(detail);
58 if (!this.multiple) {
59 ContextMenu.close();
60 }
61 });
62 _DuoyunCascaderPickElement_onOpen.set(this, () => {
63 var _a;
64 if (this.disabled || !((_a = this.options) === null || _a === void 0 ? void 0 : _a.length))
65 return;
66 ContextMenu.open(html `
67 <dy-cascader
68 style="margin: -0.4em -1em; min-height: 10em;"
69 .options=${this.options}
70 .value=${this.value}
71 @change=${(evt) => __classPrivateFieldGet(this, _DuoyunCascaderPickElement_onChange, "f").call(this, evt)}
72 ?multiple=${this.multiple}
73 >
74 ${this.fit
75 ? html `
76 <style>
77 dy-cascader::part(column) {
78 width: ${this.getBoundingClientRect().width / getCascaderDeep(this.options, 'children')}px;
79 }
80 </style>
81 `
82 : ''}
83 </dy-cascader>
84 `, {
85 activeElement: this,
86 width: 'auto',
87 });
88 });
89 _DuoyunCascaderPickElement_renderValue.set(this, (value) => {
90 return html `${value.join(' / ')}`;
91 });
92 _DuoyunCascaderPickElement_renderMultipleValue.set(this, (value) => {
93 return html ` ${value.map((e) => html `<dy-tag small>${e.join(' / ')}</dy-tag>`)} `;
94 });
95 this.mounted = () => {
96 this.effect(() => {
97 if (__classPrivateFieldGet(this, _DuoyunCascaderPickElement_cascader, "f")) {
98 __classPrivateFieldGet(this, _DuoyunCascaderPickElement_cascader, "f").value = this.value;
99 }
100 }, () => [this.value]);
101 return () => this.active && ContextMenu.close();
102 };
103 this.render = () => {
104 var _a;
105 const isEmpty = this.multiple ? !((_a = this.value) === null || _a === void 0 ? void 0 : _a.length) : !this.value;
106 return html `
107 <div class=${classMap({ value: true, placeholder: isEmpty })}>
108 ${isEmpty
109 ? this.placeholder
110 : this.multiple
111 ? __classPrivateFieldGet(this, _DuoyunCascaderPickElement_renderMultipleValue, "f").call(this, this.value)
112 : __classPrivateFieldGet(this, _DuoyunCascaderPickElement_renderValue, "f").call(this, this.value)}
113 </div>
114 <dy-use class="icon" .element=${icons.expand}></dy-use>
115 `;
116 };
117 this.addEventListener('click', __classPrivateFieldGet(this, _DuoyunCascaderPickElement_onOpen, "f"));
118 this.addEventListener('keydown', commonHandle);
119 this.tabIndex = 0;
120 this.internals.role = 'combobox';
121 }
122 showPicker() {
123 __classPrivateFieldGet(this, _DuoyunCascaderPickElement_onOpen, "f").call(this);
124 }
125};
126_DuoyunCascaderPickElement_cascader = new WeakMap(), _DuoyunCascaderPickElement_onChange = new WeakMap(), _DuoyunCascaderPickElement_onOpen = new WeakMap(), _DuoyunCascaderPickElement_renderValue = new WeakMap(), _DuoyunCascaderPickElement_renderMultipleValue = new WeakMap();
127__decorate([
128 attribute
129], DuoyunCascaderPickElement.prototype, "placeholder", void 0);
130__decorate([
131 property
132], DuoyunCascaderPickElement.prototype, "options", void 0);
133__decorate([
134 boolattribute
135], DuoyunCascaderPickElement.prototype, "fit", void 0);
136__decorate([
137 boolattribute
138], DuoyunCascaderPickElement.prototype, "disabled", void 0);
139__decorate([
140 boolattribute
141], DuoyunCascaderPickElement.prototype, "multiple", void 0);
142__decorate([
143 globalemitter
144], DuoyunCascaderPickElement.prototype, "change", void 0);
145__decorate([
146 property
147], DuoyunCascaderPickElement.prototype, "value", void 0);
148__decorate([
149 state
150], DuoyunCascaderPickElement.prototype, "active", void 0);
151DuoyunCascaderPickElement = __decorate([
152 customElement('dy-cascader-picker'),
153 adoptedStyle(style),
154 adoptedStyle(pickerStyle),
155 adoptedStyle(focusStyle)
156], DuoyunCascaderPickElement);
157export { DuoyunCascaderPickElement };
158//# sourceMappingURL=cascader-picker.js.map
\No newline at end of file