@kubex/zinc 1.0.20 → 1.0.22

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubex/zinc",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "A collection of web components for building web applications based off of @shoelace-style/Shoelace",
5
5
  "keywords": [
6
6
  "web components",
@@ -156,6 +156,7 @@ export default class ZnInlineEdit extends ZincElement implements ZincFormControl
156
156
  submitKeyHandler = (e: KeyboardEvent) => {
157
157
  if (e.key === 'Enter' && this.isEditing) {
158
158
  this.isEditing = false;
159
+ this.emit('zn-submit', {detail: {value: this.value, element: this}});
159
160
  this.formControlController.submit();
160
161
  this.input.blur();
161
162
  }
@@ -186,6 +187,7 @@ export default class ZnInlineEdit extends ZincElement implements ZincFormControl
186
187
  handleSubmitClick = (e: MouseEvent) => {
187
188
  e.preventDefault();
188
189
  this.isEditing = false;
190
+ this.emit('zn-submit', {detail: {value: this.value, element: this}});
189
191
  this.formControlController.submit();
190
192
  };
191
193
 
@@ -1,5 +1,6 @@
1
1
  import {classMap} from "lit/directives/class-map.js";
2
2
  import {type CSSResultGroup, html, type PropertyValues, unsafeCSS} from 'lit';
3
+ import {ifDefined} from "lit/directives/if-defined.js";
3
4
  import {property} from 'lit/decorators.js';
4
5
  import ZincElement from '../../internal/zinc-element';
5
6
  import ZnDropdown from "../dropdown";
@@ -41,6 +42,7 @@ export default class ZnNavbar extends ZincElement {
41
42
  @property({type: Array}) dropdown = [];
42
43
  @property({attribute: "no-pad", type: Boolean}) noPad: false
43
44
  @property({attribute: 'manual-add-items', type: Boolean}) manualAddItems = false;
45
+ @property({type: Boolean}) isolated = false;
44
46
 
45
47
  private _preItems: NodeListOf<Element>;
46
48
  private _postItems: NodeListOf<Element>;
@@ -194,6 +196,17 @@ export default class ZnNavbar extends ZincElement {
194
196
  //console.log("Showing More")
195
197
  }
196
198
 
199
+ private handleClick = (e: MouseEvent) => {
200
+ const path = e.composedPath();
201
+ const tabAttr = this.isolated ? 'data-tab' : 'tab';
202
+ const li = path.find(el => el instanceof HTMLElement && el.tagName === 'LI' && (el.hasAttribute(tabAttr) || el.hasAttribute('tab-uri'))) as HTMLElement;
203
+
204
+ if (li) {
205
+ e.stopPropagation();
206
+ this.emit('zn-select', {detail: {item: li}});
207
+ }
208
+ };
209
+
197
210
  render() {
198
211
  if (!this.navigation) {
199
212
  this.navigation = [];
@@ -211,7 +224,7 @@ export default class ZnNavbar extends ZincElement {
211
224
  'navbar__container--stacked': this.stacked,
212
225
  'navbar__container--icon-bar': this.iconBar
213
226
  })}">
214
- <ul class="${classMap({
227
+ <ul @click="${this.handleClick}" class="${classMap({
215
228
  'navbar': true,
216
229
  'navbar--slim': this.slim,
217
230
  'navbar--border': this.border,
@@ -232,7 +245,11 @@ export default class ZnNavbar extends ZincElement {
232
245
  <li class="${classMap({'active': item.active})}" tab-uri="${item.path}">${content}</li>`;
233
246
  }
234
247
  return html`
235
- <li class="${classMap({'active': item.active})}" tab="${item.tab}">${content}</li>`;
248
+ <li class="${classMap({'active': item.active})}"
249
+ tab="${ifDefined(!this.isolated ? item.tab : undefined)}"
250
+ data-tab="${ifDefined(this.isolated ? item.tab : undefined)}">
251
+ ${content}
252
+ </li>`;
236
253
  })}
237
254
  <li class="more">
238
255
  <zn-dropdown placement="bottom-end" id="extended-dropdown">
@@ -10,6 +10,7 @@ import ZnNavbar from '../navbar';
10
10
  import type {PropertyValues} from 'lit';
11
11
  import type {ZincFormControl} from '../../internal/zinc-element';
12
12
  import type {ZnMenuSelectEvent} from '../../events/zn-menu-select';
13
+ import type {ZnSelectEvent} from "../../events/zn-select";
13
14
 
14
15
  import styles from './translations.scss';
15
16
 
@@ -35,7 +36,7 @@ export default class ZnTranslations extends ZincElement implements ZincFormContr
35
36
  };
