hortimagic 1.0.2 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ import { LitElement, css, html } from 'lit';
2
+ import { customElement, property } from 'lit/decorators.js';
3
+ import { createRef, ref } from 'lit/directives/ref.js';
4
+ import type { Ref } from 'lit/directives/ref.js';
5
+
6
+ /**
7
+ * HmSelect 是一个可自定义的下拉选择组件,基于 LitElement 构建
8
+ *
9
+ * @example
10
+ * 基本用法:
11
+ * ```html
12
+ * <hm-select .labelList="${[['选项1', 'value1'], ['选项2', 'value2']]}" @change="${(e) => logger.log('hm-select',e.detail)}"></hm-select>
13
+ * ```
14
+ *
15
+ * @example
16
+ * 禁用状态:
17
+ * ```html
18
+ * <hm-select .labelList="${[['选项1', 'value1'], ['选项2', 'value2']]}" disabled></hm-select>
19
+ * ```
20
+ *
21
+ * @example
22
+ * 带默认选中项:
23
+ * ```html
24
+ * <hm-select .labelList="${[['选项1', 'value1'], ['选项2', 'value2']]}" .index="${1}"></hm-select>
25
+ * ```
26
+ *
27
+ * @example
28
+ * 使用数字值:
29
+ * ```html
30
+ * <hm-select .labelList="${[['一', 1], ['二', 2], ['三', 3]]}" .index="${0}"></hm-select>
31
+ * ```
32
+ *
33
+ * @slot - 默认插槽,用于放置额外内容
34
+ * @fires change - 当选择项改变时触发,携带 { value, label, index } 信息
35
+ */
36
+ @customElement('hm-select')
37
+ export class HmSelect extends LitElement {
38
+ /**
39
+ * 当前选中项的索引,默认为 0
40
+ * @type {number}
41
+ */
42
+ @property({ type: Number, reflect: true })
43
+ index = 0;
44
+
45
+ /**
46
+ * 当前选中项的值
47
+ * @type {any}
48
+ */
49
+ @property({ reflect: true })
50
+ value: any = 0;
51
+
52
+ /**
53
+ * 选择项列表,每个项为 [label, value] 的数组格式
54
+ * @type {Array<Array<string | any>>}
55
+ */
56
+ @property({ type: Array, attribute: 'label-list' })
57
+ labelList: (string | any)[][] = [['0', 0]];
58
+
59
+ /**
60
+ * 是否禁用选择器
61
+ * @type {boolean}
62
+ */
63
+ @property({ type: Boolean })
64
+ disabled = false;
65
+
66
+ private selectRef: Ref<HTMLSelectElement> = createRef();
67
+
68
+ static styles = css`
69
+ :host {
70
+ display: inline-block;
71
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
72
+ font-size: 14px;
73
+ }
74
+
75
+ .select-wrapper {
76
+ position: relative;
77
+ display: inline-block;
78
+ }
79
+
80
+ select {
81
+ width: 100%;
82
+ padding: 8px 12px;
83
+ border: 1px solid #ccc;
84
+ border-radius: 4px;
85
+ background-color: #fff;
86
+ font-size: inherit;
87
+ cursor: pointer;
88
+ box-sizing: border-box;
89
+ appearance: none;
90
+ padding-right: 30px;
91
+ }
92
+
93
+ select:focus {
94
+ outline: none;
95
+ border-color: #007cba;
96
+ box-shadow: 0 0 0 2px rgba(0, 124, 186, 0.3);
97
+ }
98
+
99
+ select:disabled {
100
+ background-color: #f5f5f5;
101
+ color: #999;
102
+ cursor: not-allowed;
103
+ }
104
+
105
+ .select-icon {
106
+ position: absolute;
107
+ right: 8px;
108
+ top: 50%;
109
+ transform: translateY(-50%);
110
+ pointer-events: none;
111
+ color: #666;
112
+ }
113
+
114
+ .select-wrapper::after {
115
+ content: '';
116
+ position: absolute;
117
+ right: 10px;
118
+ top: 50%;
119
+ transform: translateY(-50%) rotate(0deg);
120
+ border-left: 5px solid transparent;
121
+ border-right: 5px solid transparent;
122
+ border-top: 5px solid #666;
123
+ pointer-events: none;
124
+ }
125
+ `;
126
+
127
+ constructor() {
128
+ super();
129
+ // 初始化时设置默认值
130
+ }
131
+
132
+ connectedCallback() {
133
+ super.connectedCallback();
134
+ // 设置默认选中项
135
+ this.index = this.index < this.labelList.length ? this.index : 0;
136
+
137
+ // 根据index设置当前值
138
+ if (this.index < this.labelList.length) {
139
+ this.value = this.labelList[this.index][1];
140
+ }
141
+ }
142
+
143
+ render() {
144
+ return html`
145
+ <div class="select-wrapper">
146
+ <select
147
+ ${ref(this.selectRef)}
148
+ ?disabled="${this.disabled}"
149
+ @change="${this._handleChange}"
150
+ >
151
+ ${this.labelList.map(([label, value], idx) => html`
152
+ <option
153
+ value="${value}"
154
+ ?selected="${idx === this.index}"
155
+ >
156
+ ${label}
157
+ </option>
158
+ `)}
159
+ </select>
160
+ <span class="select-icon"></span>
161
+ </div>
162
+ `;
163
+ }
164
+
165
+ /**
166
+ * 处理选择项改变事件
167
+ * @param {Event} e - change 事件
168
+ * @private
169
+ */
170
+ private _handleChange(e: Event) {
171
+ const selectElement = e.target as HTMLSelectElement;
172
+ const selectedIndex = selectElement.selectedIndex;
173
+ const selectedOption = selectElement.options[selectedIndex];
174
+
175
+ // 更新组件内部状态
176
+ this.index = selectedIndex;
177
+ // 从labelList中找到对应的实际值类型,而不是直接使用字符串
178
+ if (selectedIndex < this.labelList.length) {
179
+ this.value = this.labelList[selectedIndex][1];
180
+ } else {
181
+ this.value = selectedOption.value;
182
+ }
183
+
184
+ // 触发自定义事件,传递当前选中的值
185
+ this.dispatchEvent(new CustomEvent('change', {
186
+ detail: {
187
+ value: selectedOption.value,
188
+ label: selectedOption.text,
189
+ index: selectedIndex
190
+ },
191
+ bubbles: true,
192
+ composed: true
193
+ }));
194
+ }
195
+
196
+ /**
197
+ * 获取当前选中的值
198
+ * @returns {any} 当前选中的值,返回labelList中对应项的实际值类型
199
+ */
200
+ getValue(): any {
201
+ if (this.selectRef.value) {
202
+ // 获取当前选中的值
203
+ const selectValue = this.selectRef.value.value;
204
+ // 尝试从labelList中找到匹配项并返回原始值
205
+ const selectedIndex = Array.from(this.selectRef.value.options)
206
+ .findIndex(option => option.value === selectValue);
207
+
208
+ if (selectedIndex !== -1 && selectedIndex < this.labelList.length) {
209
+ return this.labelList[selectedIndex][1];
210
+ }
211
+
212
+ // 如果没找到匹配项,返回当前DOM值
213
+ return selectValue;
214
+ }
215
+ return this.value;
216
+ }
217
+
218
+ /**
219
+ * 设置选中值
220
+ * @param {any} value - 要设置的值,可以是字符串或数字等类型
221
+ */
222
+ setValue(value: any) {
223
+ // 查找labelList中匹配的索引
224
+ const idx = this.labelList.findIndex(item => item[1] === value);
225
+
226
+ if (this.selectRef.value) {
227
+ if (idx !== -1) {
228
+ // 如果找到匹配项,使用该项的实际值(转换为字符串设置到DOM)
229
+ this.selectRef.value.value = String(this.labelList[idx][1]);
230
+ } else {
231
+ // 如果未找到匹配项,尝试直接设置值
232
+ this.selectRef.value.value = String(value);
233
+ }
234
+ }
235
+
236
+ // 同时更新组件内部的value和index
237
+ this.value = value;
238
+ if (idx !== -1) {
239
+ this.index = idx;
240
+ }
241
+ }
242
+ }
@@ -1,5 +1,6 @@
1
1
  import { LitElement, css, html } from 'lit'
