@farmzone/fz-react-ui 1.0.4 → 1.0.6

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.cjs CHANGED
@@ -5019,7 +5019,7 @@ function Table(props) {
5019
5019
  id: `${col.key}-body`,
5020
5020
  "data-row-cell": "true",
5021
5021
  "data-col-key": col.key,
5022
- className: `${isCheckbox ? "p-0" : "px-2 py-4"} whitespace-nowrap text-sm text-gray-900 border-r border-b border-gray-200 last:border-r-0 bg-inherit group-hover:bg-gray-100 transition-all`,
5022
+ className: `${isCheckbox ? "p-0" : "px-2 py-3"} whitespace-nowrap text-sm text-gray-900 border-r border-b border-gray-200 last:border-r-0 bg-inherit group-hover:bg-gray-100 transition-all`,
5023
5023
  style: {
5024
5024
  width,
5025
5025
  minWidth: minWidth || width,
@@ -6147,14 +6147,53 @@ function resolveValueColSpan(colspan) {
6147
6147
  const span = colspan ?? 10;
6148
6148
  return COL_SPAN_CLASS[span] ?? COL_SPAN_CLASS[10];
6149
6149
  }
6150
+ var THREE_DIGIT_AREA_CODES = /* @__PURE__ */ new Set([
6151
+ "031",
6152
+ "032",
6153
+ "033",
6154
+ "041",
6155
+ "042",
6156
+ "043",
6157
+ "051",
6158
+ "052",
6159
+ "053",
6160
+ "054",
6161
+ "055",
6162
+ "061",
6163
+ "062",
6164
+ "063"
6165
+ ]);
6150
6166
  function formatPhoneNumber(value) {
6151
- const numbers = value.replace(/\D/g, "");
6152
- if (numbers.length <= 3) return numbers;
6153
- if (numbers.length <= 7) return `${numbers.slice(0, 3)}-${numbers.slice(3)}`;
6154
- if (numbers.length <= 11) {
6155
- return `${numbers.slice(0, 3)}-${numbers.slice(3, 7)}-${numbers.slice(7)}`;
6167
+ let digits = value.replace(/\D/g, "");
6168
+ if (digits.startsWith("02")) {
6169
+ if (digits.length > 2 && digits.length <= 6) {
6170
+ return `${digits.slice(0, 2)}-${digits.slice(2)}`;
6171
+ }
6172
+ if (digits.length > 6) {
6173
+ return `${digits.slice(0, 2)}-${digits.slice(2, 6)}-${digits.slice(6)}`;
6174
+ }
6175
+ } else if (THREE_DIGIT_AREA_CODES.has(digits.slice(0, 3))) {
6176
+ if (digits.length > 3 && digits.length <= 6) {
6177
+ return `${digits.slice(0, 3)}-${digits.slice(3)}`;
6178
+ }
6179
+ if (digits.length > 6) {
6180
+ return `${digits.slice(0, 3)}-${digits.slice(3, 6)}-${digits.slice(6)}`;
6181
+ }
6182
+ } else {
6183
+ if (digits.length > 3 && digits.length <= 7) {
6184
+ return `${digits.slice(0, 3)}-${digits.slice(3)}`;
6185
+ }
6186
+ if (digits.length > 7) {
6187
+ return `${digits.slice(0, 3)}-${digits.slice(3, 7)}-${digits.slice(7)}`;
6188
+ }
6156
6189
  }
6157
- return `${numbers.slice(0, 3)}-${numbers.slice(3, 7)}-${numbers.slice(7, 11)}`;
6190
+ return digits;
6191
+ }
6192
+ function resolvePhoneMaxLength(formattedValue) {
6193
+ if (formattedValue.startsWith("02") || THREE_DIGIT_AREA_CODES.has(formattedValue.slice(0, 3))) {
6194
+ return 12;
6195
+ }
6196
+ return 13;
6158
6197
  }
6159
6198
  function formatNumberWithCommas(value) {
6160
6199
  const numStr = String(value).replace(/,/g, "");
@@ -6978,11 +7017,13 @@ function PhoneField(props) {
6978
7017
  hasError = false
6979
7018
  } = props;
6980
7019
  const { placeholder = "010-0000-0000", disabled = false, readOnly = false, className = "" } = config;
7020
+ const phoneValue = String(value ?? "");
7021
+ const phoneMaxLength = resolvePhoneMaxLength(phoneValue);
6981
7022
  const handleChange = (e) => {
6982
7023
  onChange(formatPhoneNumber(e.target.value));
6983
7024
  };
6984
7025
  if (readOnly) {
6985
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: fieldControlWrapClass(className), children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: FIELD_READONLY_TEXT_CLASS, children: String(value ?? "") || "\u2014" }) });
7026
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: fieldControlWrapClass(className), children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: FIELD_READONLY_TEXT_CLASS, children: phoneValue || "\u2014" }) });
6986
7027
  }
