@sustaina/shared-ui 1.23.1 → 1.25.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.js CHANGED
@@ -5,7 +5,6 @@ var clsx2 = require('clsx');
5
5
  var tailwindMerge = require('tailwind-merge');
6
6
  var jsxRuntime = require('react/jsx-runtime');
7
7
  var React4 = require('react');
8
- var dateFns = require('date-fns');
9
8
  var lucideReact = require('lucide-react');
10
9
  var reactDom = require('react-dom');
11
10
  var SelectPrimitive = require('@radix-ui/react-select');
@@ -13,6 +12,7 @@ var reactHookForm = require('react-hook-form');
13
12
  var reactSlot = require('@radix-ui/react-slot');
14
13
  var LabelPrimitive = require('@radix-ui/react-label');
15
14
  var classVarianceAuthority = require('class-variance-authority');
15
+ var dateFns = require('date-fns');
16
16
  var PopoverPrimitive = require('@radix-ui/react-popover');
17
17
  var CheckboxPrimitive = require('@radix-ui/react-checkbox');
18
18
  var CollapsiblePrimitive = require('@radix-ui/react-collapsible');
@@ -58,6 +58,7 @@ var selection = require('@lexical/selection');
58
58
  var SeparatorPrimitive = require('@radix-ui/react-separator');
59
59
  var SwitchPrimitive = require('@radix-ui/react-switch');
60
60
  var Cropper = require('react-easy-crop');
61
+ var reactNumberFormat = require('react-number-format');
61
62
 
62
63
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
63
64
 
@@ -3460,6 +3461,66 @@ function getBuilder(fieldType) {
3460
3461
  return new JSONBuilder();
3461
3462
  }
3462
3463
  }