2
2
  import { customElement, property, query } from 'lit/decorators.js'
3
+ import { logger } from '../core/log-tools';
3
4
 
4
5
  /**
5
6
  * 滑动单元格组件
@@ -36,7 +37,7 @@ export class HmSwipeCell extends LitElement {
36
37
  */
37
38
  @property()
38
39
  rightButtonCallback = function () {
39
- console.debug('点击了一下');
40
+ logger.log('cell', '点击了一下');
40
41
  };
41
42
 
42
43
  @query('.slider') sliderElement!: HTMLElement;
@@ -361,9 +362,7 @@ export class HmSwipeCell extends LitElement {
361
362
  <slot name="left-actions"> </slot>
362
363
  </div>
363
364
  <div class="slider">
364
- <slot name="content" class="content">
365
- <hm-cell></hm-cell>
366
- </slot>
365
+ <slot><hm-cell class="content"></hm-cell></slot>
367
366
  </div>
368
367
  <div class="actions right-actions">
369
368
  <slot name="right-actions">
@@ -1,5 +1,6 @@
1
1
  import { LitElement, html, css } from 'lit';
2
2
  import { customElement, property } from 'lit/decorators.js';
3
+ // import { logger } from '../core/log-tools';
3
4
 
4
5
  /**
5
6
  * 滑动开关组件
@@ -25,7 +26,7 @@ import { customElement, property } from 'lit/decorators.js';
25
26
  * <hm-switch openIcon="check" closeIcon="close"></hm-switch>
26
27
  *
27
28
  * <!-- 监听状态变化 -->
28
- * <hm-switch @hm-switch-change="${(e) => console.log('开关状态:', e.detail.checked)}"></hm-switch>
29
+ * <hm-switch @hm-switch-change="${(e) => logger.log('hm-switch','开关状态:', e.detail.checked)}"></hm-switch>
29
30
  * ```
30
31
  */
31
32
  @customElement('hm-switch')
@@ -41,20 +42,20 @@ export class HmSwitch extends LitElement {
41
42
  @property({ type: String }) color = '#1890ff';
42
43
 
43
44
  /** 自定义开启状态内容 */
44
- @property({ type: String }) openContent = '';
45
+ @property({ type: String, attribute: 'open-content' }) openContent = '';
45
46
 
46
47
  /** 自定义关闭状态内容 */
47
- @property({ type: String }) closeContent = '';
48
+ @property({ type: String, attribute: 'close-content' }) closeContent = '';
48
49
  /** 自定义开启状态图标 */
49
- @property({ type: String }) openIcon = '';
50
+ @property({ type: String, attribute: 'open-icon' }) openIcon = '';
50
51
 
51
52
  /** 自定义关闭状态图标 */
52
- @property({ type: String }) closeIcon = '';
53
+ @property({ type: String, attribute: 'close-icon' }) closeIcon = '';
53
54
 
54
55
  change() {
55
56
  if (this.disabled || this.loading) return;
56
57
  this.checked = !this.checked;
57
- // console.debug('changed!!');
58
+ // logger.log('hm-switch', 'changed!!');
58
59
  // 触发自定义事件供外部监听
59
60
  this.dispatchEvent(new CustomEvent('hm-switch-change', {
60
61
  detail: { checked: this.checked },
@@ -4,6 +4,7 @@ export * as hm_menu from './hm-menu';
4
4
  export * as hm_notification from './hm-notification';
5
5
  export * as hm_button from './hm-button';
6
6
  export * as hm_cell from './hm-cell';
7
+ export * as hm_select from './hm-select';
7
8
  export * as hm_swipe_cell from './hm-swipe-cell';
8
9
  export * as hm_switch from './hm-switch';
9
10
  export * as hm_accordion from './hm-accordion';
@@ -1,26 +1,30 @@
1
1
  import type { HmNotification } from "./hm-notification";
2
+ import type { HmButton } from "./hm-button";
2
3
  import type { HmIcon } from "./hm-icon";
3
4
  import type { HmMenu } from "./hm-menu";
4
5
  import type { HmMovePanel } from "./hm-move-panel";
5
6
  import type { HmCell } from "./hm-cell";
7
+ import type { HmSelect } from "./hm-select";
6
8
  import type { HmSwipeCell } from "./hm-swipe-cell";
7
- import type { HcSwitch, HmSwitch } from "./hm-switch";
9
+ import type { HmSwitch } from "./hm-switch";
8
10
  import type { HmAccordion } from "./hm-accordion";
9
11
  import type { HmInput } from "./hm-input";
10
12
  import type { HmDialog } from "./hm-dialog";
11
13
 
12
14
  declare global {
13
15
  interface HTMLElementTagNameMap {
14
- "hm-icon": HmIcon;
16
+ 'hm-accordion': HmAccordion;
17
+ 'hm-button': HmButton;
18
+ 'hm-cell': HmCell;
19
+ 'hm-dialog': HmDialog;
20
+ 'hm-input': HmInput;
15
21
  'hm-menu': HmMenu
16
22
  "hm-move-panel": HmMovePanel;
17
23
  "hm-notification": HmNotification;
18
- 'hm-cell': HmCell;
24
+ "hm-select": HmSelect,
19
25
  "hm-swipe-cell": HmSwipeCell;
20
26
  'hm-switch': HmSwitch;
21
- 'hm-accordion': HmAccordion;
22
- 'hm-input': HmInput;
23
- 'hm-dialog': HmDialog;
27
+ "hm-icon": HmIcon;
24
28
  }
25
29
  }
26
30