@starasia/dropdown 1.0.6 → 1.0.7

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.
@@ -13,6 +13,7 @@ interface IProps {
13
13
  handleChangeValue: (label: string, values: string) => void;
14
14
  multiSelect: boolean | undefined;
15
15
  searchValue?: string;
16
+ highlightedIndex: number;
16
17
  }
17
18
  declare const DropdownList: React.ForwardRefExoticComponent<IProps & React.RefAttributes<HTMLDivElement>>;
18
19
  export default DropdownList;
@@ -1,6 +1,6 @@
1
- import { jsx as n, jsxs as p, Fragment as q } from "react/jsx-runtime";
2
- import F, { useState as y, useRef as k, useEffect as b, forwardRef as M } from "react";
3
- const P = `@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap");
1
+ import { jsx as a, jsxs as p, Fragment as H } from "react/jsx-runtime";
2
+ import O, { useState as R, useRef as z, useEffect as b, forwardRef as K } from "react";
3
+ const U = `@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap");
4
4
  * {
5
5
  margin: 0;
6
6
  padding: 0;
@@ -266,6 +266,11 @@ const P = `@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@30
266
266
  /* background: var(--starasia-ui-dropdown-background-color-secondary); */
267
267
  /* box-shadow: 0px 0px 0px -1px #f0f0f0; */
268
268
  }
269
+ .starasia-input-dropdwon-item.highlight {
270
+ background-color: rgba(var(--starasia-ui-raw-color-brand-secondary, 144, 176, 218), 0.35);
271
+ outline: 2px solid rgba(var(--starasia-ui-raw-color-brand-secondary, 144, 176, 218), 0.4);
272
+ outline-offset: -2px;
273
+ }
269
274
 
270
275
  .starasia-input-dropdwon-item-disable {
271
276
  color: var(--starasia-ui-dropdown-color-disable) !important;
@@ -397,23 +402,23 @@ const P = `@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@30
397
402
  width: 100%;
398
403
  }
