@api-client/ui 0.5.0 → 0.5.2

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.
Files changed (35) hide show
  1. package/build/src/elements/highlight/MarkedHighlight.d.ts.map +1 -1
  2. package/build/src/elements/highlight/MarkedHighlight.js +2 -1
  3. package/build/src/elements/highlight/MarkedHighlight.js.map +1 -1
  4. package/build/src/md/button/internals/base.js +1 -1
  5. package/build/src/md/button/internals/base.js.map +1 -1
  6. package/build/src/md/dialog/internals/Dialog.d.ts +18 -0
  7. package/build/src/md/dialog/internals/Dialog.d.ts.map +1 -1
  8. package/build/src/md/dialog/internals/Dialog.js +60 -2
  9. package/build/src/md/dialog/internals/Dialog.js.map +1 -1
  10. package/build/src/md/input/Input.d.ts +4 -4
  11. package/build/src/md/input/Input.d.ts.map +1 -1
  12. package/build/src/md/input/Input.js +3 -11
  13. package/build/src/md/input/Input.js.map +1 -1
  14. package/build/src/md/text-area/internals/TextAreaElement.d.ts.map +1 -1
  15. package/build/src/md/text-area/internals/TextAreaElement.js +1 -2
  16. package/build/src/md/text-area/internals/TextAreaElement.js.map +1 -1
  17. package/build/src/md/text-field/internals/TextField.d.ts.map +1 -1
  18. package/build/src/md/text-field/internals/TextField.js +1 -2
  19. package/build/src/md/text-field/internals/TextField.js.map +1 -1
  20. package/demo/md/dialog/dialog.ts +135 -1
  21. package/package.json +2 -2
  22. package/src/elements/highlight/MarkedHighlight.ts +2 -1
  23. package/src/md/button/internals/base.ts +1 -1
  24. package/src/md/dialog/internals/Dialog.ts +54 -1
  25. package/src/md/input/Input.ts +4 -4
  26. package/src/md/text-area/internals/TextAreaElement.ts +1 -2
  27. package/src/md/text-field/internals/TextField.ts +4 -5
  28. package/test/README.md +372 -0
  29. package/test/dom-assertions.test.ts +182 -0
  30. package/test/helpers/TestUtils.ts +243 -0
  31. package/test/helpers/UiMock.ts +83 -13
  32. package/test/md/dialog/UiDialog.test.ts +169 -0
  33. package/test/setup.test.ts +217 -0
  34. package/test/setup.ts +117 -0
  35. package/.github/workflows/test.yml +0 -42