36
37
  @property({type: Object}) values: Record<string, string> = {};
37
38
 
38
- @state() private _activeLanguage = '';
39
+ @state() private _activeLanguage = 'en';
39
40
 
40
41
  get validity(): ValidityState {
41
42
  return validValidityState;
@@ -99,18 +100,18 @@ export default class ZnTranslations extends ZincElement implements ZincFormContr
99
100
  this.value = JSON.stringify(this.values);
100
101
 
101
102
  // Ensure active language is valid
102
- if (!this._activeLanguage || !Object.prototype.hasOwnProperty.call(this.values, this._activeLanguage)) {
103
+ if (!this._activeLanguage || (!Object.prototype.hasOwnProperty.call(this.values, this._activeLanguage) && this._activeLanguage !== 'en')) {
103
104
  const keys = Object.keys(this.values);
104
105
  if (keys.length > 0) {
105
106
  this._activeLanguage = keys[0];
106
107
  } else {
107
- this._activeLanguage = '';
108
+ this._activeLanguage = 'en';
108
109
  }
109
110
  }
110
111
  }
111
112
  }
112
113
 
113
- private handleLanguageAdd(e: ZnMenuSelectEvent) {
114
+ private handleLanguageAdd = (e: ZnMenuSelectEvent) => {
114
115
  const element = e.detail.element as HTMLElement;
115
116
  const languageCode = element.getAttribute('data-path');
116
117
  if (languageCode) {
@@ -119,21 +120,21 @@ export default class ZnTranslations extends ZincElement implements ZincFormContr
119
120
  this._activeLanguage = languageCode;
120
121
  this.updateValue();
121
122
  }
122
- }
123
+ };
123
124
 
124
- private handleNavbarClick(e: MouseEvent) {
125
- const path = e.composedPath();
126
- const li = path.find(el => el instanceof HTMLElement && el.tagName === 'LI' && el.hasAttribute('tab')) as HTMLElement;
125
+ private handleNavbarClick = (e: ZnSelectEvent) => {
126
+ e.stopPropagation();
127
+ const li = e.detail.item as HTMLElement;
127
128
  if (li) {
128
- const tab = li.getAttribute('tab');
129
+ const tab = li.getAttribute('tab') || li.getAttribute('data-tab');
129
130
  if (tab) {
130
131
  this._activeLanguage = tab;
131
132
  this.requestUpdate();
132
133
  }
133
134
  }
134
- }
135
+ };
135
136
 
136
- private handleValueUpdate(e: CustomEvent) {
137
+ private handleValueUpdate = (e: CustomEvent) => {
137
138
  const target = e.target as (ZnInput | ZnInlineEdit);
138
139
  if (this._activeLanguage) {
139
140
  const newValue: string = target.value as string;
@@ -142,7 +143,7 @@ export default class ZnTranslations extends ZincElement implements ZincFormContr
142
143
  this.updateValue();
143
144
  }
144
145
  }
145
- }
146
+ };
146
147
 
147
148
  private updateValue() {
148
149
  this.value = JSON.stringify(this.values);
@@ -150,23 +151,45 @@ export default class ZnTranslations extends ZincElement implements ZincFormContr
150
151
  this.emit('zn-input');
151
152
  }
152
153
 