3464
+ var FILTER_FIELD_MAP = {
3465
+ timezone: "timezoneId",
3466
+ decimalSeparator: "decimalSeparatorId",
3467
+ country: "countryId",
3468
+ currency: "currencyId"
3469
+ };
3470
+ function transformFilterKeys(obj, fieldMap = FILTER_FIELD_MAP) {
3471
+ if (Array.isArray(obj)) {
3472
+ return obj.map((item) => transformFilterKeys(item, fieldMap));
3473
+ }
3474
+ if (obj && typeof obj === "object") {
3475
+ const newObj = {};
3476
+ for (const key in obj) {
3477
+ const mappedKey = fieldMap[key] ?? key;
3478
+ newObj[mappedKey] = transformFilterKeys(obj[key], fieldMap);
3479
+ }
3480
+ return newObj;
3481
+ }
3482
+ return obj;
3483
+ }
3484
+ var sanitizeInput = (val) => {
3485
+ if (!val) return val;
3486
+ if (typeof val !== "string") return "__INVALID_TYPE__";
3487
+ if (val.includes("\n") || val.includes("\r") || /[\u2028\u2029]/u.test(val))
3488
+ return "__INVALID_NEWLINE__";
3489
+ if (/\\\\/.test(val)) return "__INVALID_ESCAPE__";
3490
+ if (/\\(n|t|r|b|f|u[0-9a-fA-F]{4})/.test(val)) return "__INVALID_ESCAPE__";
3491
+ if (/\p{Cf}/u.test(val)) return "__INVALID_UNICODE_WHITESPACE__";
3492
+ if (/[\u00A0\u1680\u180E\u202F\u205F\u3000]/u.test(val)) return "__INVALID_UNICODE_WHITESPACE__";
3493
+ const trimmed = val.trim();
3494
+ if (/^\{.*\}$/s.test(trimmed) || /^\[.*\]$/s.test(trimmed)) return "__INVALID_JSON_LITERAL__";
3495
+ if (/\\\{/.test(val) || /\\\}/.test(val)) return "__INVALID_JSON_ESCAPE__";
3496
+ if (/[%_*~^]/.test(trimmed)) return "__INVALID_WILDCARD__";
3497
+ if (/[%><={}\\[\]"']/u.test(trimmed)) return "__INVALID_CHAR__";
3498
+ if (/\p{Cc}/u.test(val)) return "__INVALID_CONTROL_CHAR__";
3499
+ return trimmed.replace(/\s+/g, " ");
3500
+ };
3501
+ var numericTypes = ["number", "integer", "decimal"];
3502
+ var dateTypes = ["date", "datemonth"];
3503
+ var validateByFieldType = (value, fieldType) => {
3504
+ if (!value) return { valid: true };
3505
+ if (numericTypes.includes(fieldType)) {
3506
+ if (!/^\d+(\.\d+)?$/.test(value)) {
3507
+ return { valid: false, message: "Please enter a valid number." };
3508
+ }
3509
+ }
3510
+ if (fieldType === "boolean") {
3511
+ if (!["true", "false"].includes(value.toLowerCase())) {
3512
+ return { valid: false, message: "Please enter a boolean value (true/false)." };
3513
+ }
3514
+ }
3515
+ if (dateTypes.includes(fieldType)) {
3516
+ const normalized = fieldType === "datemonth" ? `${value}-01` : value;
3517
+ const parsed = dateFns.parseISO(normalized);
3518
+ if (!dateFns.isValid(parsed)) {
3519
+ return { valid: false, message: "Invalid date format." };
3520
+ }
3521
+ }
3522
+ return { valid: true };
3523
+ };
3463
3524
  var AdvanceSearch = ({
3464
3525
  fields,
3465
3526
  portalId,
@@ -3467,7 +3528,8 @@ var AdvanceSearch = ({
3467
3528
  limitRows = 4,
3468
3529
  onSearch,
3469
3530
  onClear,
3470
- shortDateFormat
3531
+ shortDateFormat,
3532
+ filterFieldMap = FILTER_FIELD_MAP
3471
3533
  }) => {
3472
3534
  const fieldsData = React4.useMemo(() => {
3473
3535
  if (fields.length === 0) throw new Error("fields cannot be an empty array");
@@ -3519,72 +3581,75 @@ var AdvanceSearch = ({
3519
3581
  const onSubmit = React4.useCallback(() => {
3520
3582
  const operatorValidation = {};
3521
3583
  rows.forEach((r) => {
3522
- const availableOperators = operatorsForField(r.fieldName);
3523
- if (!availableOperators.length || !availableOperators.includes(r.operator)) {
3584
+ const ops = operatorsForField(r.fieldName);
3585
+ if (!ops.length || !ops.includes(r.operator))
3524
3586
  operatorValidation[r.id] = "Please select an operator.";
3525
- }
3526
3587
  });
3527
3588
  setOperatorErrors(operatorValidation);
3528
- if (Object.keys(operatorValidation).length > 0) {
3529
- return;
3530
- }
3589
+ if (Object.keys(operatorValidation).length > 0) return;
3531
3590
  const currentValues = getValues();
3532
- let hasRangeError = false;
3533
- const rawRows = rows.map((r) => {
3534
- const startFieldName = `value_${r.id}`;
3535
- const startValue = currentValues[startFieldName] ?? "";
3591
+ let hasError = false;
3592
+ const processedRows = rows.map((r) => {
3593
+ const startField = `value_${r.id}`;
3594
+ const endField = `value2_${r.id}`;
3595
+ let v1 = currentValues[startField] ?? "";
3596
+ let v2 = currentValues[endField] ?? "";
3597
+ const s1 = sanitizeInput(v1);
3598
+ if (s1?.startsWith("__INVALID")) {
3599
+ hasError = true;
3600
+ setError(startField, { type: "validate", message: "Invalid input." });
3601
+ return null;
3602
+ }
3603
+ v1 = s1 || "";
3604
+ const valid1 = validateByFieldType(v1, r.fieldType);
3605
+ if (!valid1.valid) {
3606
+ hasError = true;
3607
+ setError(startField, { type: "validate", message: valid1.message });
3608
+ return null;
3609
+ }
3536
3610
  if (r.operator === "between") {
3537
- const endFieldName = `value2_${r.id}`;
3538
- const endValue = currentValues[endFieldName] ?? "";
3539
- if (startValue && endValue) {
3540
- const startDate = parseRangeValue(startValue, r.fieldType);
3541
- const endDate = parseRangeValue(endValue, r.fieldType);
3542
- if (startDate && endDate && dateFns.isAfter(startDate, endDate)) {
3543
- hasRangeError = true;
3544
- setError(startFieldName, {
3545
- type: "validate",
3546
- message: "Start value must be before end value."
3547
- });
3548
- setError(endFieldName, {
3549
- type: "validate",
3550
- message: "End value must be after start value."
3551
- });
3552
- } else {
3553
- clearErrors([startFieldName, endFieldName]);
3611
+ const s2 = sanitizeInput(v2);
3612
+ if (s2?.startsWith("__INVALID")) {
3613
+ hasError = true;
3614
+ setError(endField, { type: "validate", message: "Invalid input." });
3615
+ return null;
3616
+ }
3617
+ v2 = s2 || "";
3618
+ const valid2 = validateByFieldType(v2, r.fieldType);
3619
+ if (!valid2.valid) {
3620
+ hasError = true;
3621
+ setError(endField, { type: "validate", message: valid2.message });
3622
+ return null;
3623
+ }
3624
+ if (v1 && v2 && ["date", "datemonth"].includes(r.fieldType)) {
3625
+ const d1 = parseRangeValue(v1, r.fieldType);
3626
+ const d2 = parseRangeValue(v2, r.fieldType);
3627
+ if (d1 && d2 && dateFns.isAfter(d1, d2)) {
3628
+ hasError = true;
3629
+ setError(startField, { type: "validate", message: "Start value must be before end value." });
3630
+ setError(endField, { type: "validate", message: "End value must be after start value." });
3631
+ return null;
3554
3632
  }
3555
3633
  }
3556
- return {
3557
- ...r,
3558
- value: startValue ?? "",
3559
- value2: endValue ?? ""
3560
- };
3634
+ return { ...r, value: v1, value2: v2 };
3561
3635
  }
3562
- return {
3563
- ...r,
3564
- value: startValue ?? ""
3565
- };
3636
+ return { ...r, value: v1 };
3566
3637
  });
3567
- if (hasRangeError) {
3568
- return;
3569
- }
3638
+ if (hasError) return;
3639
+ const cleanedRows = processedRows.filter(Boolean);
3570
3640
  const param = {
3571
- AND: rawRows.map((r) => {
3572
- const builder = getBuilder(r.fieldType);
3573
- return builder.build(r);
3574
- }).filter(Boolean)
3641
+ AND: cleanedRows.map((r) => getBuilder(r.fieldType).build(r)).filter(Boolean)
3575
3642
  };
3576
- if (onSearch) {
3577
- onSearch(param);
3578
- }
3643
+ if (onSearch) onSearch(transformFilterKeys(param, filterFieldMap));
3579
3644
  }, [
3580
- clearErrors,
3581
- getValues,
3582
- onSearch,
3645
+ rows,
3583
3646
  operatorsForField,
3647
+ getValues,
3584
3648
  parseRangeValue,
3585
- rows,
3586
3649
  setError,
3587
- setOperatorErrors
3650
+ setOperatorErrors,
3651
+ filterFieldMap,
3652
+ onSearch
3588
3653
  ]);
3589
3654
  return /* @__PURE__ */ jsxRuntime.jsx(
3590
3655
  ExpandCollapse_default,
@@ -3622,9 +3687,7 @@ var AdvanceSearch = ({
3622
3687
  unregister(`value_${row.id}`);
3623
3688
  unregister(`value2_${row.id}`);
3624
3689
  },
3625
- onClearValue: (which) => {
3626
- clearValue(row.id, which);
3627
- },
3690
+ onClearValue: (which) => clearValue(row.id, which),
3628
3691
  disableAdd: limitRows !== void 0 && rows.length >= limitRows
3629
3692
  },
3630
3693
  row.id
@@ -3649,7 +3712,7 @@ var AdvanceSearch = ({
3649
3712
  Button,
3650
3713
  {
3651
3714
  type: "submit",
3652
- className: "w-full bg-[#379a2a] text-white hover:bg-[#2f7c21] md:w-auto md:min-w-[120px]",
3715
+ className: "w-full bg-sus-green-2 text-white hover:bg-[#2f7c21] md:w-auto md:min-w-[120px]",
3653
3716
  "data-testid": "advsearch-btn-search",
3654
3717
  children: "Search"
3655
3718
  }
@@ -6586,8 +6649,8 @@ var GridSettingsModal = ({
6586
6649
  }
6587
6650
  }, [isOpen, currentColumns, form]);
6588
6651
  const addColumn = async () => {
6589
- const isValid5 = await trigger("columns");
6590
- if (isValid5) {
6652
+ const isValid6 = await trigger("columns");
6653
+ if (isValid6) {
6591
6654
  append({ id: "" });
6592
6655
  requestAnimationFrame(() => {
6593
6656
  const container = scrollRef.current;
@@ -9777,6 +9840,173 @@ var Truncated = ({ children, className, ellipsis = true, as = "p", style }) => {
9777
9840
  );
9778
9841
  };
9779
9842
  var truncated_default = Truncated;
9843
+ var InputPrimitive2 = React4__namespace.forwardRef(
9844
+ ({ className, type = "text", ...props }, ref) => {
9845
+ return /* @__PURE__ */ jsxRuntime.jsx(
9846
+ "input",
9847
+ {
9848
+ ref,
9849
+ type,
9850
+ className: cn(
9851
+ "placeholder:text-neutral-400 text-neutral-900 flex h-10 w-full min-w-0 items-center rounded-lg border bg-white px-4 text-sm transition-colors outline-none file:inline-flex file:h-7 file:rounded-md file:border-0 file:bg-transparent file:px-2 file:text-sm file:font-medium hover:border-neutral-500 focus-visible:border-neutral-900 focus-visible:ring-0 disabled:cursor-not-allowed disabled:bg-neutral-100 disabled:text-neutral-400 disabled:border-neutral-200 aria-invalid:border-destructive",
9852
+ className
9853
+ ),
9854
+ ...props
9855
+ }
9856
+ );
9857
+ }
9858
+ );
9859
+ InputPrimitive2.displayName = "InputPrimitive";
9860
+ var inputVariants2 = classVarianceAuthority.cva("", {
9861
+ variants: {
9862
+ controlSize: {
9863
+ sm: "h-9 rounded-md px-3 text-sm",
9864
+ md: "h-10 rounded-lg px-4 text-sm",
9865
+ lg: "h-12 rounded-xl px-5 text-base"
9866
+ },
9867
+ fullWidth: {
9868
+ true: "w-full",
9869
+ false: "w-auto"
9870
+ },
9871
+ appearance: {
9872
+ filled: "border-neutral-200 hover:border-neutral-500 focus-visible:border-neutral-900",
9873
+ unfilled: "bg-white border-neutral-300 hover:border-neutral-500 focus-visible:border-neutral-900"
9874
+ }
9875
+ },
9876
+ defaultVariants: {
9877
+ controlSize: "sm",
9878
+ fullWidth: true,
9879
+ appearance: "filled"
9880
+ }
9881
+ });
9882
+ var Input2 = React4__namespace.forwardRef(
9883
+ ({
9884
+ className,
9885
+ wrapperClassName,
9886
+ controlSize,
9887
+ fullWidth,
9888
+ appearance,
9889
+ addonPrefix,
9890
+ addonPrefixProps,
9891
+ prefixInteractive,
9892
+ addonSuffix,
9893
+ addonSuffixProps,
9894
+ suffixInteractive,
9895
+ invalid,
9896
+ loading,
9897
+ loadingIcon,
9898
+ validationMessage,
9899
+ validationIcon,
9900
+ validationMessageProps,
9901
+ onValueChange,
9902
+ type = "text",
9903
+ ...rest
9904
+ }, ref) => {
9905
+ const inputProps = rest;
9906
+ const hasPrefix = Boolean(addonPrefix);
9907
+ const hasSuffix = Boolean(addonSuffix) || loading;
9908
+ const isFullWidth = fullWidth ?? true;
9909
+ const {
9910
+ "aria-invalid": ariaInvalidProp,
9911
+ "aria-describedby": ariaDescribedByProp,
9912
+ onChange: onChangeProp
9913
+ } = rest;
9914
+ const ariaInvalid = invalid ?? ariaInvalidProp;
9915
+ const messageId = React4__namespace.useId();
9916
+ const handleChange = React4__namespace.useCallback(
9917
+ (event) => {
9918
+ onChangeProp?.(event);
9919
+ onValueChange?.(event.target.value);
9920
+ },
9921
+ [onChangeProp, onValueChange]
9922
+ );
9923
+ const resolvedAriaInvalid = typeof ariaInvalid === "string" ? ariaInvalid : ariaInvalid ? true : void 0;
9924
+ const describedBy = validationMessage ? [ariaDescribedByProp, messageId].filter(Boolean).join(" ") : ariaDescribedByProp;
9925
+ const controlWrapperClassName = cn(
9926
+ "relative inline-flex items-center",
9927
+ isFullWidth ? "w-full" : "w-fit",
9928
+ !validationMessage && wrapperClassName
9929
+ );
9930
+ const inputElement = /* @__PURE__ */ jsxRuntime.jsx(
9931
+ InputPrimitive2,
9932
+ {
9933
+ ref,
9934
+ type,
9935
+ "data-slot": "input",
9936
+ className: cn(
9937
+ inputVariants2({ controlSize, fullWidth: isFullWidth, appearance }),
9938
+ hasPrefix && "pl-10",
9939
+ hasSuffix && "pr-10",
9940
+ className
9941
+ ),
9942
+ "aria-invalid": resolvedAriaInvalid,
9943
+ "aria-describedby": describedBy || void 0,
9944
+ onChange: handleChange,
9945
+ ...inputProps
9946
+ }
9947
+ );
9948
+ if (!hasPrefix && !hasSuffix && !validationMessage) {
9949
+ return inputElement;
9950
+ }
9951
+ const { className: prefixClassName, ...prefixRest } = addonPrefixProps ?? {};
9952
+ const { className: suffixClassName, ...suffixRest } = addonSuffixProps ?? {};
9953
+ const { className: validationMessageClassName, ...validationMessageRest } = validationMessageProps ?? {};
9954
+ const suffixContent = /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
9955
+ addonSuffix,
9956
+ loading && (loadingIcon ?? /* @__PURE__ */ jsxRuntime.jsx(Spinner, { size: 16, variant: "muted" }))
9957
+ ] });
9958
+ const inputWithAffixes = /* @__PURE__ */ jsxRuntime.jsxs("div", { "data-slot": "input-wrapper", className: controlWrapperClassName, children: [
9959
+ hasPrefix && /* @__PURE__ */ jsxRuntime.jsx(
9960
+ "span",
9961
+ {
9962
+ ...prefixRest,
9963
+ className: cn(
9964
+ "absolute left-3 top-1/2 -translate-y-1/2 inline-flex items-center text-muted-foreground",
9965
+ !prefixInteractive && "pointer-events-none",
9966
+ prefixClassName
9967
+ ),
9968
+ children: addonPrefix
9969
+ }
9970
+ ),
9971
+ inputElement,
9972
+ hasSuffix && /* @__PURE__ */ jsxRuntime.jsx(
9973
+ "span",
9974
+ {
9975
+ ...suffixRest,
9976
+ className: cn(
9977
+ "absolute right-3 top-1/2 -translate-y-1/2 inline-flex items-center gap-2 text-muted-foreground",
9978
+ !suffixInteractive && "pointer-events-none",
9979
+ suffixClassName
9980
+ ),
9981
+ children: suffixContent
9982
+ }
9983
+ )
9984
+ ] });
9985
+ if (!validationMessage) {
9986
+ return inputWithAffixes;
9987
+ }
9988
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cn("flex flex-col gap-1", isFullWidth ? "w-full" : "w-fit", wrapperClassName), children: [
9989
+ inputWithAffixes,
9990
+ /* @__PURE__ */ jsxRuntime.jsxs(
9991
+ "p",
9992
+ {
9993
+ id: messageId,
9994
+ ...validationMessageRest,
9995
+ className: cn("flex items-center gap-2 text-sm text-destructive", validationMessageClassName),
9996
+ children: [
9997
+ validationIcon ?? /* @__PURE__ */ jsxRuntime.jsx(lucideReact.CircleX, { className: "size-4" }),
9998
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: validationMessage })
9999
+ ]
10000
+ }
10001
+ )
10002
+ ] });
10003
+ }
10004
+ );
10005
+ Input2.displayName = "Input";
10006
+ var InputNumber = ({ customInputProps, ...props }) => {
10007
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNumberFormat.NumericFormat, { customInput: Input2, ...props, ...customInputProps });
10008
+ };
10009
+ var InputNumber_default = InputNumber;
9780
10010
 
9781
10011
  exports.Accordion = Accordion;
9782
10012
  exports.AccordionContent = AccordionContent;
@@ -9819,6 +10049,7 @@ exports.GridSettingsModal = GridSettingsModal_default;
9819
10049
  exports.HeaderCell = HeaderCell_default;
9820
10050
  exports.Image = Image2;
9821
10051
  exports.Input = Input;
10052
+ exports.InputNumber = InputNumber_default;
9822
10053
  exports.Label = Label2;
9823
10054
  exports.List = List_default;
9824
10055
  exports.ListContainer = container_default;