@@ -18,6 +18,8 @@ class ComponentDemoPage extends DemoPage {
18
18
 
19
19
  @reactive() accessor overflowOpened = false
20
20
 
21
+ @reactive() accessor formOpened = false
22
+
21
23
  protected openSimple(): void {
22
24
  this.simpleOpened = true
23
25
  }
@@ -45,6 +47,15 @@ class ComponentDemoPage extends DemoPage {
45
47
  this.reportClosingReason(e.detail)
46
48
  }
47
49
 
50
+ protected openForm(): void {
51
+ this.formOpened = true
52
+ }
53
+
54
+ protected formClosed(e: CustomEvent<UiDialogClosingReason>): void {
55
+ this.formOpened = false
56
+ this.reportClosingReason(e.detail)
57
+ }
58
+
48
59
  imperativeDialog: UiDialog | null = null
49
60
 
50
61
  protected openImperative(): void {
@@ -81,7 +92,8 @@ class ComponentDemoPage extends DemoPage {
81
92
  contentTemplate(): TemplateResult {
82
93
  return html`
83
94
  <a href="../">Back</a>
84
- ${this.simpleDialog()} ${this.fullDialog()} ${this.overflowDialog()} ${this.renderImperativeDialog()}
95
+ ${this.simpleDialog()} ${this.fullDialog()} ${this.overflowDialog()} ${this.formDialog()}
96
+ ${this.renderImperativeDialog()}
85
97
  `
86
98
  }
87
99
 
@@ -244,6 +256,128 @@ class ComponentDemoPage extends DemoPage {
244
256
  `
245
257
  }
246
258
 
259
+ formDialog(): TemplateResult {
260
+ return html`
261
+ <section class="demo-section">
262
+ <h2 class="title-large">Form dialog</h2>
263
+ <p>This dialog contains a form with validation and submit handling.</p>
264
+ <ui-button color="filled" @click="${this.openForm}">Open Form Dialog</ui-button>
265
+ <form @submit="${this.handleFormSubmit}" style="display: contents;">
266
+ <ui-dialog
267
+ ?open="${this.formOpened}"
268
+ @close="${this.formClosed}"
269
+ modal
270
+ style="--ui-dialog-max-width: 500px;"
271
+ submitClose
272
+ >
273
+ <ui-icon slot="icon" icon="info"></ui-icon>
274
+ <span slot="title">User Registration</span>
275
+
276
+ <div style="display: flex; flex-direction: column; gap: 16px;">
277
+ <label style="display: flex; flex-direction: column; gap: 4px;">
278
+ <span style="font-weight: 500; color: var(--md-sys-color-on-surface);">Full Name *</span>
279
+ <input
280
+ type="text"
281
+ name="fullName"
282
+ required
283
+ style="padding: 12px; border: 1px solid var(--md-sys-color-outline); border-radius: 4px; font-size: 14px;"
284
+ placeholder="Enter your full name"
285
+ />
286
+ </label>
287
+
288
+ <label style="display: flex; flex-direction: column; gap: 4px;">
289
+ <span style="font-weight: 500; color: var(--md-sys-color-on-surface);">Email *</span>
290
+ <input
291
+ type="email"
292
+ name="email"
293
+ required
294
+ style="padding: 12px; border: 1px solid var(--md-sys-color-outline); border-radius: 4px; font-size: 14px;"
295
+ placeholder="Enter your email address"
296
+ />
297
+ </label>
298
+
299
+ <label style="display: flex; flex-direction: column; gap: 4px;">
300
+ <span style="font-weight: 500; color: var(--md-sys-color-on-surface);">Phone Number</span>
301
+ <input
302
+ type="tel"
303
+ name="phone"
304
+ style="padding: 12px; border: 1px solid var(--md-sys-color-outline); border-radius: 4px; font-size: 14px;"
305
+ placeholder="Enter your phone number (optional)"
306
+ />
307
+ </label>
308
+
309
+ <label style="display: flex; flex-direction: column; gap: 4px;">
310
+ <span style="font-weight: 500; color: var(--md-sys-color-on-surface);">Department</span>
311
+ <select
312
+ name="department"
313
+ style="padding: 12px; border: 1px solid var(--md-sys-color-outline); border-radius: 4px; font-size: 14px; background: white;"
314
+ >
315
+ <option value="">Select a department</option>
316
+ <option value="engineering">Engineering</option>
317
+ <option value="design">Design</option>
318
+ <option value="marketing">Marketing</option>
319
+ <option value="sales">Sales</option>
320
+ <option value="support">Support</option>
321
+ </select>
322
+ </label>
323
+
324
+ <label style="display: flex; align-items: center; gap: 8px; margin-top: 8px;">
325
+ <input type="checkbox" name="newsletter" style="margin: 0;" />
326
+ <span style="font-size: 14px; color: var(--md-sys-color-on-surface);">
327
+ Subscribe to our newsletter
328
+ </span>
329
+ </label>
330
+
331
+ <label style="display: flex; align-items: center; gap: 8px;">
332
+ <input type="checkbox" name="terms" required style="margin: 0;" />
333
+ <span style="font-size: 14px; color: var(--md-sys-color-on-surface);">
334
+ I agree to the <a href="#" style="color: var(--md-sys-color-primary);">Terms and Conditions</a> *
335
+ </span>
336
+ </label>
337
+ </div>
338
+
339
+ <ui-button color="text" slot="button" value="dismiss">Cancel</ui-button>
340
+ <ui-button color="filled" slot="button" value="confirm" type="submit">Register</ui-button>
341
+ </ui-dialog>
342
+ </form>
343
+ </section>
344
+ `
345
+ }
346
+
347
+ protected handleFormSubmit(e: CustomEvent): void {
348
+ e.preventDefault()
349
+
350
+ // Get form data from the event
351
+ const form = e.target as HTMLFormElement
352
+ const formData = new FormData(form)
353
+
354
+ // Convert to regular object for easier handling
355
+ const data: Record<string, string | boolean> = {}
356
+ for (const [key, value] of formData.entries()) {
357
+ if (key === 'newsletter' || key === 'terms') {
358
+ data[key] = value === 'on'
359
+ } else {
360
+ data[key] = value as string
361
+ }
362
+ }
363
+
364
+ console.log('Form submitted with data:', data)
365
+
366
+ // Simulate form validation
367
+ if (!data.fullName || !data.email) {
368
+ console.error('Required fields are missing')
369
+ return
370
+ }
371
+
372
+ if (!data.terms) {
373
+ console.error('Terms and conditions must be accepted')
374
+ return
375
+ }
376
+
377
+ // If validation passes, the dialog will close automatically
378
+ console.log('Registration successful!', data)
379
+ }
380
+
247
381
  renderImperativeDialog(): TemplateResult {
248
382
  return html`
249
383
  <section class="demo-section">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@api-client/ui",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Internal UI component library for the API Client ecosystem.",
5
5
  "license": "UNLICENSED",
6
6
  "main": "build/src/index.js",
@@ -182,7 +182,7 @@
182
182
  "dompurify": "^3.2.5",
183
183
  "idb-keyval": "^6.1.0",
184
184
  "lit": "^3.2.1",
185
- "marked": "^15.0.7",
185
+ "marked": "^16.0.0",
186
186
  "monaco-editor": "^0.52.2",
187
187
  "nanoid": "^5.1.5",
188
188
  "prismjs": "^1.28.0",
@@ -247,7 +247,8 @@ export default class MarkedHighlight extends LitElement {
247
247
  pedantic: this.pedantic,
248
248
  gfm: true,
249
249
  }
250
- let out = marked(data, opts) as string
250
+ // eslint-disable-next-line no-misleading-character-class
251
+ let out = marked(data.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, ''), opts) as string
251
252
  if (this.sanitize) {
252
253
  if (this.sanitizer) {
253
254
  out = this.sanitizer(out)
@@ -240,7 +240,7 @@ export default class BaseButton extends UiElement {
240
240
  }
241
241
 
242
242
  override handleClick(e: MouseEvent): void {
243
- super.handleClick(e)
243
+ // Do not call super.handleClick() here, as it would call `endPress` again.
244
244
  if (this.disabled) {
245
245
  e.preventDefault()
246
246
  e.stopPropagation()
@@ -8,6 +8,7 @@ import type { TypedEvents } from '../../../core/types.js'
8
8
  import { ifDefined } from 'lit/directives/if-defined.js'
9
9
  import { SyntheticSubmitEvent } from '../../../events/SyntheticSubmitEvent.js'
10
10
  import '../../button/ui-button.js'
11
+ import { bound } from '../../../decorators/bound.js'
11
12
 
12
13
  export interface UiDialogClosingReason {
13
14
  /**
@@ -137,6 +138,8 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
137
138
  * - When the submit event is not cancelled, then:
138
139
  * - The default confirm action is invoked.
139
140
  * - The dialog is closed.
141
+ *
142
+ * @deprecated Wrap the content in a `<form>` element instead.
140
143
  */
141
144
  @property({ type: Boolean }) accessor useForm: boolean | undefined
142
145
 
@@ -147,6 +150,15 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
147
150
  * @attribute
148
151
  */
149
152
  @property({ type: String }) accessor confirmValue: string | undefined
153
+ /**
154
+ * When the dialog is wrapped in a form, set this to `true` to close the dialog
155
+ * when the form is submitted.
156
+ *
157
+ * Note that the dialog doesn't perform any validation of the form. It only closes
158
+ * when the form is submitted, regardless of the application logic. The `submit` event
159
+ * is dispatched by the dialog when the form is valid.
160
+ */
161
+ @property({ type: Boolean }) accessor submitClose: boolean | undefined
150
162
 
151
163
  /**
152
164
  * A reference to the underlying dialog element.
@@ -173,14 +185,32 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
173
185
 
174
186
  #formId?: string
175
187
 
188
+ /**
189
+ * @deprecated Use `useForm` instead.
190
+ */
176
191
  get formId(): string | undefined {
177
192
  return this.#formId
178
193
  }
179
194
 
195
+ /**
196
+ * @deprecated This will be removed in the future.
197
+ */
180
198
  @state() accessor #inject: TemplateResult | RenderFunction | undefined
181
199
 
200
+ /**
201
+ * @deprecated This will be removed in the future.
202
+ */
182
203
  #pendingStyles?: CSSResultOrNative[]
183
204
 
205
+ /**
206
+ * A reference to the parent form element.
207
+ * When a form is found, the dialog will hook into the form's submit event
208
+ * and close the dialog when the form is submitted.
209
+ * Since the `submit` event is dispatched when the form is valid,
210
+ * we can use this to close the dialog and not worry about the form validation.
211
+ */
212
+ #form: HTMLFormElement | null = null
213
+
184
214
  constructor() {
185
215
  super()
186
216
 
@@ -192,6 +222,7 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
192
222
  * Allows to inject a template into the internals of the element.
193
223
  * This is helpful when working with imperative dialogs.
194
224
  * @param content The content to inject into the body.
225
+ * @deprecated This will be removed in the future. To use forms, wrap the content in a `<form>` element.
195
226
  */
196
227
  inject(content?: TemplateResult | RenderFunction, styles?: CSSResultOrNative[]): void {
197
228
  this.#inject = content
@@ -210,6 +241,22 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
210
241
  }
211
242
  }
212
243
 
244
+ override connectedCallback(): void {
245
+ super.connectedCallback()
246
+ this.#form = this.closest('form')
247
+ if (this.#form) {
248
+ this.#form.addEventListener('submit', this.handleFormSubmit)
249
+ }
250
+ }
251
+
252
+ override disconnectedCallback(): void {
253
+ super.disconnectedCallback()
254
+ if (this.#form) {
255
+ this.#form.removeEventListener('submit', this.handleFormSubmit)
256
+ this.#form = null
257
+ }
258
+ }
259
+
213
260
  protected override firstUpdated(): void {
214
261
  const styles = this.#pendingStyles
215
262
  if (styles) {
@@ -226,6 +273,13 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
226
273
  }
227
274
  }
228
275
 
276
+ @bound
277
+ protected handleFormSubmit(): void {
278
+ if (this.submitClose) {
279
+ this.handleInteraction('confirm')
280
+ }
281
+ }
282
+
229
283
  override handleClick(e: MouseEvent): void {
230
284
  super.handleClick(e)
231
285
  const path = e.composedPath()
@@ -238,7 +292,6 @@ export default class UiDialog extends UiElement implements TypedEvents<DialogEve
238
292
  // Adds support for forms.
239
293
  // When a form's submit button is clicked we yield the flow control to the form.
240
294
  // This way the form can handle the submit event.
241
- e.preventDefault()
242
295
  return
243
296
  }
244
297
  const { value = '' } = button
@@ -143,7 +143,7 @@ export default abstract class Input extends UiElement {
143
143
  * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#max
144
144
  * @attribute
145
145
  */
146
- @property({ type: String }) accessor max = ''
146
+ @property({ type: String }) accessor max: number | string = ''
147
147
 
148
148
  /**
149
149
  * The maximum number of characters a user can enter into the text field. Set
@@ -160,7 +160,7 @@ export default abstract class Input extends UiElement {
160
160
  * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#min
161
161
  * @attribute
162
162
  */
163
- @property({ type: String }) accessor min = ''
163
+ @property({ type: String }) accessor min: number | string = ''
164
164
 
165
165
  /**
166
166
  * The minimum number of characters a user can enter into the text field. Set
@@ -169,7 +169,7 @@ export default abstract class Input extends UiElement {
169
169
  * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#minlength
170
170
  * @attribute
171
171
  */
172
- @property({ type: Number }) accessor minLength = -1
172
+ @property({ type: Number }) accessor minLength: number | undefined
173
173
 
174
174
  /**
175
175
  * A regular expression that the text field's value must match to pass
@@ -304,7 +304,7 @@ export default abstract class Input extends UiElement {
304
304
  * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#step
305
305
  * @attribute
306
306
  */
307
- @property({ type: String }) accessor step = ''
307
+ @property({ type: Number }) accessor step = 1
308
308
 
309
309
  /**
310
310
  * Returns the native validation error message that would be displayed upon
@@ -35,7 +35,6 @@ export default class TextAreaElement extends Input {
35
35
  protected override renderInput(): TemplateResult {
36
36
  const placeholderValue = this.placeholder || undefined
37
37
  const maxLengthValue = this.maxLength > -1 ? this.maxLength : undefined
38
- const minLengthValue = this.minLength > -1 ? this.minLength : undefined
39
38
  const roleValue = this.dataRole || undefined
40
39
  const { disabled } = this
41
40
 
@@ -59,7 +58,7 @@ export default class TextAreaElement extends Input {
59
58
  name="${ifDefined(this.name)}"
60
59
  .value=${live(this.getInputValue())}
61
60
  maxlength=${ifDefined(maxLengthValue)}
62
- minlength=${ifDefined(minLengthValue)}
61
+ minlength=${ifDefined(this.minLength)}
63
62
  placeholder=${ifDefined(placeholderValue)}
64
63
  role=${ifDefined(roleValue as 'textbox')}
65
64
  ?disabled=${disabled}
@@ -10,7 +10,6 @@ export default class TextField extends Input {
10
10
  const maxValue = this.max || undefined
11
11
  const maxLengthValue = this.maxLength > -1 ? this.maxLength : undefined
12
12
  const minValue = this.min || undefined
13
- const minLengthValue = this.minLength > -1 ? this.minLength : undefined
14
13
  const patternValue = this.pattern || undefined
15
14
  const roleValue = this.dataRole || undefined
16
15
  const stepValue = this.step || undefined
@@ -36,17 +35,17 @@ export default class TextField extends Input {
36
35
  type=${this.effectiveType}
37
36
  name="${ifDefined(this.name)}"
38
37
  .value=${live(this.getInputValue())}
39
- max=${ifDefined(maxValue as unknown as number)}
38
+ max=${ifDefined(maxValue)}
40
39
  maxlength=${ifDefined(maxLengthValue)}
41
- min=${ifDefined(minValue as unknown as number)}
42
- minlength=${ifDefined(minLengthValue)}
40
+ min=${ifDefined(minValue)}
41
+ minlength=${ifDefined(this.minLength)}
43
42
  pattern=${ifDefined(patternValue)}
44
43
  placeholder=${ifDefined(placeholderValue)}
45
44
  role=${ifDefined(roleValue as 'textbox')}
46
45
  ?disabled=${disabled}
47
46
  ?readonly=${this.readOnly}
48
47
  ?required=${this.required}
49
- step=${ifDefined(stepValue as unknown as number)}
48
+ step=${ifDefined(stepValue)}
50
49
  autocomplete="${ifDefined(this.autocomplete)}"
51
50
  autocapitalize="${ifDefined(this.autocapitalize)}"
52
51
  inputmode="${ifDefined(this.inputMode)}"