399
404
  `;
400
- function V(a) {
401
- const [i, t] = y(a), c = k(null), o = (e) => {
402
- c.current && !c.current.contains(e.target) && t(!1);
405
+ function G(n) {
406
+ const [c, s] = R(n), d = z(null), i = (o) => {
407
+ d.current && !d.current.contains(o.target) && s(!1);
403
408
  };
404
- return b(() => (document.addEventListener("click", o, !0), () => {
405
- document.removeEventListener("click", o, !0);
406
- }), []), { ref: c, isComponentVisible: i, setIsComponentVisible: t };
409
+ return b(() => (document.addEventListener("click", i, !0), () => {
410
+ document.removeEventListener("click", i, !0);
411
+ }), []), { ref: d, isComponentVisible: c, setIsComponentVisible: s };
407
412
  }
408
- const J = ({ ...a }) => /* @__PURE__ */ n(
413
+ const Q = ({ ...n }) => /* @__PURE__ */ a(
409
414
  "svg",
410
415
  {
411
416
  xmlns: "http://www.w3.org/2000/svg",
412
- ...a,
417
+ ...n,
413
418
  viewBox: "0 0 24 24",
414
419
  fill: "none",
415
420
  color: "#939E99",
416
- children: /* @__PURE__ */ n(
421
+ children: /* @__PURE__ */ a(
417
422
  "path",
418
423
  {
419
424
  d: "M4 9L12 17L20 9",
@@ -424,15 +429,15 @@ const J = ({ ...a }) => /* @__PURE__ */ n(
424
429
  }
425
430
  )
426
431
  }
427
- ), G = ({ ...a }) => /* @__PURE__ */ p(
432
+ ), T = ({ ...n }) => /* @__PURE__ */ p(
428
433
  "svg",
429
434
  {
430
435
  xmlns: "http://www.w3.org/2000/svg",
431
- ...a,
436
+ ...n,
432
437
  viewBox: "0 0 20 20",
433
438
  fill: "none",
434
439
  children: [
435
- /* @__PURE__ */ n(
440
+ /* @__PURE__ */ a(
436
441
  "circle",
437
442
  {
438
443
  cx: "9.80547",
@@ -444,7 +449,7 @@ const J = ({ ...a }) => /* @__PURE__ */ n(
444
449
  strokeLinejoin: "round"
445
450
  }
446
451
  ),
447
- /* @__PURE__ */ n(
452
+ /* @__PURE__ */ a(
448
453
  "path",
449
454
  {
450
455
  d: "M15.0153 15.4043L17.9519 18.3333",
@@ -456,19 +461,19 @@ const J = ({ ...a }) => /* @__PURE__ */ n(
456
461
  )
457
462
  ]
458
463
  }
459
- ), K = ({ isChecked: a }) => /* @__PURE__ */ p("div", { className: "checkbox-wrapper-30", children: [
464
+ ), X = ({ isChecked: n }) => /* @__PURE__ */ p("div", { className: "checkbox-wrapper-30", children: [
460
465
  /* @__PURE__ */ p("span", { className: "checkbox", children: [
461
- /* @__PURE__ */ n(
466
+ /* @__PURE__ */ a(
462
467
  "input",
463
468
  {
464
469
  type: "checkbox",
465
470
  style: { pointerEvents: "none" },
466
- checked: a
471
+ checked: n
467
472
  }
468
473
  ),
469
- /* @__PURE__ */ n("svg", { children: /* @__PURE__ */ n("use", { xlinkHref: "#checkbox-30", className: "checkbox" }) })
474
+ /* @__PURE__ */ a("svg", { children: /* @__PURE__ */ a("use", { xlinkHref: "#checkbox-30", className: "checkbox" }) })
470
475
  ] }),
471
- /* @__PURE__ */ n("svg", { xmlns: "http://www.w3.org/2000/svg", style: { display: "none" }, children: /* @__PURE__ */ n("symbol", { id: "checkbox-30", viewBox: "0 0 22 22", children: /* @__PURE__ */ n(
476
+ /* @__PURE__ */ a("svg", { xmlns: "http://www.w3.org/2000/svg", style: { display: "none" }, children: /* @__PURE__ */ a("symbol", { id: "checkbox-30", viewBox: "0 0 22 22", children: /* @__PURE__ */ a(
472
477
  "path",
473
478
  {
474
479
  fill: "none",
@@ -476,58 +481,55 @@ const J = ({ ...a }) => /* @__PURE__ */ n(
476
481
  d: "M5.5,11.3L9,14.8L20.2,3.3l0,0c-0.5-1-1.5-1.8-2.7-1.8h-13c-1.7,0-3,1.3-3,3v13c0,1.7,1.3,3,3,3h13 c1.7,0,3-1.3,3-3v-13c0-0.4-0.1-0.8-0.3-1.2"
477
482
  }
478
483
  ) }) })
479
- ] }), Q = M(
484
+ ] }), Y = K(
480
485
  ({
481
- positionDropdown: a,
482
- dropdownLists: i,
483
- isComponentVisible: t,
484
- searchAble: c,
485
- onSearch: o,
486
- value: e,
487
- handleChangeValue: h,
486
+ positionDropdown: n,
487
+ dropdownLists: c,
488
+ isComponentVisible: s,
489
+ searchAble: d,
490
+ onSearch: i,
491
+ value: o,
492
+ handleChangeValue: g,
488
493
  multiSelect: l,
489
- searchValue: v
490
- }, I) => {
491
- const [C, R] = y(0), [$, z] = y(), w = k(null);
494
+ searchValue: $,
495
+ highlightedIndex: v
496
+ }, N) => {
497
+ const [I, W] = R(0), f = z(null), x = z([]);
492
498
  return b(() => {
493
- if (w.current) {
494
- const r = w.current.getBoundingClientRect();
495
- R(r.width);
499
+ if (f.current) {
500
+ const r = f.current.getBoundingClientRect();
501
+ W(r.width);
496
502
  }
497
- }, [w, t]), b(() => {
498
- i && v ? i.forEach((r) => {
499
- if (r.label.startsWith(v || "")) {
500
- z(r.value);
501
- return;
502
- }
503
- }) : z(void 0);
504
- }, [v, i]), /* @__PURE__ */ n(q, { children: t ? /* @__PURE__ */ p(
503
+ }, [f, s]), b(() => {
504
+ var r;
505
+ v >= 0 && x.current[v] && ((r = x.current[v]) == null || r.scrollIntoView({ block: "nearest" }));
506
+ }, [v]), /* @__PURE__ */ a(H, { children: s ? /* @__PURE__ */ p(
505
507
  "div",
506
508
  {
507
509
  className: "starasia-input-dropdown-lists-container",
508
510
  onClick: (r) => r.stopPropagation(),
509
- ref: I,
511
+ ref: N,
510
512
  children: [
511
- c ? /* @__PURE__ */ p(
513
+ d ? /* @__PURE__ */ p(
512
514
  "div",
513
515
  {
514
516
  className: "starasia-input-dropdown-container-dropdown-search",
515
517
  style: {
516
- order: a == "bottom" ? 0 : 2,
518
+ order: n == "bottom" ? 0 : 2,
517
519
  position: "sticky",
518
520
  top: 0,
519
521
  bottom: 0,
520
522
  zIndex: 1
521
523
  },
522
524
  children: [
523
- /* @__PURE__ */ n("div", { style: { display: "flex" }, children: /* @__PURE__ */ n(G, { strokeWidth: 2, width: 20 }) }),
524
- /* @__PURE__ */ n(
525
+ /* @__PURE__ */ a("div", { style: { display: "flex" }, children: /* @__PURE__ */ a(T, { strokeWidth: 2, width: 20 }) }),
526
+ /* @__PURE__ */ a(
525
527
  "input",
526
528
  {
527
529
  onChange: (r) => {
528
- o && o(r.target.value);
530
+ i && i(r.target.value);
529
531
  },
530
- value: v,
532
+ value: $,
531
533
  autoFocus: !0,
532
534
  style: { width: "100%" }
533
535
  }
@@ -535,44 +537,47 @@ const J = ({ ...a }) => /* @__PURE__ */ n(
535
537
  ]
536
538
  }
537
539
  ) : null,
538
- /* @__PURE__ */ n(
540
+ /* @__PURE__ */ a(
539
541
  "div",
540
542
  {
541
543
  className: `starasia-input-dropdwon-item-container ${l ? "multi" : ""}`,
542
- ref: w,
543
- children: i.map((r, x) => {
544
- const m = !!(e != null && e.find((E) => E.value === r.value));
544
+ ref: f,
545
+ children: c.map((r, w) => {
546
+ const y = !!(o != null && o.find((k) => k.value === r.value));
545
547
  return /* @__PURE__ */ p(
546
548
  "div",
547
549
  {
548
- className: `starasia-input-dropdwon-item ${m ? "active" : " "} ${r.disable ? "starasia-input-dropdwon-item-disable" : null}`,
549
- onClick: () => h(r.label, r.value),
550
+ className: `starasia-input-dropdwon-item ${y ? "active" : ""} ${w === v ? "highlight" : ""} ${r.disable ? "starasia-input-dropdwon-item-disable" : ""}`,
551
+ ref: (k) => {
552
+ x.current[w] = k;
553
+ },
554
+ onClick: () => g(r.label, r.value),
550
555
  children: [
551
- l ? /* @__PURE__ */ n("div", { style: { marginRight: "8px" }, children: /* @__PURE__ */ n(K, { isChecked: m }) }) : null,
552
- r.icon ? /* @__PURE__ */ n("div", { children: F.cloneElement(r.icon, {
556
+ l ? /* @__PURE__ */ a("div", { style: { marginRight: "8px" }, children: /* @__PURE__ */ a(X, { isChecked: y }) }) : null,
557
+ r.icon ? /* @__PURE__ */ a("div", { children: O.cloneElement(r.icon, {
553
558
  width: 16
554
559
  }) }) : null,
555
560
  /* @__PURE__ */ p("div", { children: [
556
- /* @__PURE__ */ n(
561
+ /* @__PURE__ */ a(
557
562
  "p",
558
563
  {
559
- className: `starasia-input-dropdwon-item-text ${m ? "active" : null} ${r.disable ? "starasia-input-dropdwon-item-disable" : null}`,
564
+ className: `starasia-input-dropdwon-item-text ${y ? "active" : null} ${r.disable ? "starasia-input-dropdwon-item-disable" : null}`,
560
565
  style: {
561
566
  textWrap: "nowrap",
562
- width: `${C - 12}px`,
567
+ width: `${I - 12}px`,
563
568
  overflow: "hidden",
564
569
  textOverflow: "ellipsis"
565
570
  },
566
571
  children: r.label
567
572
  }
568
573
  ),
569
- r.description ? /* @__PURE__ */ n(
574
+ r.description ? /* @__PURE__ */ a(
570
575
  "p",
571
576
  {
572
- className: `starasia-input-dropdwon-item-text-description ${m ? "active" : null} ${r.disable ? "starasia-input-dropdwon-item-disable" : null}`,
577
+ className: `starasia-input-dropdwon-item-text-description ${y ? "active" : null} ${r.disable ? "starasia-input-dropdwon-item-disable" : null}`,
573
578
  style: {
574
579
  textWrap: "nowrap",
575
- width: `${C - 12}px`,
580
+ width: `${I - 12}px`,
576
581
  overflow: "hidden",
577
582
  textOverflow: "ellipsis"
578
583
  },
@@ -582,7 +587,7 @@ const J = ({ ...a }) => /* @__PURE__ */ n(
582
587
  ] })
583
588
  ]
584
589
  },
585
- x
590
+ w
586
591
  );
587
592
  })
588
593
  }
@@ -591,149 +596,202 @@ const J = ({ ...a }) => /* @__PURE__ */ n(
591
596
  }
592
597
  ) : null });
593
598
  }
594
- ), T = ({
595
- children: a,
596
- handleChangePosition: i
599
+ ), Z = ({
600
+ children: n,
601
+ handleChangePosition: c
597
602
  }) => {
598
- const t = k(null);
603
+ const s = z(null);
599
604
  b(() => {
600
- var h;
601
- const o = () => {
602
- if (t.current) {
603
- let l = t.current.parentElement;
604
- for (; l && !c(l); )
605
+ var g;
606
+ const i = () => {
607
+ if (s.current) {
608
+ let l = s.current.parentElement;
609
+ for (; l && !d(l); )
605
610
  l = l.parentElement;
606
- l && i();
611
+ l && c();
607
612
  }
608
613
  };
609
- let e = (h = t.current) == null ? void 0 : h.parentElement;
610
- for (; e && !c(e); )
611
- e = e.parentElement;
612
- return e && e.addEventListener("scroll", o), o(), () => {
613
- e && e.removeEventListener("scroll", o);
614
+ let o = (g = s.current) == null ? void 0 : g.parentElement;
615
+ for (; o && !d(o); )
616
+ o = o.parentElement;
617
+ return o && o.addEventListener("scroll", i), i(), () => {
618
+ o && o.removeEventListener("scroll", i);
614
619
  };
615
620
  }, []);
616
- const c = (o) => o && (o.scrollHeight > o.clientHeight || o.scrollWidth > o.clientWidth);
617
- return /* @__PURE__ */ n("div", { ref: t, children: a });
618
- }, A = "starasia-input-dropdown-styles", U = (a) => {
619
- if (!document.getElementById(A)) {
620
- const i = document.createElement("style");
621
- i.id = A, i.textContent = a, document.head.appendChild(i);
621
+ const d = (i) => i && (i.scrollHeight > i.clientHeight || i.scrollWidth > i.clientWidth);
622
+ return /* @__PURE__ */ a("div", { ref: s, children: n });
623
+ }, S = "starasia-input-dropdown-styles", _ = (n) => {
624
+ if (!document.getElementById(S)) {
625
+ const c = document.createElement("style");
626
+ c.id = S, c.textContent = n, document.head.appendChild(c);
622
627
  }
623
628
  };
624
- U(P);
625
- const _ = ({
626
- dropdownLists: a,
627
- onChange: i,
628
- defaultValue: t,
629
- size: c = "md",
630
- multiSelect: o,
631
- searchAble: e,
632
- iconLeft: h,
629
+ _(U);
630
+ const or = ({
631
+ dropdownLists: n,
632
+ onChange: c,
633
+ defaultValue: s,
634
+ size: d = "md",
635
+ multiSelect: i,
636
+ searchAble: o,
637
+ iconLeft: g,
633
638
  placeholder: l,
634
- onSearch: v,
635
- error: I,
636
- onBlur: C,
637
- onFocus: R,
638
- disable: $,
639
- searchValue: z
639
+ onSearch: $,
640
+ error: v,
641
+ onBlur: N,
642
+ onFocus: I,
643
+ disable: W,
644
+ searchValue: f
640
645
  }) => {
641
- const w = k(), [r, x] = y([]), [m, E] = y(""), g = k(null), { isComponentVisible: B, ref: L, setIsComponentVisible: N } = V(!1), H = () => {
642
- N((s) => !s);
643
- }, j = (s, d) => {
644
- if (!o)
645
- x([{ label: s, value: d }]), i([{ label: s, value: d }]), N(!1);
646
- else if (r == null ? void 0 : r.find((u) => u.value === d)) {
646
+ const x = z(), [r, w] = R([]), [y, k] = R(""), [h, L] = R(-1), m = z(null), { isComponentVisible: C, ref: D, setIsComponentVisible: B } = G(!1), V = () => {
647
+ B((e) => !e);
648
+ }, A = (e, t) => {
649
+ if (!i)
650
+ w([{ label: e, value: t }]), c([{ label: e, value: t }]), B(!1);
651
+ else if (r == null ? void 0 : r.find((u) => u.value === t)) {
647
652
  const u = [
648
- ...r ? r.filter((D) => D.value !== d) : []
653
+ ...r ? r.filter((J) => J.value !== t) : []
649
654
  ];
650
- x(u), i(u);
655
+ w(u), c(u);
651
656
  } else {
652
- const u = [...r || [], { label: s, value: d }];
653
- x(u), i(u);
657
+ const u = [...r || [], { label: e, value: t }];
658
+ w(u), c(u);
654
659
  }
655
660
  };
656
661
  b(() => {
657
- JSON.stringify(w.current) === JSON.stringify(t) || (w.current = t, x(w.current || []));
658
- }, [t]);
659
- const O = () => r.length ? o ? r == null ? void 0 : r.map((s, d) => /* @__PURE__ */ p("div", { className: "starasia-input-dropdown-preview-item", children: [
660
- /* @__PURE__ */ n("p", { style: { minWidth: "max-content" }, children: s == null ? void 0 : s.label }, d),
661
- /* @__PURE__ */ n(
662
- X,
662
+ JSON.stringify(x.current) === JSON.stringify(s) || (x.current = s, w(x.current || []));
663
+ }, [s]);
664
+ const M = () => r.length ? i ? r == null ? void 0 : r.map((e, t) => /* @__PURE__ */ p("div", { className: "starasia-input-dropdown-preview-item", children: [
665
+ /* @__PURE__ */ a("p", { style: { minWidth: "max-content" }, children: e == null ? void 0 : e.label }, t),
666
+ /* @__PURE__ */ a(
667
+ rr,
663
668
  {
664
- onClick: () => j(s.label, s.value)
669
+ onClick: () => A(e.label, e.value)
665
670
  }
666
671
  )
667
- ] })) : /* @__PURE__ */ n("p", { style: { minWidth: "max-content" }, children: r == null ? void 0 : r[0].label }) : /* @__PURE__ */ n("p", { className: "starasia-input-dropdown-placeholder", children: l }), f = () => {
668
- if (L.current && g.current) {
669
- const s = window.innerHeight, d = L.current.getBoundingClientRect(), W = g.current.getBoundingClientRect(), u = d.bottom + W.height;
670
- g.current.style.left = `${d.left}px`, g.current.style.width = `${d.width}px`, u >= s - 10 ? (g.current.style.top = `${d.top - W.height - 5}px`, E("top")) : (g.current.style.top = `${d.top + d.height}px`, E("bottom"));
672
+ ] })) : /* @__PURE__ */ a("p", { style: { minWidth: "max-content" }, children: r == null ? void 0 : r[0].label }) : /* @__PURE__ */ a("p", { className: "starasia-input-dropdown-placeholder", children: l }), E = () => {
673
+ if (D.current && m.current) {
674
+ const e = window.innerHeight, t = D.current.getBoundingClientRect(), j = m.current.getBoundingClientRect(), u = t.bottom + j.height;
675
+ m.current.style.left = `${t.left}px`, m.current.style.width = `${t.width}px`, u >= e - 10 ? (m.current.style.top = `${t.top - j.height - 5}px`, k("top")) : (m.current.style.top = `${t.top + t.height}px`, k("bottom"));
671
676
  }
672
677
  };
673
- return b(() => (window.addEventListener("scroll", f), window.addEventListener("resize", f), () => {
674
- window.removeEventListener("scroll", f), window.removeEventListener("resize", f);
678
+ b(() => (window.addEventListener("scroll", E), window.addEventListener("resize", E), () => {
679
+ window.removeEventListener("scroll", E), window.removeEventListener("resize", E);
675
680
  }), []), b(() => {
676
- f();
677
- }, [B]), /* @__PURE__ */ n(T, { handleChangePosition: f, children: /* @__PURE__ */ p(
681
+ E();
682
+ }, [C]), b(() => {
683
+ C || L(-1);
684
+ }, [C]);
685
+ const P = (e) => {
686
+ if (!C) {
687
+ (e.key === "ArrowDown" || e.key === "Enter") && (e.preventDefault(), B(!0));
688
+ return;
689
+ }
690
+ switch (e.key) {
691
+ case "ArrowDown": {
692
+ e.preventDefault();
693
+ const t = q(n, h, 1);
694
+ t !== -1 && L(t);
695
+ break;
696
+ }
697
+ case "ArrowUp": {
698
+ e.preventDefault();
699
+ const t = q(n, h, -1);
700
+ t !== -1 && L(t);
701
+ break;
702
+ }
703
+ case "Enter": {
704
+ e.preventDefault(), h >= 0 && n[h] && !n[h].disable && A(
705
+ n[h].label,
706
+ n[h].value
707
+ );
708
+ break;
709
+ }
710
+ case "Escape": {
711
+ B(!1);
712
+ break;
713
+ }
714
+ }
715
+ };
716
+ return /* @__PURE__ */ a(Z, { handleChangePosition: E, children: /* @__PURE__ */ p(
678
717
  "div",
679
718
  {
680
- className: `starasia-input-dropdown-container ${$ ? "disable" : ""} starasia-input-dropdown-size-${c} ${B ? "active" : ""} ${I ? "error" : ""}`,
681
- ref: L,
682
- onClick: H,
719
+ className: `starasia-input-dropdown-container ${W ? "disable" : ""} starasia-input-dropdown-size-${d} ${C ? "active" : ""} ${v ? "error" : ""}`,
720
+ ref: D,
721
+ onClick: V,
722
+ onKeyDown: P,
723
+ tabIndex: 0,
724
+ onBlur: N,
725
+ onFocus: I,
683
726
  children: [
684
727
  /* @__PURE__ */ p("div", { className: "starasia-input-dropdown-preview-container", children: [
685
- h ? /* @__PURE__ */ n(q, { children: F.cloneElement(h, {
686
- width: S(c),
728
+ g ? /* @__PURE__ */ a(H, { children: O.cloneElement(g, {
729
+ width: F(d),
687
730
  color: "#939E99",
688
731
  style: {
689
732
  color: "#939E99"
690
733
  }
691
734
  }) }) : null,
692
- /* @__PURE__ */ n(
735
+ /* @__PURE__ */ a(
693
736
  "div",
694
737
  {
695
738
  className: "starasia-input-dropdown-container-preview-item",
696
- tabIndex: 100,
697
- onBlur: C,
698
- onFocus: R,
699
- children: O()
739
+ children: M()
700
740
  }
701
741
  ),
702
- /* @__PURE__ */ n(
703
- J,
742
+ /* @__PURE__ */ a(
743
+ Q,
704
744
  {
705
745
  strokeWidth: 2,
706
- width: S(c),
746
+ width: F(d),
707
747
  style: { color: "#939E99" }
708
748
  }
709
749
  )
710
750
  ] }),
711
- /* @__PURE__ */ n(
712
- Q,
751
+ /* @__PURE__ */ a(
752
+ Y,
713
753
  {
714
- dropdownLists: a,
715
- handleChangeValue: j,
716
- isComponentVisible: B,
717
- positionDropdown: m,
718
- searchAble: !!e,
754
+ dropdownLists: n,
755
+ handleChangeValue: A,
756
+ isComponentVisible: C,
757
+ positionDropdown: y,
758
+ searchAble: !!o,
719
759
  value: r,
720
- onSearch: v,
721
- ref: g,
722
- multiSelect: o,
723
- searchValue: z
760
+ onSearch: $,
761
+ ref: m,
762
+ multiSelect: i,
763
+ searchValue: f,
764
+ highlightedIndex: h
724
765
  }
725
766
  )
726
767
  ]
727
768
  }
728
769
  ) });
729
- }, S = (a) => {
730
- if (a === "sm")
770
+ }, q = (n, c, s) => {
771
+ const d = n.length;
772
+ let i = c + s;
773
+ for (; i >= 0 && i < d; ) {
774
+ if (!n[i].disable)
775
+ return i;
776
+ i += s;
777
+ }
778
+ if (s === 1) {
779
+ for (let o = 0; o < d; o++)
780
+ if (!n[o].disable)
781
+ return o;
782
+ } else
783
+ for (let o = d - 1; o >= 0; o--)
784
+ if (!n[o].disable)
785
+ return o;
786
+ return -1;
787
+ }, F = (n) => {
788
+ if (n === "sm")
731
789
  return 16;
732
- if (a === "md")
790
+ if (n === "md")
733
791
  return 16;
734
- if (a === "lg")
792
+ if (n === "lg")
735
793
  return 20;
736
- }, X = (a) => /* @__PURE__ */ n(
794
+ }, rr = (n) => /* @__PURE__ */ a(
737
795
  "svg",
738
796
  {
739
797
  xmlns: "http://www.w3.org/2000/svg",
@@ -741,8 +799,8 @@ const _ = ({
741
799
  height: "18",
742
800
  viewBox: "0 0 18 18",
743
801
  fill: "none",
744
- ...a,
745
- children: /* @__PURE__ */ n(
802
+ ...n,
803
+ children: /* @__PURE__ */ a(
746
804
  "path",
747
805
  {
748
806
  d: "M12 6L6 12M6.00001 6L12 12",
@@ -755,5 +813,5 @@ const _ = ({
755
813
  }
756
814
  );
757
815
  export {
758
- _ as Dropdown
816
+ or as Dropdown
759
817
  };
@@ -1,4 +1,4 @@
1
- (function(w,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],r):(w=typeof globalThis<"u"?globalThis:w||self,r(w.Dropdown={},w.jsxRuntime,w.React))})(this,function(w,r,o){"use strict";const q=`@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap");
1
+ (function(w,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],r):(w=typeof globalThis<"u"?globalThis:w||self,r(w.Dropdown={},w.jsxRuntime,w.React))})(this,function(w,r,e){"use strict";const F=`@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap");
2
2
  * {
3
3
  margin: 0;
4
4
  padding: 0;
@@ -264,6 +264,11 @@
264
264
  /* background: var(--starasia-ui-dropdown-background-color-secondary); */
265
265
  /* box-shadow: 0px 0px 0px -1px #f0f0f0; */
266
266
  }
267
+ .starasia-input-dropdwon-item.highlight {
268
+ background-color: rgba(var(--starasia-ui-raw-color-brand-secondary, 144, 176, 218), 0.35);
269
+ outline: 2px solid rgba(var(--starasia-ui-raw-color-brand-secondary, 144, 176, 218), 0.4);
270
+ outline-offset: -2px;
271
+ }
267
272
 
268
273
  .starasia-input-dropdwon-item-disable {
269
274
  color: var(--starasia-ui-dropdown-color-disable) !important;
@@ -394,4 +399,4 @@
394
399
  height: 100%;
395
400
  width: 100%;
396
401
  }
397
- `;function A(a){const[e,t]=o.useState(a),p=o.useRef(null),i=s=>{p.current&&!p.current.contains(s.target)&&t(!1)};return o.useEffect(()=>(document.addEventListener("click",i,!0),()=>{document.removeEventListener("click",i,!0)}),[]),{ref:p,isComponentVisible:e,setIsComponentVisible:t}}const D=({...a})=>r.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",...a,viewBox:"0 0 24 24",fill:"none",color:"#939E99",children:r.jsx("path",{d:"M4 9L12 17L20 9",stroke:"currentColor",strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5})}),F=({...a})=>r.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",...a,viewBox:"0 0 20 20",fill:"none",children:[r.jsx("circle",{cx:"9.80547",cy:"9.8055",r:"7.49047",stroke:"currentColor",strokeWidth:"1.3",strokeLinecap:"round",strokeLinejoin:"round"}),r.jsx("path",{d:"M15.0153 15.4043L17.9519 18.3333",stroke:"currentColor",strokeWidth:"1.3",strokeLinecap:"round",strokeLinejoin:"round"})]}),O=({isChecked:a})=>r.jsxs("div",{className:"checkbox-wrapper-30",children:[r.jsxs("span",{className:"checkbox",children:[r.jsx("input",{type:"checkbox",style:{pointerEvents:"none"},checked:a}),r.jsx("svg",{children:r.jsx("use",{xlinkHref:"#checkbox-30",className:"checkbox"})})]}),r.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",style:{display:"none"},children:r.jsx("symbol",{id:"checkbox-30",viewBox:"0 0 22 22",children:r.jsx("path",{fill:"none",stroke:"currentColor",d:"M5.5,11.3L9,14.8L20.2,3.3l0,0c-0.5-1-1.5-1.8-2.7-1.8h-13c-1.7,0-3,1.3-3,3v13c0,1.7,1.3,3,3,3h13 c1.7,0,3-1.3,3-3v-13c0-0.4-0.1-0.8-0.3-1.2"})})})]}),H=o.forwardRef(({positionDropdown:a,dropdownLists:e,isComponentVisible:t,searchAble:p,onSearch:i,value:s,handleChangeValue:g,multiSelect:l,searchValue:v},E)=>{const[m,z]=o.useState(0),[N,k]=o.useState(),h=o.useRef(null);return o.useEffect(()=>{if(h.current){const n=h.current.getBoundingClientRect();z(n.width)}},[h,t]),o.useEffect(()=>{e&&v?e.forEach(n=>{if(n.label.startsWith(v||"")){k(n.value);return}}):k(void 0)},[v,e]),r.jsx(r.Fragment,{children:t?r.jsxs("div",{className:"starasia-input-dropdown-lists-container",onClick:n=>n.stopPropagation(),ref:E,children:[p?r.jsxs("div",{className:"starasia-input-dropdown-container-dropdown-search",style:{order:a=="bottom"?0:2,position:"sticky",top:0,bottom:0,zIndex:1},children:[r.jsx("div",{style:{display:"flex"},children:r.jsx(F,{strokeWidth:2,width:20})}),r.jsx("input",{onChange:n=>{i&&i(n.target.value)},value:v,autoFocus:!0,style:{width:"100%"}})]}):null,r.jsx("div",{className:`starasia-input-dropdwon-item-container ${l?"multi":""}`,ref:h,children:e.map((n,x)=>{const f=!!(s!=null&&s.find(C=>C.value===n.value));return r.jsxs("div",{className:`starasia-input-dropdwon-item ${f?"active":" "} ${n.disable?"starasia-input-dropdwon-item-disable":null}`,onClick:()=>g(n.label,n.value),children:[l?r.jsx("div",{style:{marginRight:"8px"},children:r.jsx(O,{isChecked:f})}):null,n.icon?r.jsx("div",{children:o.cloneElement(n.icon,{width:16})}):null,r.jsxs("div",{children:[r.jsx("p",{className:`starasia-input-dropdwon-item-text ${f?"active":null} ${n.disable?"starasia-input-dropdwon-item-disable":null}`,style:{textWrap:"nowrap",width:`${m-12}px`,overflow:"hidden",textOverflow:"ellipsis"},children:n.label}),n.description?r.jsx("p",{className:`starasia-input-dropdwon-item-text-description ${f?"active":null} ${n.disable?"starasia-input-dropdwon-item-disable":null}`,style:{textWrap:"nowrap",width:`${m-12}px`,overflow:"hidden",textOverflow:"ellipsis"},children:n.description}):null]})]},x)})})]}):null})}),M=({children:a,handleChangePosition:e})=>{const t=o.useRef(null);o.useEffect(()=>{var g;const i=()=>{if(t.current){let l=t.current.parentElement;for(;l&&!p(l);)l=l.parentElement;l&&e()}};let s=(g=t.current)==null?void 0:g.parentElement;for(;s&&!p(s);)s=s.parentElement;return s&&s.addEventListener("scroll",i),i(),()=>{s&&s.removeEventListener("scroll",i)}},[]);const p=i=>i&&(i.scrollHeight>i.clientHeight||i.scrollWidth>i.clientWidth);return r.jsx("div",{ref:t,children:a})},W="starasia-input-dropdown-styles";(a=>{if(!document.getElementById(W)){const e=document.createElement("style");e.id=W,e.textContent=a,document.head.appendChild(e)}})(q);const P=({dropdownLists:a,onChange:e,defaultValue:t,size:p="md",multiSelect:i,searchAble:s,iconLeft:g,placeholder:l,onSearch:v,error:E,onBlur:m,onFocus:z,disable:N,searchValue:k})=>{const h=o.useRef(),[n,x]=o.useState([]),[f,C]=o.useState(""),b=o.useRef(null),{isComponentVisible:I,ref:B,setIsComponentVisible:S}=A(!1),V=()=>{S(d=>!d)},j=(d,c)=>{if(!i)x([{label:d,value:c}]),e([{label:d,value:c}]),S(!1);else if(n==null?void 0:n.find(u=>u.value===c)){const u=[...n?n.filter(G=>G.value!==c):[]];x(u),e(u)}else{const u=[...n||[],{label:d,value:c}];x(u),e(u)}};o.useEffect(()=>{JSON.stringify(h.current)===JSON.stringify(t)||(h.current=t,x(h.current||[]))},[t]);const J=()=>n.length?i?n==null?void 0:n.map((d,c)=>r.jsxs("div",{className:"starasia-input-dropdown-preview-item",children:[r.jsx("p",{style:{minWidth:"max-content"},children:d==null?void 0:d.label},c),r.jsx(T,{onClick:()=>j(d.label,d.value)})]})):r.jsx("p",{style:{minWidth:"max-content"},children:n==null?void 0:n[0].label}):r.jsx("p",{className:"starasia-input-dropdown-placeholder",children:l}),y=()=>{if(B.current&&b.current){const d=window.innerHeight,c=B.current.getBoundingClientRect(),L=b.current.getBoundingClientRect(),u=c.bottom+L.height;b.current.style.left=`${c.left}px`,b.current.style.width=`${c.width}px`,u>=d-10?(b.current.style.top=`${c.top-L.height-5}px`,C("top")):(b.current.style.top=`${c.top+c.height}px`,C("bottom"))}};return o.useEffect(()=>(window.addEventListener("scroll",y),window.addEventListener("resize",y),()=>{window.removeEventListener("scroll",y),window.removeEventListener("resize",y)}),[]),o.useEffect(()=>{y()},[I]),r.jsx(M,{handleChangePosition:y,children:r.jsxs("div",{className:`starasia-input-dropdown-container ${N?"disable":""} starasia-input-dropdown-size-${p} ${I?"active":""} ${E?"error":""}`,ref:B,onClick:V,children:[r.jsxs("div",{className:"starasia-input-dropdown-preview-container",children:[g?r.jsx(r.Fragment,{children:o.cloneElement(g,{width:$(p),color:"#939E99",style:{color:"#939E99"}})}):null,r.jsx("div",{className:"starasia-input-dropdown-container-preview-item",tabIndex:100,onBlur:m,onFocus:z,children:J()}),r.jsx(D,{strokeWidth:2,width:$(p),style:{color:"#939E99"}})]}),r.jsx(H,{dropdownLists:a,handleChangeValue:j,isComponentVisible:I,positionDropdown:f,searchAble:!!s,value:n,onSearch:v,ref:b,multiSelect:i,searchValue:k})]})})},$=a=>{if(a==="sm")return 16;if(a==="md")return 16;if(a==="lg")return 20},T=a=>r.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",...a,children:r.jsx("path",{d:"M12 6L6 12M6.00001 6L12 12",stroke:"currentColor","stroke-width":"1.5","stroke-linecap":"round","stroke-linejoin":"round"})});w.Dropdown=P,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
402
+ `;function O(a){const[l,d]=e.useState(a),c=e.useRef(null),i=o=>{c.current&&!c.current.contains(o.target)&&d(!1)};return e.useEffect(()=>(document.addEventListener("click",i,!0),()=>{document.removeEventListener("click",i,!0)}),[]),{ref:c,isComponentVisible:l,setIsComponentVisible:d}}const H=({...a})=>r.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",...a,viewBox:"0 0 24 24",fill:"none",color:"#939E99",children:r.jsx("path",{d:"M4 9L12 17L20 9",stroke:"currentColor",strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5})}),V=({...a})=>r.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",...a,viewBox:"0 0 20 20",fill:"none",children:[r.jsx("circle",{cx:"9.80547",cy:"9.8055",r:"7.49047",stroke:"currentColor",strokeWidth:"1.3",strokeLinecap:"round",strokeLinejoin:"round"}),r.jsx("path",{d:"M15.0153 15.4043L17.9519 18.3333",stroke:"currentColor",strokeWidth:"1.3",strokeLinecap:"round",strokeLinejoin:"round"})]}),M=({isChecked:a})=>r.jsxs("div",{className:"checkbox-wrapper-30",children:[r.jsxs("span",{className:"checkbox",children:[r.jsx("input",{type:"checkbox",style:{pointerEvents:"none"},checked:a}),r.jsx("svg",{children:r.jsx("use",{xlinkHref:"#checkbox-30",className:"checkbox"})})]}),r.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",style:{display:"none"},children:r.jsx("symbol",{id:"checkbox-30",viewBox:"0 0 22 22",children:r.jsx("path",{fill:"none",stroke:"currentColor",d:"M5.5,11.3L9,14.8L20.2,3.3l0,0c-0.5-1-1.5-1.8-2.7-1.8h-13c-1.7,0-3,1.3-3,3v13c0,1.7,1.3,3,3,3h13 c1.7,0,3-1.3,3-3v-13c0-0.4-0.1-0.8-0.3-1.2"})})})]}),P=e.forwardRef(({positionDropdown:a,dropdownLists:l,isComponentVisible:d,searchAble:c,onSearch:i,value:o,handleChangeValue:g,multiSelect:p,searchValue:B,highlightedIndex:v},$)=>{const[z,N]=e.useState(0),y=e.useRef(null),f=e.useRef([]);return e.useEffect(()=>{if(y.current){const n=y.current.getBoundingClientRect();N(n.width)}},[y,d]),e.useEffect(()=>{var n;v>=0&&f.current[v]&&((n=f.current[v])==null||n.scrollIntoView({block:"nearest"}))},[v]),r.jsx(r.Fragment,{children:d?r.jsxs("div",{className:"starasia-input-dropdown-lists-container",onClick:n=>n.stopPropagation(),ref:$,children:[c?r.jsxs("div",{className:"starasia-input-dropdown-container-dropdown-search",style:{order:a=="bottom"?0:2,position:"sticky",top:0,bottom:0,zIndex:1},children:[r.jsx("div",{style:{display:"flex"},children:r.jsx(V,{strokeWidth:2,width:20})}),r.jsx("input",{onChange:n=>{i&&i(n.target.value)},value:B,autoFocus:!0,style:{width:"100%"}})]}):null,r.jsx("div",{className:`starasia-input-dropdwon-item-container ${p?"multi":""}`,ref:y,children:l.map((n,h)=>{const m=!!(o!=null&&o.find(k=>k.value===n.value));return r.jsxs("div",{className:`starasia-input-dropdwon-item ${m?"active":""} ${h===v?"highlight":""} ${n.disable?"starasia-input-dropdwon-item-disable":""}`,ref:k=>{f.current[h]=k},onClick:()=>g(n.label,n.value),children:[p?r.jsx("div",{style:{marginRight:"8px"},children:r.jsx(M,{isChecked:m})}):null,n.icon?r.jsx("div",{children:e.cloneElement(n.icon,{width:16})}):null,r.jsxs("div",{children:[r.jsx("p",{className:`starasia-input-dropdwon-item-text ${m?"active":null} ${n.disable?"starasia-input-dropdwon-item-disable":null}`,style:{textWrap:"nowrap",width:`${z-12}px`,overflow:"hidden",textOverflow:"ellipsis"},children:n.label}),n.description?r.jsx("p",{className:`starasia-input-dropdwon-item-text-description ${m?"active":null} ${n.disable?"starasia-input-dropdwon-item-disable":null}`,style:{textWrap:"nowrap",width:`${z-12}px`,overflow:"hidden",textOverflow:"ellipsis"},children:n.description}):null]})]},h)})})]}):null})}),T=({children:a,handleChangePosition:l})=>{const d=e.useRef(null);e.useEffect(()=>{var g;const i=()=>{if(d.current){let p=d.current.parentElement;for(;p&&!c(p);)p=p.parentElement;p&&l()}};let o=(g=d.current)==null?void 0:g.parentElement;for(;o&&!c(o);)o=o.parentElement;return o&&o.addEventListener("scroll",i),i(),()=>{o&&o.removeEventListener("scroll",i)}},[]);const c=i=>i&&(i.scrollHeight>i.clientHeight||i.scrollWidth>i.clientWidth);return r.jsx("div",{ref:d,children:a})},j="starasia-input-dropdown-styles";(a=>{if(!document.getElementById(j)){const l=document.createElement("style");l.id=j,l.textContent=a,document.head.appendChild(l)}})(F);const J=({dropdownLists:a,onChange:l,defaultValue:d,size:c="md",multiSelect:i,searchAble:o,iconLeft:g,placeholder:p,onSearch:B,error:v,onBlur:$,onFocus:z,disable:N,searchValue:y})=>{const f=e.useRef(),[n,h]=e.useState([]),[m,k]=e.useState(""),[b,W]=e.useState(-1),x=e.useRef(null),{isComponentVisible:E,ref:D,setIsComponentVisible:I}=O(!1),U=()=>{I(s=>!s)},L=(s,t)=>{if(!i)h([{label:s,value:t}]),l([{label:s,value:t}]),I(!1);else if(n==null?void 0:n.find(u=>u.value===t)){const u=[...n?n.filter(X=>X.value!==t):[]];h(u),l(u)}else{const u=[...n||[],{label:s,value:t}];h(u),l(u)}};e.useEffect(()=>{JSON.stringify(f.current)===JSON.stringify(d)||(f.current=d,h(f.current||[]))},[d]);const G=()=>n.length?i?n==null?void 0:n.map((s,t)=>r.jsxs("div",{className:"starasia-input-dropdown-preview-item",children:[r.jsx("p",{style:{minWidth:"max-content"},children:s==null?void 0:s.label},t),r.jsx(K,{onClick:()=>L(s.label,s.value)})]})):r.jsx("p",{style:{minWidth:"max-content"},children:n==null?void 0:n[0].label}):r.jsx("p",{className:"starasia-input-dropdown-placeholder",children:p}),C=()=>{if(D.current&&x.current){const s=window.innerHeight,t=D.current.getBoundingClientRect(),S=x.current.getBoundingClientRect(),u=t.bottom+S.height;x.current.style.left=`${t.left}px`,x.current.style.width=`${t.width}px`,u>=s-10?(x.current.style.top=`${t.top-S.height-5}px`,k("top")):(x.current.style.top=`${t.top+t.height}px`,k("bottom"))}};e.useEffect(()=>(window.addEventListener("scroll",C),window.addEventListener("resize",C),()=>{window.removeEventListener("scroll",C),window.removeEventListener("resize",C)}),[]),e.useEffect(()=>{C()},[E]),e.useEffect(()=>{E||W(-1)},[E]);const Q=s=>{if(!E){(s.key==="ArrowDown"||s.key==="Enter")&&(s.preventDefault(),I(!0));return}switch(s.key){case"ArrowDown":{s.preventDefault();const t=A(a,b,1);t!==-1&&W(t);break}case"ArrowUp":{s.preventDefault();const t=A(a,b,-1);t!==-1&&W(t);break}case"Enter":{s.preventDefault(),b>=0&&a[b]&&!a[b].disable&&L(a[b].label,a[b].value);break}case"Escape":{I(!1);break}}};return r.jsx(T,{handleChangePosition:C,children:r.jsxs("div",{className:`starasia-input-dropdown-container ${N?"disable":""} starasia-input-dropdown-size-${c} ${E?"active":""} ${v?"error":""}`,ref:D,onClick:U,onKeyDown:Q,tabIndex:0,onBlur:$,onFocus:z,children:[r.jsxs("div",{className:"starasia-input-dropdown-preview-container",children:[g?r.jsx(r.Fragment,{children:e.cloneElement(g,{width:q(c),color:"#939E99",style:{color:"#939E99"}})}):null,r.jsx("div",{className:"starasia-input-dropdown-container-preview-item",children:G()}),r.jsx(H,{strokeWidth:2,width:q(c),style:{color:"#939E99"}})]}),r.jsx(P,{dropdownLists:a,handleChangeValue:L,isComponentVisible:E,positionDropdown:m,searchAble:!!o,value:n,onSearch:B,ref:x,multiSelect:i,searchValue:y,highlightedIndex:b})]})})},A=(a,l,d)=>{const c=a.length;let i=l+d;for(;i>=0&&i<c;){if(!a[i].disable)return i;i+=d}if(d===1){for(let o=0;o<c;o++)if(!a[o].disable)return o}else for(let o=c-1;o>=0;o--)if(!a[o].disable)return o;return-1},q=a=>{if(a==="sm")return 16;if(a==="md")return 16;if(a==="lg")return 20},K=a=>r.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",...a,children:r.jsx("path",{d:"M12 6L6 12M6.00001 6L12 12",stroke:"currentColor","stroke-width":"1.5","stroke-linecap":"round","stroke-linejoin":"round"})});w.Dropdown=J,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@starasia/dropdown",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "dropdown component for starasia UI",
5
5
  "author": "Prawito Hudoro",
6
6
  "main": "dist/dropdown.umd.js",
@@ -12,10 +12,6 @@
12
12
  "dist/*.map",
13
13
  "dist/*.css"
14
14
  ],
15
- "scripts": {
16
- "dev": "vite --config vite.config.ts --port 3000",
17
- "build": "vite build --config vite.config.ts"
18
- },
19
15
  "keywords": [],
20
16
  "license": "ISC",
21
17
  "type": "module",
@@ -37,5 +33,9 @@
37
33
  "react-dom": "^18.2.0",
38
34
  "@types/react": "^18.2.55",
39
35
  "@types/react-dom": "^18.2.19"
36
+ },
37
+ "scripts": {
38
+ "dev": "vite --config vite.config.ts --port 3000",
39
+ "build": "vite build --config vite.config.ts"
40
40
  }
41
- }
41
+ }