@sustaina/shared-ui 1.1.8 → 1.2.0

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/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React3 from 'react';
2
2
  import React3__default, { createContext, isValidElement, useState, useEffect, useId, useContext, useRef, useCallback, useMemo, createElement } from 'react';
3
- import { Controller, useFormContext, useForm, FormProvider, get, set, appendErrors, useFormState } from 'react-hook-form';
3
+ import { Controller, useFormContext, useForm, useFieldArray, FormProvider, get, set, appendErrors, useFormState } from 'react-hook-form';
4
4
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
5
5
  import clsx3, { clsx } from 'clsx';
6
6
  import { twMerge } from 'tailwind-merge';
@@ -11,7 +11,7 @@ import { Slot } from '@radix-ui/react-slot';
11
11
  import { cva } from 'class-variance-authority';
12
12
  import * as LabelPrimitive2 from '@radix-ui/react-label';
13
13
  import * as DialogPrimitive from '@radix-ui/react-dialog';
14
- import { SortableContext, verticalListSortingStrategy, arrayMove, useSortable } from '@dnd-kit/sortable';
14
+ import { SortableContext, verticalListSortingStrategy, useSortable } from '@dnd-kit/sortable';
15
15
  import { CSS } from '@dnd-kit/utilities';
16
16
  import * as SelectPrimitive from '@radix-ui/react-select';
17
17
  import * as TooltipPrimitive from '@radix-ui/react-tooltip';
@@ -1420,19 +1420,17 @@ function TooltipContent({
1420
1420
  ) });
1421
1421
  }