154
+ private handleKeyDown = (event: KeyboardEvent) => {
155
+ if (event.key === 'Enter') {
156
+ if (event.target instanceof ZnInlineEdit) {
157
+ return;
158
+ }
159
+
160
+ const hasModifier = event.altKey || event.ctrlKey || event.metaKey || event.shiftKey;
161
+
162
+ if (!hasModifier && !event.defaultPrevented && !event.isComposing) {
163
+ this.formControlController.submit();
164
+ }
165
+ }
166
+ };
167
+
168
+ private handleSubmit = () => {
169
+ this.formControlController.submit();
170
+ };
171
+
153
172
  render() {
154
173
  const availableLanguages = Object.entries(this.languages)
155
- .filter(([code]) => !Object.prototype.hasOwnProperty.call(this.values, code))
174
+ .filter(([code]) => code !== 'en' && !Object.prototype.hasOwnProperty.call(this.values, code))
156
175
  .map(([code, name]) => ({
157
176
  title: name,
158
177
  type: 'dropdown',
159
178
  path: code
160
179
  }));
161
180
 
162
- const navigation = Object.keys(this.values).map(code => ({
181
+ const visibleTabs = Object.keys(this.values);
182
+ if (!visibleTabs.includes('en')) {
183
+ visibleTabs.unshift('en');
184
+ }
185
+
186
+ const navigation = visibleTabs.map(code => ({
163
187
  title: this.languages[code] || code,
164
188
  active: code === this._activeLanguage,
165
189
  tab: code
166
190
  }));
167
191
 
168
192
  const currentTranslation = this.values[this._activeLanguage];
169
- const useInlineEdit = currentTranslation && currentTranslation.length > 0;
170
193
 
171
194
  const hasLabelSlot = this.hasSlotController.test('label');
172
195
  const hasLabel = this.label ? true : hasLabelSlot;
@@ -178,7 +201,8 @@ export default class ZnTranslations extends ZincElement implements ZincFormContr
178
201
  'form-control': true,
179
202
  'form-control--medium': true,
180
203
  'form-control--has-label': hasLabel,
181
- })}">
204
+ })}"
205
+ @keydown="${this.handleKeyDown}">
182
206
  <label part="form-control-label" class="form-control__label" for="input"
183
207
  aria-hidden=${hasLabel ? 'false' : 'true'}>
184
208
  <slot name="label">${this.label}</slot>
@@ -187,26 +211,21 @@ export default class ZnTranslations extends ZincElement implements ZincFormContr
187
211
  <zn-navbar
188
212
  .navigation="${navigation}"
189
213
  .dropdown="${availableLanguages}"
190
- @click="${this.handleNavbarClick}"
214
+ name="${this.name}-translations-navbar"
215
+ isolated
216
+ @zn-select="${this.handleNavbarClick}"
191
217
  @zn-menu-select="${this.handleLanguageAdd}"
192
218
  manual-add-items
193
219
  ></zn-navbar>
194
220
  <div class="input-container">
195
- ${this._activeLanguage ? html`
196
- ${useInlineEdit ? html`
197
- <zn-inline-edit
198
- .value=${currentTranslation}
199
- @zn-change="${this.handleValueUpdate}"
200
- @zn-input="${this.handleValueUpdate}"
201
- ></zn-inline-edit>
202
- ` : html`
203
- <zn-input
204
- .value="${currentTranslation || ''}"
205
- placeholder="Enter translation..."
206
- @zn-change="${this.handleValueUpdate}"
207
- ></zn-input>
208
- `}
209
- ` : ''}
221
+ <zn-inline-edit
222
+ .value=${currentTranslation}
223
+ name="${this.name}"
224
+ placeholder="Enter translation..."
225
+ @zn-change="${this.handleValueUpdate}"
226
+ @zn-input="${this.handleValueUpdate}"
227
+ @zn-submit="${this.handleSubmit}"
228
+ ></zn-inline-edit>
210
229
  </div>
211
230
  </div>
212
231
  `;
@@ -1,6 +1,6 @@
1
1
  import type ZnMenuItem from "../components/menu-item";
2
2
 
3
- export type ZnSelectEvent = CustomEvent<{ item: ZnMenuItem }>;
3
+ export type ZnSelectEvent = CustomEvent<{ item: ZnMenuItem | HTMLElement }>;
4
4
 
5
5
  declare global {
6
6
  interface GlobalEventHandlersEventMap {