@rogieking/figui3 1.0.4 → 1.0.6

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 (2) hide show
  1. package/fig.js +62 -44
  2. package/package.json +1 -1
package/fig.js CHANGED
@@ -398,43 +398,44 @@ window.customElements.define('fig-popover-2', FigPopover2);
398
398
 
399
399
 
400
400
  /* Tabs */
401
+ class FigTab extends HTMLElement {
402
+ constructor() {
403
+ super()
404
+ }
405
+ connectedCallback() {
406
+ this.setAttribute("label", this.innerText)
407
+ this.addEventListener("click", this.handleClick.bind(this))
408
+ }
409
+ disconnectedCallback() {
410
+ this.removeEventListener("click", this.handleClick)
411
+ }
412
+ handleClick() {
413
+ this.setAttribute("selected", "true")
414
+ }
415
+ }
416
+ window.customElements.define('fig-tab', FigTab);
417
+
401
418
  class FigTabs extends HTMLElement {
402
419
  constructor() {
403
420
  super()
404
421
  }
405
422
  connectedCallback() {
406
- const tabs = this.querySelectorAll('fig-tab')
407
- const name = this.getAttribute("name") || ("tabs-" + this.uniqueId())
408
- for (const tab of tabs) {
409
- let input = document.createElement('input')
410
- input.type = "radio"
411
- input.name = name
412
- input.checked = tab.hasAttribute("selected")
413
- input.value = tab.getAttribute("value") || this.slugify(tab.innerText)
414
- tab.setAttribute("label", tab.innerText)
415
- tab.append(input)
416
- input.addEventListener("input", this.handleInput.bind(this))
417
- }
418
- }
419
- uniqueId() {
420
- return Date.now().toString(36) + Math.random().toString(36).substring(2)
421
- }
422
- slugify(text) {
423
- return text
424
- .toLowerCase()
425
- .trim()
426
- .replace(/[^\w\s-]/g, '')
427
- .replace(/[\s_-]+/g, '-')
428
- .replace(/^-+|-+$/g, '');
423
+ this.name = this.getAttribute("name") || "tabs"
424
+ this.addEventListener("click", this.handleClick.bind(this))
429
425
  }
430
- handleInput() {
431
- const radios = this.querySelectorAll("[type=radio]")
432
- for (const radio of radios) {
433
- if (radio.checked) {
434
- this.value = radio.value
435
- radio.parentNode.setAttribute("selected", "")
436
- } else {
437
- radio.parentNode.removeAttribute("selected")
426
+ disconnectedCallback() {
427
+ this.removeEventListener("click", this.handleClick)
428
+ }
429
+ handleClick(event) {
430
+ const target = event.target;
431
+ if (target.nodeName.toLowerCase() === "fig-tab") {
432
+ const tabs = this.querySelectorAll("fig-tab")
433
+ for (const tab of tabs) {
434
+ if (tab === target) {
435
+ this.selectedTab = tab
436
+ } else {
437
+ tab.removeAttribute("selected")
438
+ }
438
439
  }
439
440
  }
440
441
  }
@@ -442,26 +443,43 @@ class FigTabs extends HTMLElement {
442
443
  window.customElements.define('fig-tabs', FigTabs);
443
444
 
444
445
  /* Segmented Control */
446
+ class FigSegment extends HTMLElement {
447
+ constructor() {
448
+ super()
449
+ }
450
+ connectedCallback() {
451
+ this.addEventListener("click", this.handleClick.bind(this))
452
+ }
453
+ disconnectedCallback() {
454
+ this.removeEventListener("click", this.handleClick)
455
+ }
456
+ handleClick() {
457
+ this.setAttribute("selected", "true")
458
+ }
459
+ }
460
+ window.customElements.define('fig-segment', FigSegment);
461
+
445
462
  class FigSegmentedControl extends HTMLElement {
446
463
  constructor() {
447
464
  super()
448
465
  }
449
466
  connectedCallback() {
450
- const segments = this.querySelectorAll('fig-segment')
451
- const name = this.getAttribute("name") || "segmented-control"
452
- for (const segment of segments) {
453
- let input = document.createElement('input')
454
- input.type = "radio"
455
- input.name = name
456
- input.checked = segment.hasAttribute("selected")
457
- input.value = segment.getAttribute("value")
458
- segment.append(input)
459
- input.addEventListener("input", this.handleInput.bind(this))
467
+ this.name = this.getAttribute("name") || "segmented-control"
468
+ this.addEventListener("click", this.handleClick.bind(this))
469
+ }
470
+ handleClick(event) {
471
+ const target = event.target;
472
+ if (target.nodeName.toLowerCase() === "fig-segment") {
473
+ const segments = this.querySelectorAll("fig-segment")
474
+ for (const segment of segments) {
475
+ if (segment === target) {
476
+ this.selectedSegment = segment
477
+ } else {
478
+ segment.removeAttribute("selected")
479
+ }
480
+ }
460
481
  }
461
482
  }
462
- handleInput() {
463
- this.value = this.querySelector(":checked").value
464
- }
465
483
  }
466
484
  window.customElements.define('fig-segmented-control', FigSegmentedControl);
467
485
 
package/package.json CHANGED
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "name": "@rogieking/figui3",
3
- "version": "1.0.4"
3
+ "version": "1.0.6"
4
4
  }