1422
1422
  function SortableRow({
1423
- itemId,
1424
- index,
1425
- control,
1426
1423
  availableColumns,
1427
1424
  currentColumns,
1428
- disableDrag = false,
1429
- disableEdit = false,
1430
- hideFormMessage,
1431
- removeColumn
1425
+ control,
1426
+ name,
1427
+ value,
1428
+ isDragging,
1429
+ onRemove
1432
1430
  }) {
1433
1431
  const { attributes, listeners, setNodeRef, transform, transition } = useSortable({
1434
- id: itemId,
1435
- disabled: disableDrag
1432
+ id: value,
1433
+ disabled: name == "columns.0.id"
1436
1434
  });
1437
1435
  const style = {
1438
1436
  transform: CSS.Transform.toString(transform),
@@ -1447,35 +1445,33 @@ function SortableRow({
1447
1445
  FormField2,
1448
1446
  {
1449
1447
  control,
1450
- name: `columns.${index}.id`,
1448
+ name,
1451
1449
  render: ({ field }) => /* @__PURE__ */ jsxs(FormItem2, { className: "flex-1", children: [
1452
1450
  /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
1453
1451
  /* @__PURE__ */ jsx(
1454
1452
  GripVertical,
1455
1453
  {
1454
+ ...attributes,
1455
+ ...listeners,
1456
1456
  className: cn(
1457
1457
  "h-5 w-5 text-[#B9B9B9] focus:outline-none cursor-grab",
1458
- index == 0 && "opacity-0 pointer-events-none"
1459
- ),
1460
- ...attributes,
1461
- ...listeners
1458
+ name == "columns.0.id" && "opacity-0 pointer-events-none"
1459
+ )
1462
1460
  }
1463
1461
  ),
1464
1462
  /* @__PURE__ */ jsxs(
1465
1463
  Select,
1466
1464
  {
1467
1465
  value: field.value,
1468
- onValueChange: (val) => {
1469
- field.onChange(val);
1470
- },
1471
- disabled: disableEdit || index == 0,
1466
+ onValueChange: field.onChange,
1467
+ disabled: name == "columns.0.id",
1472
1468
  children: [
1473
1469
  /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsx(
1474
1470
  SelectTrigger,
1475
1471
  {
1476
1472
  className: cn(
1477
1473
  "w-full border-[#DDDDDD] data-[disabled]:opacity-100 aria-invalid:border-[#BB0B0E]",
1478
- index == 0 ? "text-[#8B8B8B] bg-[#EAEAEA] cursor-not-allowed" : ""
1474
+ name == "columns.0.id" ? "text-[#8B8B8B] bg-[#EAEAEA] cursor-not-allowed" : ""
1479
1475
  ),
1480
1476
  children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Choose column..." })
1481
1477
  }
@@ -1497,7 +1493,7 @@ function SortableRow({
1497
1493
  ]
1498
1494
  }
1499
1495
  ),
1500
- index == 0 ? /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsxs(Tooltip, { children: [
1496
+ name == "columns.0.id" ? /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsxs(Tooltip, { children: [
1501
1497
  /* @__PURE__ */ jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsx(Info, { className: "h-5 w-5", stroke: "white", fill: "#8B8B8B" }) }),
1502
1498
  /* @__PURE__ */ jsx(
1503
1499
  TooltipContent,
@@ -1512,14 +1508,14 @@ function SortableRow({
1512
1508
  ] }) }) : /* @__PURE__ */ jsx(
1513
1509
  CircleMinus,
1514
1510
  {
1515
- onClick: () => index > 0 && removeColumn(index),
1511
+ onClick: onRemove,
1516
1512
  className: "h-5 w-5 cursor-pointer",
1517
1513
  stroke: "white",
1518
1514
  fill: "#C32A2C"
1519
1515
  }
1520
1516
  )
1521
1517
  ] }),
1522
- /* @__PURE__ */ jsx(FormMessage, { className: cn("pl-6 text-[#BB0B0E]", hideFormMessage && "hidden") })
1518
+ !isDragging && /* @__PURE__ */ jsx(FormMessage, { className: cn("pl-6 text-[#BB0B0E]") })
1523
1519
  ] })
1524
1520
  }
1525
1521
  ) });
@@ -1594,18 +1590,31 @@ var GridSettingsModal = ({
1594
1590
  onClose,
1595
1591
  onSaveColumns
1596
1592
  }) => {
1597
- const [hideFormMessage, setHideFormMessage] = useState(false);
1593
+ const [isDragging, setIsDragging] = useState(false);
1598
1594
  const form = useForm({
1599
1595
  resolver: t2(GridSettingsSchema),
1600
1596
  defaultValues: { columns: currentColumns },
1601
1597
  mode: "onChange"
1602
1598
  });
1599
+ const { control, trigger } = form;
1600
+ const { fields, append, remove, move } = useFieldArray({
1601
+ control,
1602
+ name: "columns",
1603
+ keyName: "fieldId"
1604
+ });
1603
1605
  useEffect(() => {
1604
1606
  if (isOpen) {
1605
1607
  form.reset({ columns: currentColumns });
1606
1608
  }
1607
1609
  }, [isOpen, currentColumns, form]);
1608
- const columns = form.watch("columns");
1610
+ const addColumn = async () => {
1611
+ const isValid2 = await trigger("columns");
1612
+ if (isValid2) {
1613
+ append({ id: "" });
1614
+ } else {
1615
+ return;
1616
+ }
1617
+ };
1609
1618
  const onSubmit = (data) => {
1610
1619
  const ordering = data.columns.map((i2) => i2.id);
1611
1620
  const visibility = Object.fromEntries(
@@ -1616,28 +1625,16 @@ var GridSettingsModal = ({
1616
1625
  onSaveColumns({ ordering, visibility, pinning });
1617
1626
  }
1618
1627
  };
1619
- const addColumn = async () => {
1620
- const isValid2 = await form.trigger("columns");
1621
- if (!isValid2) {
1622
- return;
1623
- }
1624
- form.setValue("columns", [...columns, { id: "" }], { shouldValidate: false });
1625
- };
1626
- const removeColumn = (index) => {
1627
- const newCols = [...columns];
1628
- newCols.splice(index, 1);
1629
- form.setValue("columns", newCols);
1630
- };
1631
1628
  const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 5 } }));
1632
- const handleDragEnd = (event) => {
1629
+ function handleDragEnd(event) {
1633
1630
  const { active, over } = event;
1634
1631
  if (!over || active.id === over.id) return;
1635
- const oldIndex = columns.findIndex((c) => c.id === active.id);
1636
- const newIndex = columns.findIndex((c) => c.id === over.id);
1637
- if (newIndex === 0) return;
1638
- const reordered = arrayMove(columns, oldIndex, newIndex);
1639
- form.setValue("columns", reordered, { shouldValidate: true });
1640
- };
1632
+ const oldIndex = fields.findIndex((f) => f.fieldId === active.id);
1633
+ const newIndex = fields.findIndex((f) => f.fieldId === over.id);
1634
+ if (oldIndex !== -1 && newIndex !== -1) {
1635
+ move(oldIndex, newIndex);
1636
+ }
1637
+ }
1641
1638
  return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(Dialog, { open: isOpen, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxs(DialogContent, { className: "sm:max-w-xl p-0 border-0", showCloseButton: false, children: [
1642
1639
  /* @__PURE__ */ jsx(DialogClose, { asChild: true, children: /* @__PURE__ */ jsx(
1643
1640
  "button",
@@ -1669,43 +1666,42 @@ var GridSettingsModal = ({
1669
1666
  /* @__PURE__ */ jsx(
1670
1667
  SortableRow,
1671
1668
  {
1672
- itemId: columns[0]?.id,
1673
- index: 0,
1674
- control: form.control,
1669
+ value: fields[0]?.fieldId,
1670
+ control,
1671
+ name: `columns.0.id`,
1672
+ isDragging,
1675
1673
  availableColumns,
1676
- currentColumns: columns,
1677
- disableDrag: true,
1678
- disableEdit: true,
1679
- removeColumn
1680
- }
1674
+ currentColumns: fields
1675
+ },
1676
+ fields[0]?.fieldId
1681
1677
  ),
1682
1678
  /* @__PURE__ */ jsx(
1683
1679
  DndContext,
1684
1680
  {
1685
1681
  sensors,
1686
1682
  collisionDetection: closestCenter,
1687
- onDragStart: () => setHideFormMessage(true),
1683
+ onDragStart: () => setIsDragging(true),
1688
1684
  onDragEnd: (event) => {
1689
- setHideFormMessage(false);
1685
+ setIsDragging(false);
1690
1686
  handleDragEnd(event);
1691
1687
  },
1692
1688
  children: /* @__PURE__ */ jsx(
1693
1689
  SortableContext,
1694
1690
  {
1695
- items: columns?.slice(1).map((c) => c.id),
1691
+ items: fields?.slice(1).map((c) => c?.fieldId),
1696
1692
  strategy: verticalListSortingStrategy,
1697
- children: columns?.slice(1).map((item, index) => /* @__PURE__ */ jsx(
1693
+ children: fields?.slice(1).map((col, index) => /* @__PURE__ */ jsx(
1698
1694
  SortableRow,
1699
1695
  {
1700
- itemId: item.id,
1701
- index: index + 1,
1702
- control: form.control,
1696
+ value: col?.fieldId,
1697
+ control,
1698
+ name: `columns.${index + 1}.id`,
1699
+ isDragging,
1703
1700
  availableColumns,
1704
- currentColumns: columns,
1705
- hideFormMessage,
1706
- removeColumn
1701
+ currentColumns: fields,
1702
+ onRemove: () => remove(index + 1)
1707
1703
  },
1708
- item.id + index
1704
+ col?.fieldId
1709
1705
  ))
1710
1706
  }
1711
1707
  )
@@ -1717,7 +1713,7 @@ var GridSettingsModal = ({
1717
1713
  type: "button",
1718
1714
  className: cn("bg-[#41875C99] text-white w-full rounded-t-lg", addButtonClassname),
1719
1715
  onClick: addColumn,
1720
- disabled: columns.length >= limit,
1716
+ disabled: fields.length >= limit,
1721
1717
  children: [
1722
1718
  /* @__PURE__ */ jsx(Plus, { className: "h-4 w-4 mr-1" }),
1723
1719
  " Add column"
@@ -4817,6 +4813,7 @@ var AdvanceSearch = ({
4817
4813
  onClick: () => {
4818
4814
  clearAllRow();
4819
4815
  Object.keys(getValues()).forEach((k) => resetField(k));
4816
+ onSubmit();
4820
4817
  },
4821
4818
  children: "Clear Search"
4822
4819
  }
@@ -4836,7 +4833,176 @@ var AdvanceSearch = ({
4836
4833
  );
4837
4834
  };
4838
4835
  var AdvanceSearch_default = AdvanceSearch;
4836
+ function cn3(...inputs) {
4837
+ return twMerge(clsx(inputs));
4838
+ }
4839
+ function Dialog2({ ...props }) {
4840
+ return /* @__PURE__ */ jsx(DialogPrimitive.Root, { "data-slot": "dialog", ...props });
4841
+ }
4842
+ function DialogPortal2({ ...props }) {
4843
+ return /* @__PURE__ */ jsx(DialogPrimitive.Portal, { "data-slot": "dialog-portal", ...props });
4844
+ }
4845
+ function DialogOverlay2({ className, ...props }) {
4846
+ return /* @__PURE__ */ jsx(
4847
+ DialogPrimitive.Overlay,
4848
+ {
4849
+ "data-slot": "dialog-overlay",
4850
+ className: cn3(
4851
+ "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/50",
4852
+ className
4853
+ ),
4854
+ ...props
4855
+ }
4856
+ );
4857
+ }
4858
+ function DialogContent2({
4859
+ className,
4860
+ children,
4861
+ showCloseButton = true,
4862
+ ...props
4863
+ }) {
4864
+ return /* @__PURE__ */ jsxs(DialogPortal2, { "data-slot": "dialog-portal", children: [
4865
+ /* @__PURE__ */ jsx(DialogOverlay2, {}),
4866
+ /* @__PURE__ */ jsxs(
4867
+ DialogPrimitive.Content,
4868
+ {
4869
+ "data-slot": "dialog-content",
4870
+ className: cn3(
4871
+ "bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border p-6 shadow-lg duration-200 sm:max-w-lg",
4872
+ className
4873
+ ),
4874
+ ...props,
4875
+ children: [
4876
+ children,
4877
+ showCloseButton && /* @__PURE__ */ jsxs(
4878
+ DialogPrimitive.Close,
4879
+ {
4880
+ "data-slot": "dialog-close",
4881
+ className: "ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground absolute top-4 right-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
4882
+ children: [
4883
+ /* @__PURE__ */ jsx(XIcon, {}),
4884
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Close" })
4885
+ ]
4886
+ }
4887
+ )
4888
+ ]
4889
+ }
4890
+ )
4891
+ ] });
4892
+ }
4893
+ function DialogHeader2({ className, ...props }) {
4894
+ return /* @__PURE__ */ jsx(
4895
+ "div",
4896
+ {
4897
+ "data-slot": "dialog-header",
4898
+ className: cn3("flex flex-col gap-2 text-center sm:text-left", className),
4899
+ ...props
4900
+ }
4901
+ );
4902
+ }
4903
+ function DialogTitle2({ className, ...props }) {
4904
+ return /* @__PURE__ */ jsx(
4905
+ DialogPrimitive.Title,
4906
+ {
4907
+ "data-slot": "dialog-title",
4908
+ className: cn3("text-lg leading-none font-semibold", className),
4909
+ ...props
4910
+ }
4911
+ );
4912
+ }
4913
+ function DialogDescription2({
4914
+ className,
4915
+ ...props
4916
+ }) {
4917
+ return /* @__PURE__ */ jsx(
4918
+ DialogPrimitive.Description,
4919
+ {
4920
+ "data-slot": "dialog-description",
4921
+ className: cn3("text-muted-foreground text-sm", className),
4922
+ ...props
4923
+ }
4924
+ );
4925
+ }
4926
+ var buttonVariants4 = cva(
4927
+ "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-all disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 shrink-0 [&_svg]:shrink-0 outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive cursor-pointer",
4928
+ {
4929
+ variants: {
4930
+ variant: {
4931
+ default: "bg-sus-primary-1 text-primary-foreground shadow-xs hover:bg-sus-primary/90",
4932
+ cancel: "border bg-[#8B8B8B] text-white shadow-xs hover:bg-accent hover:text-accent-foreground dark:border-input dark:hover:bg-input/50",
4933
+ outlineLeave: "border border-[#BB0B0E] bg-background shadow-xs hover:bg-accent hover:text-accent-foreground text-[#BB0B0E]"
4934
+ },
4935
+ size: {
4936
+ default: "h-9 px-4 has-[>svg]:px-3",
4937
+ option: "py-5 h-9 px-4 has-[>svg]:px-3",
4938
+ sm: "h-8 rounded-md gap-1.5 px-3 has-[>svg]:px-2.5",
4939
+ lg: "h-10 rounded-md px-6 has-[>svg]:px-4",
4940
+ icon: "size-9",
4941
+ "icon-xs": "size-5",
4942
+ "icon-sm": "size-[22px]",
4943
+ "icon-md": "size-7",
4944
+ "icon-lg": "size-10"
4945
+ },
4946
+ active: {
4947
+ true: "bg-sus-primary-1 text-white",
4948
+ false: ""
4949
+ }
4950
+ },
4951
+ defaultVariants: {
4952
+ variant: "default",
4953
+ size: "default"
4954
+ }
4955
+ }
4956
+ );
4957
+ function Button4({
4958
+ className,
4959
+ variant,
4960
+ size,
4961
+ active,
4962
+ asChild = false,
4963
+ ...props
4964
+ }) {
4965
+ const Comp = asChild ? Slot : "button";
4966
+ return /* @__PURE__ */ jsx(
4967
+ Comp,
4968
+ {
4969
+ "data-slot": "button",
4970
+ className: cn3(buttonVariants4({ variant, size, className, active })),
4971
+ ...props
4972
+ }
4973
+ );
4974
+ }
4975
+ function ConfirmDialog({ dialogData, setDialog, onClose }) {
4976
+ const handleClose = () => {
4977
+ setDialog({ state: false });
4978
+ onClose?.();
4979
+ };
4980
+ const titleColor = {
4981
+ default: "text-[#000000]",
4982
+ success: "text-[#3f9e33]",
4983
+ error: "text-[#c63638]"
4984
+ };
4985
+ const showCloseBtn = dialogData.showCloseBtn ?? true;
4986
+ return /* @__PURE__ */ jsx(Dialog2, { open: dialogData.state, onOpenChange: handleClose, children: /* @__PURE__ */ jsxs(DialogContent2, { children: [
4987
+ /* @__PURE__ */ jsxs(DialogHeader2, { children: [
4988
+ /* @__PURE__ */ jsx(DialogTitle2, { className: cn3(titleColor[dialogData.variant || "default"], "font-bold"), children: dialogData.title || "" }),
4989
+ /* @__PURE__ */ jsx(DialogDescription2, { children: dialogData.description || "" })
4990
+ ] }),
4991
+ /* @__PURE__ */ jsxs("div", { className: "w-full flex justify-center py-5 gap-5", children: [
4992
+ showCloseBtn && /* @__PURE__ */ jsx(Button4, { variant: "cancel", onClick: handleClose, children: "Close" }),
4993
+ dialogData.btn && /* @__PURE__ */ jsx(
4994
+ Button4,
4995
+ {
4996
+ variant: dialogData.variantBtn || "default",
4997
+ disabled: dialogData.btnState === false,
4998
+ onClick: dialogData.onClickBtn,
4999
+ children: dialogData.btn
5000
+ }
5001
+ )
5002
+ ] })
5003
+ ] }) });
5004
+ }
4839
5005
 
4840
- export { AdvanceSearch_default as AdvanceSearch, Button, DataTable_default as DataTable, FormErrorMessage, FormField, FormFieldContext, FormItem, FormItemContext, FormLabel, GridSettingsModal_default as GridSettingsModal, HeaderCell_default as HeaderCell, navbar_default as Navbar, NumberInput, TextInput, booleanToSelectValue, buttonVariants, cn, compareAlphanumeric, getColumnIdFromTable, renderContentSlot, selectValueToBoolean, useFormField, useGridSettingsStore, useHover_default as useHover, useIntersectionObserver_default as useIntersectionObserver, useMediaQuery_default as useMediaQuery, useScreenSize_default as useScreenSize };
5006
+ export { AdvanceSearch_default as AdvanceSearch, Button, DataTable_default as DataTable, ConfirmDialog as Dialog, FormErrorMessage, FormField, FormFieldContext, FormItem, FormItemContext, FormLabel, GridSettingsModal_default as GridSettingsModal, HeaderCell_default as HeaderCell, navbar_default as Navbar, NumberInput, TextInput, booleanToSelectValue, buttonVariants, cn, compareAlphanumeric, getColumnIdFromTable, renderContentSlot, selectValueToBoolean, useFormField, useGridSettingsStore, useHover_default as useHover, useIntersectionObserver_default as useIntersectionObserver, useMediaQuery_default as useMediaQuery, useScreenSize_default as useScreenSize };
4841
5007
  //# sourceMappingURL=index.mjs.map
4842
5008
  //# sourceMappingURL=index.mjs.map