6987
7028
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: fieldControlWrapClass(className), children: /* @__PURE__ */ jsxRuntime.jsx(
6988
7029
  Input2,
@@ -6990,12 +7031,12 @@ function PhoneField(props) {
6990
7031
  name,
6991
7032
  ref,
6992
7033
  onBlur,
6993
- value: String(value ?? ""),
7034
+ value: phoneValue,
6994
7035
  type: "text",
6995
7036
  placeholder,
6996
7037
  disabled,
6997
7038
  readOnly,
6998
- maxLength: 13,
7039
+ maxLength: phoneMaxLength,
6999
7040
  status: "default",
7000
7041
  className: formControlClass(hasError),
7001
7042
  bare: true,
@@ -7636,40 +7677,35 @@ function PageFilter(props) {
7636
7677
  values,
7637
7678
  primaryColor
7638
7679
  } = props;
7639
- const [localValues, setLocalValues] = React6.useState(values);
7640
7680
  const initialValuesRef = React6.useRef(values);
7641
- const updateLocal = (updates) => {
7642
- setLocalValues((prev) => ({ ...prev, ...updates }));
7643
- };
7644
7681
  const handleInputChange = (key) => (e) => {
7645
- updateLocal({ [key]: e.target.value });
7682
+ onChange({ [key]: e.target.value });
7646
7683
  };
7647
7684
  const handleSelectChange = (key) => (value) => {
7648
- updateLocal({ [key]: value });
7685
+ onChange({ [key]: value });
7649
7686
  };
7650
7687
  const handleRadioChange = (key) => (value) => {
7651
- updateLocal({ [key]: value });
7688
+ onChange({ [key]: value });
7652
7689
  };
7653
7690
  const handleDateChange = (key) => (date) => {
7654
- updateLocal({ [key]: date });
7691
+ onChange({ [key]: date });
7655
7692
  };
7656
7693
  const handleRangeChangeCurried = (startKey, endKey) => (start, end) => {
7657
- updateLocal({ [startKey]: start, [endKey]: end });
7694
+ onChange({ [startKey]: start, [endKey]: end });
7658
7695
  };
7659
7696
  const handleCheckboxChange = (key) => (checked) => {
7660
- updateLocal({ [key]: checked ? "true" : "false" });
7697
+ onChange({ [key]: checked ? "true" : "false" });
7661
7698
  };
7662
7699
  const handleSubmit = () => {
7663
- onChange(localValues);
7664
7700
  onSubmit();
7665
7701
  };
7666
7702
  const handleReset = () => {
7667
- setLocalValues(initialValuesRef.current);
7703
+ onChange(initialValuesRef.current);
7668
7704
  onReset?.();
7669
7705
  };
7670
7706
  const renderFilterOption = (option, gap, index) => {
7671
7707
  const optionKey = option.key;
7672
- const currentValue = String(localValues[optionKey] ?? "");
7708
+ const currentValue = String(values[optionKey] ?? "");
7673
7709
  const rowGap2 = index !== 0 ? option.label ? gap : 5 : 0;
7674
7710
  switch (option.type) {
7675
7711
  case "input":
@@ -7744,10 +7780,7 @@ function PageFilter(props) {
7744
7780
  return /* @__PURE__ */ jsxRuntime.jsx(
7745
7781
  "div",
7746
7782
  {
7747
- className: cn(
7748
- "flex flex-col gap-3 bg-white py-3 px-5 border-t-main border-t-1 shadow-panel",
7749
- containerClassName
7750
- ),
7783
+ className: cn("flex flex-col gap-3 bg-white py-3 px-5 border-t-main border-t-1 shadow-panel", containerClassName),
7751
7784
  style: primaryColor ? { borderTopColor: primaryColor } : void 0,
7752
7785
  children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "relative flex flex-col", style: { gap: `${colGap}px` }, children: rows.map((row, ix) => {
7753
7786
  const isLastRow = rows.length - 1 === ix;
@@ -9841,6 +9874,144 @@ function useScrollToTop(containerId) {
9841
9874
  return scrollToTop;
9842
9875
  }
9843
9876
 
9877
+ // src/hooks/useParams/config/utils.ts
9878
+ function paramsToSearchParams(params) {
9879
+ const searchParams = new URLSearchParams();
9880
+ Object.entries(params).forEach(([key, value]) => {
9881
+ if (value === null || value === void 0) {
9882
+ return;
9883
+ }
9884
+ if (Array.isArray(value)) {
9885
+ value.forEach((item) => searchParams.append(key, String(item)));
9886
+ } else if (value !== "") {
9887
+ searchParams.set(key, String(value));
9888
+ }
9889
+ });
9890
+ return searchParams;
9891
+ }
9892
+ function useParams(initialParams) {
9893
+ if (!initialParams) {
9894
+ throw new Error("initialParams is required");
9895
+ }
9896
+ const [params, setParams] = React6.useState(initialParams);
9897
+ const [localParams, setLocalParams] = React6.useState(initialParams);
9898
+ const [searchParams, setSearchParams] = reactRouter.useSearchParams();
9899
+ const isInitializedRef = React6.useRef(false);
9900
+ const getInitialState = React6.useCallback(() => {
9901
+ const result = { ...initialParams };
9902
+ Object.keys(initialParams).forEach((key) => {
9903
+ const initialValue = initialParams[key];
9904
+ if (Array.isArray(initialValue)) {
9905
+ const urlValue = searchParams.getAll(key);
9906
+ if (urlValue.length > 0) {
9907
+ result[key] = urlValue;
9908
+ }
9909
+ } else {
9910
+ const urlValue = searchParams.get(key);
9911
+ if (urlValue !== null) {
9912
+ if (urlValue === "null") {
9913
+ result[key] = null;
9914
+ } else if (urlValue === "undefined") {
9915
+ result[key] = void 0;
9916
+ } else {
9917
+ result[key] = urlValue;
9918
+ }
9919
+ }
9920
+ }
9921
+ });
9922
+ return result;
9923
+ }, [initialParams, searchParams]);
9924
+ React6.useEffect(() => {
9925
+ if (!isInitializedRef.current) {
9926
+ const initialState = getInitialState();
9927
+ setLocalParams(initialState);
9928
+ setParams(initialState);
9929
+ if (!searchParams.toString()) {
9930
+ const newSearchParams = paramsToSearchParams(initialParams);
9931
+ setSearchParams(newSearchParams, { replace: true });
9932
+ }
9933
+ isInitializedRef.current = true;
9934
+ }
9935
+ }, [getInitialState, initialParams, searchParams, setSearchParams]);
9936
+ const updateLocalParam = React6.useCallback(
9937
+ (key, value) => {
9938
+ setLocalParams((prev) => ({
9939
+ ...prev,
9940
+ [key]: value
9941
+ }));
9942
+ },
9943
+ [setLocalParams]
9944
+ );
9945
+ const updateParams = React6.useCallback(
9946
+ (updatedParams) => {
9947
+ const newParams = {
9948
+ ...params,
9949
+ ...updatedParams
9950
+ };
9951
+ setParams(newParams);
9952
+ setLocalParams(newParams);
9953
+ setSearchParams(paramsToSearchParams(newParams));
9954
+ },
9955
+ [params, setSearchParams]
9956
+ );
9957
+ const updateParam = React6.useCallback(
9958
+ (key, value) => {
9959
+ const newParams = {
9960
+ ...params,
9961
+ [key]: value
9962
+ };
9963
+ setParams(newParams);
9964
+ setLocalParams(newParams);
9965
+ setSearchParams(paramsToSearchParams(newParams));
9966
+ },
9967
+ [params, setSearchParams]
9968
+ );
9969
+ const update = React6.useCallback(() => {
9970
+ const paramsWithResetPage = {
9971
+ ...localParams,
9972
+ page: 0
9973
+ };
9974
+ const newSearchParams = paramsToSearchParams(paramsWithResetPage);
9975
+ setSearchParams(newSearchParams);
9976
+ setParams(paramsWithResetPage);
9977
+ setLocalParams(paramsWithResetPage);
9978
+ }, [localParams, setSearchParams]);
9979
+ const reset = React6.useCallback(() => {
9980
+ setLocalParams(initialParams);
9981
+ setParams(initialParams);
9982
+ const newSearchParams = paramsToSearchParams(initialParams);
9983
+ setSearchParams(newSearchParams);
9984
+ }, [initialParams, setSearchParams]);
9985
+ const handleSortChange = React6.useCallback(
9986
+ (sortKey, sortOrder) => {
9987
+ const newParams = {
9988
+ ...params,
9989
+ sortBy: sortKey,
9990
+ sortOrder
9991
+ };
9992
+ updateParams(newParams);
9993
+ },
9994
+ [updateParams, params]
9995
+ );
9996
+ const sortOption = React6.useMemo(() => {
9997
+ if (!params.sortBy || !params.sortOrder) return void 0;
9998
+ return {
9999
+ sortKey: params.sortBy,
10000
+ sortOrder: params.sortOrder
10001
+ };
10002
+ }, [params.sortBy, params.sortOrder]);
10003
+ return {
10004
+ localParams,
10005
+ params,
10006
+ updateParam,
10007
+ updateLocalParam,
10008
+ update,
10009
+ reset,
10010
+ handleSortChange,
10011
+ sortOption
10012
+ };
10013
+ }
10014
+
9844
10015
  exports.ArrowIcon = ArrowIcon;
9845
10016
  exports.Badge = Badge;
9846
10017
  exports.BaseUploader = BaseUploader;
@@ -9931,6 +10102,7 @@ exports.useDetailController = useDetailController;
9931
10102
  exports.useFilePreviewViewer = useFilePreviewViewer;
9932
10103
  exports.useModal = useModal;
9933
10104
  exports.useMultiTabStore = useMultiTabStore;
10105
+ exports.useParams = useParams;
9934
10106
  exports.useScrollToTop = useScrollToTop;
9935
10107
  exports.useStableImageSrc = useStableImageSrc;
9936
10108
  exports.useToast = useToast;