UNPKG

7.43 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 _DuoyunDatePickElement_instances, _DuoyunDatePickElement_value_get, _DuoyunDatePickElement_valueString_get, _DuoyunDatePickElement_onSubmit, _DuoyunDatePickElement_onOpen, _DuoyunDatePickElement_onClear;
13import { connectStore, adoptedStyle, customElement, attribute, emitter, 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 { Time } from '../lib/time';
17import { theme } from '../lib/theme';
18import { icons } from '../lib/icons';
19import { locale } from '../lib/locale';
20import { isNotNullish } from '../lib/types';
21import { commonHandle } from '../lib/hotkeys';
22import { focusStyle } from '../lib/styles';
23import { ContextMenu } from './menu';
24import { pickerStyle } from './picker';
25import './use';
26import './date-panel';
27import './button';
28const style = createCSSSheet(css `
29 :host {
30 width: 15em;
31 }
32 .value {
33 flex-grow: 1;
34 overflow: hidden;
35 text-overflow: ellipsis;
36 white-space: nowrap;
37 }
38 .placeholder {
39 color: ${theme.describeColor};
40 }
41 .icon {
42 display: flex;
43 }
44 .close,
45 :host(:not([disabled])) .clearable:hover .date {
46 display: none;
47 }
48 :host(:not([disabled])) .clearable:hover .close {
49 display: inline-flex;
50 }
51`);
52/**
53 * @customElement dy-date-picker
54 * @attr placeholder
55 * @attr time
56 * @attr clearable
57 * @attr disabled
58 * @fires change
59 * @fires clear
60 */
61let DuoyunDatePickElement = class DuoyunDatePickElement extends GemElement {
62 constructor() {
63 super();
64 _DuoyunDatePickElement_instances.add(this);
65 _DuoyunDatePickElement_onSubmit.set(this, ({ detail }) => {
66 this.change(detail);
67 ContextMenu.close();
68 });
69 _DuoyunDatePickElement_onOpen.set(this, () => {
70 if (this.disabled)
71 return;
72 let v = undefined;
73 const onChange = ({ detail, target }) => {
74 const panel = target;
75 panel.highlights = [];
76 panel.value = detail;
77 const root = panel.getRootNode();
78 const button = root.querySelector('dy-button');
79 button.disabled = false;
80 v = detail;
81 };
82 const onSubmit = () => {
83 if (isNotNullish(v)) {
84 this.change(v);
85 ContextMenu.close();
86 }
87 };
88 ContextMenu.open(html `
89 <dy-date-panel
90 style="border: none;"
91 .value=${__classPrivateFieldGet(this, _DuoyunDatePickElement_instances, "a", _DuoyunDatePickElement_value_get)}
92 ?time=${this.time}
93 @change=${this.time ? onChange : __classPrivateFieldGet(this, _DuoyunDatePickElement_onSubmit, "f")}
94 ></dy-date-panel>
95 ${this.time
96 ? html `
97 <style>
98 .footer {
99 margin-block-start: 2em;
100 display: flex;
101 justify-content: flex-end;
102 }
103 </style>
104 <div class="footer">
105 <dy-button disabled @click=${onSubmit} small>${locale.ok}</dy-button>
106 </div>
107 `
108 : ''}
109 `, {
110 activeElement: this,
111 width: this.time ? '30em' : undefined,
112 });
113 });
114 _DuoyunDatePickElement_onClear.set(this, (e) => {
115 e.stopPropagation();
116 this.clear(null);
117 });
118 this.mounted = () => {
119 return () => this.active && ContextMenu.close();
120 };
121 this.render = () => {
122 return html `
123 <div class=${classMap({ value: true, placeholder: !__classPrivateFieldGet(this, _DuoyunDatePickElement_instances, "a", _DuoyunDatePickElement_value_get) })}>${__classPrivateFieldGet(this, _DuoyunDatePickElement_instances, "a", _DuoyunDatePickElement_valueString_get) || this.placeholder}</div>
124 <div class=${classMap({ icon: true, clearable: this.clearable })}>
125 <dy-use class="date" .element=${icons.date}></dy-use>
126 <dy-use class="close" @click=${__classPrivateFieldGet(this, _DuoyunDatePickElement_onClear, "f")} .element=${icons.close}></dy-use>
127 </div>
128 `;
129 };
130 this.addEventListener('click', __classPrivateFieldGet(this, _DuoyunDatePickElement_onOpen, "f"));
131 this.addEventListener('keydown', commonHandle);
132 this.tabIndex = 0;
133 this.internals.role = 'combobox';
134 }
135 showPicker() {
136 __classPrivateFieldGet(this, _DuoyunDatePickElement_onOpen, "f").call(this);
137 }
138};
139_DuoyunDatePickElement_onSubmit = new WeakMap(), _DuoyunDatePickElement_onOpen = new WeakMap(), _DuoyunDatePickElement_onClear = new WeakMap(), _DuoyunDatePickElement_instances = new WeakSet(), _DuoyunDatePickElement_value_get = function _DuoyunDatePickElement_value_get() {
140 return isNotNullish(this.value) ? this.value : undefined;
141}, _DuoyunDatePickElement_valueString_get = function _DuoyunDatePickElement_valueString_get() {
142 return __classPrivateFieldGet(this, _DuoyunDatePickElement_instances, "a", _DuoyunDatePickElement_value_get) && new Time(__classPrivateFieldGet(this, _DuoyunDatePickElement_instances, "a", _DuoyunDatePickElement_value_get)).format(this.time ? undefined : 'YYYY-MM-DD');
143};
144__decorate([
145 attribute
146], DuoyunDatePickElement.prototype, "placeholder", void 0);
147__decorate([
148 boolattribute
149], DuoyunDatePickElement.prototype, "time", void 0);
150__decorate([
151 boolattribute
152], DuoyunDatePickElement.prototype, "clearable", void 0);
153__decorate([
154 boolattribute
155], DuoyunDatePickElement.prototype, "disabled", void 0);
156__decorate([
157 state
158], DuoyunDatePickElement.prototype, "active", void 0);
159__decorate([
160 property
161], DuoyunDatePickElement.prototype, "value", void 0);
162__decorate([
163 globalemitter
164], DuoyunDatePickElement.prototype, "change", void 0);
165__decorate([
166 emitter
167], DuoyunDatePickElement.prototype, "clear", void 0);
168DuoyunDatePickElement = __decorate([
169 customElement('dy-date-picker'),
170 adoptedStyle(pickerStyle),
171 adoptedStyle(style),
172 adoptedStyle(focusStyle),
173 connectStore(icons)
174], DuoyunDatePickElement);
175export { DuoyunDatePickElement };
176//# sourceMappingURL=date-picker.js.map
\No newline at end of file