@idds/js 1.6.0 → 1.6.3

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.
@@ -1299,8 +1299,7 @@ var InaUI = (() => {
1299
1299
  input.classList.add(`${PREFIX}-time-picker__input--with-suffix`);
1300
1300
  input.placeholder = this.container.getAttribute("placeholder") || "Select time";
1301
1301
  if (this.options.disabled) input.disabled = true;
1302
- if (this.options.readonly) input.readOnly = true;
1303
- else input.readOnly = true;
1302
+ input.readOnly = !!this.options.readonly;
1304
1303
  wrapper.appendChild(input);
1305
1304
  if (this.options.allowClear) {
1306
1305
  clearBtn = document.createElement("button");
@@ -1316,8 +1315,22 @@ var InaUI = (() => {
1316
1315
  wrapper.setAttribute("role", "combobox");
1317
1316
  wrapper.setAttribute("aria-haspopup", "listbox");
1318
1317
  wrapper.setAttribute("aria-expanded", "false");
1319
- if (input && !input.id) input.id = this.inputId;
1320
- if (clearBtn && !clearBtn.hasAttribute("aria-label")) {
1318
+ if (input) {
1319
+ if (!input.id) input.id = this.inputId;
1320
+ input.readOnly = !!this.options.readonly;
1321
+ }
1322
+ if (this.options.allowClear && !clearBtn) {
1323
+ clearBtn = document.createElement("button");
1324
+ clearBtn.type = "button";
1325
+ clearBtn.className = `${PREFIX}-time-picker__clear-button`;
1326
+ clearBtn.setAttribute("aria-label", "Hapus waktu");
1327
+ clearBtn.innerHTML = this.createIcon("x");
1328
+ clearBtn.style.display = "none";
1329
+ wrapper.appendChild(clearBtn);
1330
+ if (input) {
1331
+ input.classList.add(`${PREFIX}-time-picker__input--with-suffix`);
1332
+ }
1333
+ } else if (clearBtn && !clearBtn.hasAttribute("aria-label")) {
1321
1334
  clearBtn.setAttribute("aria-label", "Hapus waktu");
1322
1335
  }
1323
1336
  }
@@ -1615,13 +1628,76 @@ var InaUI = (() => {
1615
1628
  bindEvents() {
1616
1629
  this.elements.wrapper.addEventListener("click", (e) => {
1617
1630
  e.stopPropagation();
1618
- this.toggle();
1631
+ const target = e.target;
1632
+ if (target === this.elements.wrapper || target.classList.contains(`${PREFIX}-time-picker__suffix-icon`) || target.classList.contains(`${PREFIX}-time-picker__prefix-icon`)) {
1633
+ this.toggle();
1634
+ }
1635
+ });
1636
+ this.elements.input.addEventListener("input", (e) => {
1637
+ let val = e.target.value;
1638
+ const allowedChars = this.options.use12Hours ? /[0-9: apm]/gi : /[0-9:]/g;
1639
+ val = (val.match(allowedChars) || []).join("");
1640
+ let rawDigits = val.replace(/[^0-9]/g, "");
1641
+ let formatted = "";
1642
+ if (rawDigits.length > 0) {
1643
+ formatted += rawDigits.slice(0, 2);
1644
+ if (rawDigits.length > 2) {
1645
+ formatted += ":" + rawDigits.slice(2, 4);
1646
+ if (this.options.showSecond && rawDigits.length > 4) {
1647
+ formatted += ":" + rawDigits.slice(4, 6);
1648
+ }
1649
+ }
1650
+ }
1651
+ if (this.options.use12Hours) {
1652
+ const periodMatch = val.match(/(am|pm)/i);
1653
+ if (periodMatch) {
1654
+ formatted += " " + periodMatch[0].toUpperCase();
1655
+ } else if (val.includes(" ")) {
1656
+ formatted += " ";
1657
+ }
1658
+ }
1659
+ let maxLen = 5;
1660
+ if (this.options.showSecond) maxLen = 8;
1661
+ if (this.options.use12Hours) maxLen += 3;
1662
+ formatted = formatted.slice(0, maxLen);
1663
+ e.target.value = formatted;
1664
+ this.state.internalValue = formatted;
1665
+ const timeRegex = this.options.use12Hours ? /^(\d{1,2}):(\d{2})(?::(\d{2}))?\s?(AM|PM|am|pm)?$/ : /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/;
1666
+ if (timeRegex.test(formatted)) {
1667
+ this.state.currentTime = this.parseTime(formatted);
1668
+ if (this.state.isOpen) this.buildPanel();
1669
+ }
1670
+ if (this.elements.clearBtn && this.options.allowClear) {
1671
+ this.elements.clearBtn.style.display = formatted && !this.options.disabled ? "flex" : "none";
1672
+ }
1673
+ if (typeof this.options.onChange === "function") {
1674
+ this.options.onChange(formatted);
1675
+ }
1676
+ });
1677
+ this.elements.input.addEventListener("click", (e) => {
1678
+ e.stopPropagation();
1679
+ this.open();
1680
+ });
1681
+ this.elements.input.addEventListener("focus", () => {
1682
+ this.open();
1683
+ });
1684
+ this.elements.input.addEventListener("keydown", (e) => {
1685
+ if (e.key === "Enter") {
1686
+ e.preventDefault();
1687
+ if (this.state.internalValue) {
1688
+ this.close();
1689
+ } else {
1690
+ this.toggle();
1691
+ }
1692
+ } else if (e.key === "Escape") {
1693
+ if (this.state.isOpen) this.close();
1694
+ }
1619
1695
  });
1620
1696
  if (this.elements.clearBtn) {
1621
1697
  this.elements.clearBtn.addEventListener("click", (e) => {
1622
1698
  e.stopPropagation();
1623
- this.container.dataset.value = "";
1624
1699
  this.state.internalValue = "";
1700
+ this.elements.input.value = "";
1625
1701
  this.elements.clearBtn.style.display = "none";
1626
1702
  this.elements.input.dispatchEvent(
1627
1703
  new Event("change", { bubbles: true })
@@ -1630,6 +1706,12 @@ var InaUI = (() => {
1630
1706
  this.options.onChange("");
1631
1707
  }
1632
1708
  });
1709
+ this.elements.clearBtn.addEventListener("keydown", (e) => {
1710
+ if (e.key === "Enter" || e.key === " ") {
1711
+ e.preventDefault();
1712
+ this.elements.clearBtn.click();
1713
+ }
1714
+ });
1633
1715
  }
1634
1716
  document.addEventListener("click", (e) => {
1635
1717
  if (!this.container.contains(e.target)) this.close();
@@ -3726,6 +3808,12 @@ var InaUI = (() => {
3726
3808
  this.elements.clearBtn.addEventListener("click", () => {
3727
3809
  this.clear();
3728
3810
  });
3811
+ this.elements.clearBtn.addEventListener("keydown", (e) => {
3812
+ if (e.key === "Enter" || e.key === " ") {
3813
+ e.preventDefault();
3814
+ this.clear();
3815
+ }
3816
+ });
3729
3817
  }
3730
3818
  document.addEventListener("click", (e) => {
3731
3819
  if (!this.elements.wrapper.contains(e.target)) {
package/dist/index.js CHANGED
@@ -1318,8 +1318,7 @@ var TimePicker = class {
1318
1318
  input.classList.add(`${PREFIX}-time-picker__input--with-suffix`);
1319
1319
  input.placeholder = this.container.getAttribute("placeholder") || "Select time";
1320
1320
  if (this.options.disabled) input.disabled = true;
1321
- if (this.options.readonly) input.readOnly = true;
1322
- else input.readOnly = true;
1321
+ input.readOnly = !!this.options.readonly;
1323
1322
  wrapper.appendChild(input);
1324
1323
  if (this.options.allowClear) {
1325
1324
  clearBtn = document.createElement("button");
@@ -1335,8 +1334,22 @@ var TimePicker = class {
1335
1334
  wrapper.setAttribute("role", "combobox");
1336
1335
  wrapper.setAttribute("aria-haspopup", "listbox");
1337
1336
  wrapper.setAttribute("aria-expanded", "false");
1338
- if (input && !input.id) input.id = this.inputId;
1339
- if (clearBtn && !clearBtn.hasAttribute("aria-label")) {
1337
+ if (input) {
1338
+ if (!input.id) input.id = this.inputId;
1339
+ input.readOnly = !!this.options.readonly;
1340
+ }
1341
+ if (this.options.allowClear && !clearBtn) {
1342
+ clearBtn = document.createElement("button");
1343
+ clearBtn.type = "button";
1344
+ clearBtn.className = `${PREFIX}-time-picker__clear-button`;
1345
+ clearBtn.setAttribute("aria-label", "Hapus waktu");
1346
+ clearBtn.innerHTML = this.createIcon("x");
1347
+ clearBtn.style.display = "none";
1348
+ wrapper.appendChild(clearBtn);
1349
+ if (input) {
1350
+ input.classList.add(`${PREFIX}-time-picker__input--with-suffix`);
1351
+ }
1352
+ } else if (clearBtn && !clearBtn.hasAttribute("aria-label")) {
1340
1353
  clearBtn.setAttribute("aria-label", "Hapus waktu");
1341
1354
  }
1342
1355
  }
@@ -1634,13 +1647,76 @@ var TimePicker = class {
1634
1647
  bindEvents() {
1635
1648
  this.elements.wrapper.addEventListener("click", (e) => {
1636
1649
  e.stopPropagation();
1637
- this.toggle();
1650
+ const target = e.target;
1651
+ if (target === this.elements.wrapper || target.classList.contains(`${PREFIX}-time-picker__suffix-icon`) || target.classList.contains(`${PREFIX}-time-picker__prefix-icon`)) {
1652
+ this.toggle();
1653
+ }
1654
+ });
1655
+ this.elements.input.addEventListener("input", (e) => {
1656
+ let val = e.target.value;
1657
+ const allowedChars = this.options.use12Hours ? /[0-9: apm]/gi : /[0-9:]/g;
1658
+ val = (val.match(allowedChars) || []).join("");
1659
+ let rawDigits = val.replace(/[^0-9]/g, "");
1660
+ let formatted = "";
1661
+ if (rawDigits.length > 0) {
1662
+ formatted += rawDigits.slice(0, 2);
1663
+ if (rawDigits.length > 2) {
1664
+ formatted += ":" + rawDigits.slice(2, 4);
1665
+ if (this.options.showSecond && rawDigits.length > 4) {
1666
+ formatted += ":" + rawDigits.slice(4, 6);
1667
+ }
1668
+ }
1669
+ }
1670
+ if (this.options.use12Hours) {
1671
+ const periodMatch = val.match(/(am|pm)/i);
1672
+ if (periodMatch) {
1673
+ formatted += " " + periodMatch[0].toUpperCase();
1674
+ } else if (val.includes(" ")) {
1675
+ formatted += " ";
1676
+ }
1677
+ }
1678
+ let maxLen = 5;
1679
+ if (this.options.showSecond) maxLen = 8;
1680
+ if (this.options.use12Hours) maxLen += 3;
1681
+ formatted = formatted.slice(0, maxLen);
1682
+ e.target.value = formatted;
1683
+ this.state.internalValue = formatted;
1684
+ const timeRegex = this.options.use12Hours ? /^(\d{1,2}):(\d{2})(?::(\d{2}))?\s?(AM|PM|am|pm)?$/ : /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/;
1685
+ if (timeRegex.test(formatted)) {
1686
+ this.state.currentTime = this.parseTime(formatted);
1687
+ if (this.state.isOpen) this.buildPanel();
1688
+ }
1689
+ if (this.elements.clearBtn && this.options.allowClear) {
1690
+ this.elements.clearBtn.style.display = formatted && !this.options.disabled ? "flex" : "none";
1691
+ }
1692
+ if (typeof this.options.onChange === "function") {
1693
+ this.options.onChange(formatted);
1694
+ }
1695
+ });
1696
+ this.elements.input.addEventListener("click", (e) => {
1697
+ e.stopPropagation();
1698
+ this.open();
1699
+ });
1700
+ this.elements.input.addEventListener("focus", () => {
1701
+ this.open();
1702
+ });
1703
+ this.elements.input.addEventListener("keydown", (e) => {
1704
+ if (e.key === "Enter") {
1705
+ e.preventDefault();
1706
+ if (this.state.internalValue) {
1707
+ this.close();
1708
+ } else {
1709
+ this.toggle();
1710
+ }
1711
+ } else if (e.key === "Escape") {
1712
+ if (this.state.isOpen) this.close();
1713
+ }
1638
1714
  });
1639
1715
  if (this.elements.clearBtn) {
1640
1716
  this.elements.clearBtn.addEventListener("click", (e) => {
1641
1717
  e.stopPropagation();
1642
- this.container.dataset.value = "";
1643
1718
  this.state.internalValue = "";
1719
+ this.elements.input.value = "";
1644
1720
  this.elements.clearBtn.style.display = "none";
1645
1721
  this.elements.input.dispatchEvent(
1646
1722
  new Event("change", { bubbles: true })
@@ -1649,6 +1725,12 @@ var TimePicker = class {
1649
1725
  this.options.onChange("");
1650
1726
  }
1651
1727
  });
1728
+ this.elements.clearBtn.addEventListener("keydown", (e) => {
1729
+ if (e.key === "Enter" || e.key === " ") {
1730
+ e.preventDefault();
1731
+ this.elements.clearBtn.click();
1732
+ }
1733
+ });
1652
1734
  }
1653
1735
  document.addEventListener("click", (e) => {
1654
1736
  if (!this.container.contains(e.target)) this.close();
@@ -3837,6 +3919,12 @@ var PhoneInput = class {
3837
3919
  this.elements.clearBtn.addEventListener("click", () => {
3838
3920
  this.clear();
3839
3921
  });
3922
+ this.elements.clearBtn.addEventListener("keydown", (e) => {
3923
+ if (e.key === "Enter" || e.key === " ") {
3924
+ e.preventDefault();
3925
+ this.clear();
3926
+ }
3927
+ });
3840
3928
  }
3841
3929
  document.addEventListener("click", (e) => {
3842
3930
  if (!this.elements.wrapper.contains(e.target)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idds/js",
3
- "version": "1.6.0",
3
+ "version": "1.6.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },