@sustaina/shared-ui 1.3.0 → 1.4.1

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
- import * as React3 from 'react';
2
- import React3__default, { createContext, isValidElement, useState, useEffect, useId, useContext, useRef, useCallback, useMemo, createElement } from 'react';
3
- import { Controller, useFormContext, useForm, useFieldArray, FormProvider, get, set, appendErrors, useFormState } from 'react-hook-form';
1
+ import * as React4 from 'react';
2
+ import React4__default, { createContext, isValidElement, useState, useEffect, useId, useContext, useRef, useCallback, useMemo, createElement } from 'react';
3
+ import { Controller, useFormContext, useForm, useFieldArray, FormProvider, useWatch, 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';
@@ -980,6 +980,38 @@ var DataTable = ({
980
980
  );
981
981
  };
982
982
  var DataTable_default = DataTable;
983
+ var usePreventPageLeaveStore = create((set) => ({
984
+ isPreventing: false,
985
+ setPreventing: (value) => set({ isPreventing: value })
986
+ }));
987
+ var usePreventPageLeaveStore_default = usePreventPageLeaveStore;
988
+
989
+ // src/components/prevent-page-leave/PreventPageLeave.tsx
990
+ var PreventPageLeave = ({ children }) => {
991
+ const { isPreventing } = usePreventPageLeaveStore();
992
+ useEffect(() => {
993
+ if (!isPreventing || typeof window === "undefined") {
994
+ return;
995
+ }
996
+ function beforeUnload(event) {
997
+ event.preventDefault();
998
+ event.returnValue = true;
999
+ }
1000
+ window.addEventListener("beforeunload", beforeUnload);
1001
+ return () => {
1002
+ window.removeEventListener("beforeunload", beforeUnload);
1003
+ };
1004
+ }, [isPreventing]);
1005
+ return children;
1006
+ };
1007
+ var PreventPageLeave_default = PreventPageLeave;
1008
+ var usePreventPageLeave = ({ isPrevening }) => {
1009
+ const setPreventing = usePreventPageLeaveStore_default((state) => state.setPreventing);
1010
+ useEffect(() => {
1011
+ setPreventing(isPrevening);
1012
+ }, [isPrevening, setPreventing]);
1013
+ };
1014
+ var usePreventPageLeave_default = usePreventPageLeave;
983
1015
  var buttonVariants = cva(
984
1016
  "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",
985
1017
  {
@@ -1103,15 +1135,15 @@ var t2 = function(o3, t3, s2) {
1103
1135
  };
1104
1136
  };
1105
1137
  var Form = FormProvider;
1106
- var FormFieldContext2 = React3.createContext({});
1138
+ var FormFieldContext2 = React4.createContext({});
1107
1139
  var FormField2 = ({
1108
1140
  ...props
1109
1141
  }) => {
1110
1142
  return /* @__PURE__ */ jsx(FormFieldContext2.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsx(Controller, { ...props }) });
1111
1143
  };
1112
1144
  var useFormField2 = () => {
1113
- const fieldContext = React3.useContext(FormFieldContext2);
1114
- const itemContext = React3.useContext(FormItemContext2);
1145
+ const fieldContext = React4.useContext(FormFieldContext2);
1146
+ const itemContext = React4.useContext(FormItemContext2);
1115
1147
  const { getFieldState } = useFormContext();
1116
1148
  const formState = useFormState({ name: fieldContext.name });
1117
1149
  const fieldState = getFieldState(fieldContext.name, formState);
@@ -1128,9 +1160,9 @@ var useFormField2 = () => {
1128
1160
  ...fieldState
1129
1161
  };
1130
1162
  };
1131
- var FormItemContext2 = React3.createContext({});
1163
+ var FormItemContext2 = React4.createContext({});
1132
1164
  function FormItem2({ className, ...props }) {
1133
- const id = React3.useId();
1165
+ const id = React4.useId();
1134
1166
  return /* @__PURE__ */ jsx(FormItemContext2.Provider, { value: { id }, children: /* @__PURE__ */ jsx("div", { "data-slot": "form-item", className: cn("grid gap-2", className), ...props }) });
1135
1167
  }
1136
1168
  function FormControl({ ...props }) {
@@ -1421,7 +1453,6 @@ function TooltipContent({
1421
1453
  }
1422
1454
  function SortableRow({
1423
1455
  availableColumns,
1424
- currentColumns,
1425
1456
  control,
1426
1457
  name,
1427
1458
  value,
@@ -1437,88 +1468,86 @@ function SortableRow({
1437
1468
  transition
1438
1469
  };
1439
1470
  const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);
1440
- const options = availableColumns.map((col) => ({
1441
- id: col.id,
1442
- label: capitalize(col.id)
1443
- }));
1471
+ const currentFormColumns = useWatch({ control, name: "columns" });
1444
1472
  return /* @__PURE__ */ jsx("div", { ref: setNodeRef, style, className: "flex items-center rounded-md bg-white", children: /* @__PURE__ */ jsx(
1445
1473
  FormField2,
1446
1474
  {
1447
1475
  control,
1448
1476
  name,
1449
- render: ({ field }) => /* @__PURE__ */ jsxs(FormItem2, { className: "flex-1", children: [
1450
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
1451
- /* @__PURE__ */ jsx(
1452
- GripVertical,
1453
- {
1454
- ...attributes,
1455
- ...listeners,
1456
- className: cn(
1457
- "h-5 w-5 text-[#B9B9B9] focus:outline-none cursor-grab",
1458
- name == "columns.0.id" && "opacity-0 pointer-events-none"
1459
- )
1460
- }
1461
- ),
1462
- /* @__PURE__ */ jsxs(
1463
- Select,
1464
- {
1465
- value: field.value,
1466
- onValueChange: field.onChange,
1467
- disabled: name == "columns.0.id",
1468
- children: [
1469
- /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsx(
1470
- SelectTrigger,
1471
- {
1472
- className: cn(
1473
- "w-full border-[#DDDDDD] data-[disabled]:opacity-100 aria-invalid:border-[#BB0B0E]",
1474
- name == "columns.0.id" ? "text-[#8B8B8B] bg-[#EAEAEA] cursor-not-allowed" : ""
1475
- ),
1476
- children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Choose column..." })
1477
- }
1478
- ) }),
1479
- /* @__PURE__ */ jsx(SelectContent, { children: options.filter(
1480
- (i2) => i2.id === field.value || !currentColumns?.some((c, idx) => c.id === i2.id && idx !== index)
1481
- ).sort(
1482
- (a, b) => a.id === field.value ? -1 : b.id === field.value ? 1 : 0
1483
- ).map((opt) => /* @__PURE__ */ jsx(
1484
- SelectItem,
1485
- {
1486
- value: opt.id,
1487
- className: cn(
1488
- "focus:bg-[#e8edea]",
1489
- opt.id === field.value ? "font-bold bg-[#dae5de] focus:bg-[#dae5de]" : ""
1490
- ),
1491
- children: opt.label
1492
- },
1493
- opt.id
1494
- )) })
1495
- ]
1496
- }
1497
- ),
1498
- name == "columns.0.id" ? /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsxs(Tooltip, { children: [
1499
- /* @__PURE__ */ jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsx(Info, { className: "h-5 w-5", stroke: "white", fill: "#8B8B8B" }) }),
1477
+ render: ({ field }) => {
1478
+ const options = availableColumns.filter((col) => col.id === field.value || !currentFormColumns?.some((c) => c.id === col.id)).map((col) => ({ id: col.id, label: capitalize(col.id) }));
1479
+ return /* @__PURE__ */ jsxs(FormItem2, { className: "flex-1", children: [
1480
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
1500
1481
  /* @__PURE__ */ jsx(
1501
- TooltipContent,
1482
+ GripVertical,
1502
1483
  {
1503
- align: "start",
1504
- alignOffset: 10,
1505
- className: "bg-[#8B8B8B] rounded-none",
1506
- arrowClassName: "!hidden",
1507
- children: /* @__PURE__ */ jsx("p", { children: "Column cannot be removed." })
1484
+ ...attributes,
1485
+ ...listeners,
1486
+ className: cn(
1487
+ "h-5 w-5 text-[#B9B9B9] focus:outline-none cursor-grab",
1488
+ name == "columns.0.id" && "opacity-0 pointer-events-none"
1489
+ )
1490
+ }
1491
+ ),
1492
+ /* @__PURE__ */ jsxs(
1493
+ Select,
1494
+ {
1495
+ value: field.value,
1496
+ onValueChange: field.onChange,
1497
+ disabled: name == "columns.0.id",
1498
+ children: [
1499
+ /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsx(
1500
+ SelectTrigger,
1501
+ {
1502
+ className: cn(
1503
+ "w-full border-[#DDDDDD] data-[disabled]:opacity-100 aria-invalid:border-[#BB0B0E]",
1504
+ name == "columns.0.id" ? "text-[#8B8B8B] bg-[#EAEAEA] cursor-not-allowed" : ""
1505
+ ),
1506
+ children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Choose column..." })
1507
+ }
1508
+ ) }),
1509
+ /* @__PURE__ */ jsx(SelectContent, { children: options.sort(
1510
+ (a, b) => a.id === field.value ? -1 : b.id === field.value ? 1 : 0
1511
+ ).map((opt) => /* @__PURE__ */ jsx(
1512
+ SelectItem,
1513
+ {
1514
+ value: opt.id,
1515
+ className: cn(
1516
+ "focus:bg-[#e8edea]",
1517
+ opt.id === field.value ? "font-bold bg-[#dae5de] focus:bg-[#dae5de]" : ""
1518
+ ),
1519
+ children: opt.label
1520
+ },
1521
+ opt.id
1522
+ )) })
1523
+ ]
1524
+ }
1525
+ ),
1526
+ name == "columns.0.id" ? /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsxs(Tooltip, { children: [
1527
+ /* @__PURE__ */ jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsx(Info, { className: "h-5 w-5", stroke: "white", fill: "#8B8B8B" }) }),
1528
+ /* @__PURE__ */ jsx(
1529
+ TooltipContent,
1530
+ {
1531
+ align: "start",
1532
+ alignOffset: 10,
1533
+ className: "bg-[#8B8B8B] rounded-none",
1534
+ arrowClassName: "!hidden",
1535
+ children: /* @__PURE__ */ jsx("p", { children: "Column cannot be removed." })
1536
+ }
1537
+ )
1538
+ ] }) }) : /* @__PURE__ */ jsx(
1539
+ CircleMinus,
1540
+ {
1541
+ onClick: onRemove,
1542
+ className: "h-5 w-5 cursor-pointer",
1543
+ stroke: "white",
1544
+ fill: "#C32A2C"
1508
1545
  }
1509
1546
  )
1510
- ] }) }) : /* @__PURE__ */ jsx(
1511
- CircleMinus,
1512
- {
1513
- onClick: onRemove,
1514
- className: "h-5 w-5 cursor-pointer",
1515
- stroke: "white",
1516
- fill: "#C32A2C"
1517
- }
1518
- )
1519
- ] }),
1520
- !isDragging && /* @__PURE__ */ jsx(FormMessage, { className: cn("pl-6 text-[#BB0B0E]") })
1521
- ] })
1547
+ ] }),
1548
+ !isDragging && /* @__PURE__ */ jsx(FormMessage, { className: cn("pl-6 text-[#BB0B0E]") })
1549
+ ] });
1550
+ }
1522
1551
  }
1523
1552
  ) });
1524
1553
  }
@@ -1672,8 +1701,7 @@ var GridSettingsModal = ({
1672
1701
  control,
1673
1702
  name: `columns.0.id`,
1674
1703
  isDragging,
1675
- availableColumns,
1676
- currentColumns: fields
1704
+ availableColumns
1677
1705
  },
1678
1706
  fields[0]?.fieldId
1679
1707
  ),
@@ -1692,16 +1720,15 @@ var GridSettingsModal = ({
1692
1720
  {
1693
1721
  items: fields?.slice(1).map((c) => c?.fieldId),
1694
1722
  strategy: verticalListSortingStrategy,
1695
- children: fields?.slice(1).map((col, index2) => /* @__PURE__ */ jsx(
1723
+ children: fields?.slice(1).map((col, index) => /* @__PURE__ */ jsx(
1696
1724
  SortableRow,
1697
1725
  {
1698
1726
  value: col?.fieldId,
1699
1727
  control,
1700
- name: `columns.${index2 + 1}.id`,
1728
+ name: `columns.${index + 1}.id`,
1701
1729
  isDragging,
1702
1730
  availableColumns,
1703
- currentColumns: fields,
1704
- onRemove: () => remove(index2 + 1)
1731
+ onRemove: () => remove(index + 1)
1705
1732
  },
1706
1733
  col?.fieldId
1707
1734
  ))
@@ -1770,7 +1797,7 @@ var InfoIcon = (props) => {
1770
1797
  }
1771
1798
  );
1772
1799
  };
1773
- var InfoIcon_default = React3__default.memo(InfoIcon);
1800
+ var InfoIcon_default = React4__default.memo(InfoIcon);
1774
1801
  function TooltipProvider2({
1775
1802
  delayDuration = 0,
1776
1803
  ...props
@@ -1942,7 +1969,7 @@ var Navbar = ({
1942
1969
  }
1943
1970
  );
1944
1971
  };
1945
- var navbar_default = React3__default.memo(Navbar);
1972
+ var navbar_default = React4__default.memo(Navbar);
1946
1973
  var ExpandCollapse = ({ title, children, portalId }) => {
1947
1974
  const [isOpen, setIsOpen] = useState(false);
1948
1975
  const Panel = /* @__PURE__ */ jsx(
@@ -2477,13 +2504,13 @@ function DatePicker({
2477
2504
  className,
2478
2505
  ...props
2479
2506
  }) {
2480
- const today = React3.useMemo(() => startOfDay(/* @__PURE__ */ new Date()), []);
2481
- const [displayed, setDisplayed] = React3.useState(
2507
+ const today = React4.useMemo(() => startOfDay(/* @__PURE__ */ new Date()), []);
2508
+ const [displayed, setDisplayed] = React4.useState(
2482
2509
  selectedDate ? new Date(selectedDate) : /* @__PURE__ */ new Date()
2483
2510
  );
2484
2511
  minDate = clampToDay(minDate);
2485
2512
  maxDate = clampToDay(maxDate);
2486
- const disabledSet = React3.useMemo(() => {
2513
+ const disabledSet = React4.useMemo(() => {
2487
2514
  const s2 = /* @__PURE__ */ new Set();
2488
2515
  disabledDates?.forEach((d) => s2.add(startOfDay(d).toISOString()));
2489
2516
  return s2;
@@ -2492,7 +2519,7 @@ function DatePicker({
2492
2519
  const displayMonth = displayed.getMonth();
2493
2520
  const monthLabel = callbacks?.monthLabel?.(displayYear, displayMonth) ?? new Intl.DateTimeFormat(void 0, { month: "short" }).format(displayed);
2494
2521
  const yearLabel = callbacks?.yearLabel?.(displayYear) ?? String(displayYear);
2495
- const weekdays = React3.useMemo(() => {
2522
+ const weekdays = React4.useMemo(() => {
2496
2523
  const labels = [];
2497
2524
  for (let i2 = 0; i2 < 7; i2++) {
2498
2525
  const idx = i2;
@@ -2501,7 +2528,7 @@ function DatePicker({
2501
2528
  }
2502
2529
  return labels;
2503
2530
  }, [callbacks]);
2504
- const grid = React3.useMemo(() => buildCalendarGrid(displayed, true), [displayed]);
2531
+ const grid = React4.useMemo(() => buildCalendarGrid(displayed, true), [displayed]);
2505
2532
  const isDateDisabled = (date) => {
2506
2533
  const d = startOfDay(date);
2507
2534
  if (minDate && d < minDate) return true;
@@ -2588,8 +2615,8 @@ var TagsInput = ({ value = [], onChange, onClear, error }) => {
2588
2615
  onChange([...value, trimmed]);
2589
2616
  setInputValue("");
2590
2617
  };
2591
- const removeTag = (index2) => {
2592
- const newTags = value.filter((_, i2) => i2 !== index2);
2618
+ const removeTag = (index) => {
2619
+ const newTags = value.filter((_, i2) => i2 !== index);
2593
2620
  onChange(newTags);
2594
2621
  };
2595
2622
  const handleKeyDown = (e2) => {
@@ -2928,8 +2955,8 @@ function buildLocalizeFn(args) {
2928
2955
  const width = options?.width ? String(options.width) : args.defaultWidth;
2929
2956
  valuesArray = args.values[width] || args.values[defaultWidth];
2930
2957
  }
2931
- const index2 = args.argumentCallback ? args.argumentCallback(value) : value;
2932
- return valuesArray[index2];
2958
+ const index = args.argumentCallback ? args.argumentCallback(value) : value;
2959
+ return valuesArray[index];
2933
2960
  };
2934
2961
  }
2935
2962
 
@@ -3249,7 +3276,7 @@ var match = {
3249
3276
  defaultMatchWidth: "wide",
3250
3277
  parsePatterns: parseQuarterPatterns,
3251
3278
  defaultParseWidth: "any",
3252
- valueCallback: (index2) => index2 + 1
3279
+ valueCallback: (index) => index + 1
3253
3280
  }),
3254
3281
  month: buildMatchFn({
3255
3282
  matchPatterns: matchMonthPatterns,
@@ -4384,15 +4411,15 @@ function Label4({ className, ...props }) {
4384
4411
  );
4385
4412
  }
4386
4413
  var Form2 = FormProvider;
4387
- var FormFieldContext3 = React3.createContext({});
4414
+ var FormFieldContext3 = React4.createContext({});
4388
4415
  var FormField3 = ({
4389
4416
  ...props
4390
4417
  }) => {
4391
4418
  return /* @__PURE__ */ jsx(FormFieldContext3.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsx(Controller, { ...props }) });
4392
4419
  };
4393
4420
  var useFormField3 = () => {
4394
- const fieldContext = React3.useContext(FormFieldContext3);
4395
- const itemContext = React3.useContext(FormItemContext3);
4421
+ const fieldContext = React4.useContext(FormFieldContext3);
4422
+ const itemContext = React4.useContext(FormItemContext3);
4396
4423
  const { getFieldState } = useFormContext();
4397
4424
  const formState = useFormState({ name: fieldContext.name });
4398
4425
  const fieldState = getFieldState(fieldContext.name, formState);
@@ -4409,9 +4436,9 @@ var useFormField3 = () => {
4409
4436
  ...fieldState
4410
4437
  };
4411
4438
  };
4412
- var FormItemContext3 = React3.createContext({});
4439
+ var FormItemContext3 = React4.createContext({});
4413
4440
  function FormItem3({ className, ...props }) {
4414
- const id = React3.useId();
4441
+ const id = React4.useId();
4415
4442
  return /* @__PURE__ */ jsx(FormItemContext3.Provider, { value: { id }, children: /* @__PURE__ */ jsx("div", { "data-slot": "form-item", className: cn2("grid gap-2", className), ...props }) });
4416
4443
  }
4417
4444
  function FormControl2({ ...props }) {
@@ -4443,7 +4470,7 @@ var AdvanceSearchRow = ({
4443
4470
  const { control } = form;
4444
4471
  const fieldSchema = fields.find((f) => f.name === row.fieldName);
4445
4472
  const fieldType = fieldSchema?.type ?? "text";
4446
- React3__default.useEffect(() => {
4473
+ React4__default.useEffect(() => {
4447
4474
  if (operators && operators.length > 0 && !operators.includes(row.operator)) {
4448
4475
  onChangeOperator(operators[0]);
4449
4476
  }
@@ -4454,8 +4481,8 @@ var AdvanceSearchRow = ({
4454
4481
  const isLookup = fieldType === "lookup";
4455
4482
  const isNumber = fieldType === "number";
4456
4483
  const isDate2 = fieldType === "date" || fieldType === "datetime";
4457
- const [openDateValue1, setOpenDateValue1] = React3__default.useState(false);
4458
- const [openDateValue2, setOpenDateValue2] = React3__default.useState(false);
4484
+ const [openDateValue1, setOpenDateValue1] = React4__default.useState(false);
4485
+ const [openDateValue2, setOpenDateValue2] = React4__default.useState(false);
4459
4486
  const toDateFromISO = (v) => {
4460
4487
  if (!v) return void 0;
4461
4488
  try {
@@ -5014,6 +5041,6 @@ function ConfirmDialog({ dialogData, setDialog, onClose }) {
5014
5041
  ] }) });
5015
5042
  }
5016
5043
 
5017
- 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 };
5044
+ 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, PreventPageLeave_default as PreventPageLeave, TextInput, booleanToSelectValue, buttonVariants, cn, compareAlphanumeric, getColumnIdFromTable, renderContentSlot, selectValueToBoolean, useFormField, useGridSettingsStore, useHover_default as useHover, useIntersectionObserver_default as useIntersectionObserver, useMediaQuery_default as useMediaQuery, usePreventPageLeave_default as usePreventPageLeave, usePreventPageLeaveStore_default as usePreventPageLeaveStore, useScreenSize_default as useScreenSize };
5018
5045
  //# sourceMappingURL=index.mjs.map
5019
5046
  //# sourceMappingURL=index.mjs.map