react-better-html 1.1.229 → 1.1.230

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
@@ -3188,6 +3188,18 @@ var decryptString = (text) => {
3188
3188
  });
3189
3189
  return decrypted.toString(CryptoJS.enc.Utf8);
3190
3190
  };
3191
+ function findClosestNumber(numbers, target) {
3192
+ let closest = numbers[0];
3193
+ let minDistance = Math.abs(target - numbers[0]);
3194
+ for (let index = 1; index < numbers.length; index++) {
3195
+ const distance = Math.abs(target - numbers[index]);
3196
+ if (distance < minDistance || distance === minDistance && numbers[index] < closest) {
3197
+ closest = numbers[index];
3198
+ minDistance = distance;
3199
+ }
3200
+ }
3201
+ return closest;
3202
+ }
3191
3203
 
3192
3204
  // src/utils/localStorage.ts
3193
3205
  function generateLocalStorage() {
@@ -4749,13 +4761,29 @@ InputFieldComponent.date = forwardRef11(function Date2({ minDate, maxDate, ...pr
4749
4761
  }
4750
4762
  );
4751
4763
  });
4752
- InputFieldComponent.dateTime = forwardRef11(function DateTime({ minDate, maxDate, defaultDateAfterTimePick, defaultTimeAfterDatePick = "00:00", ...props }, ref) {
4764
+ InputFieldComponent.dateTime = forwardRef11(function DateTime({
4765
+ hoursToRender,
4766
+ minutesToRender,
4767
+ minDate,
4768
+ maxDate,
4769
+ defaultDateAfterTimePick,
4770
+ defaultTimeAfterDatePick = "00:00",
4771
+ ...props
4772
+ }, ref) {
4753
4773
  const theme2 = useTheme19();
4754
4774
  const holderRef = useRef5(null);
4755
4775
  const selectedHourRef = useRef5(null);
4756
4776
  const selectedMinuteRef = useRef5(null);
4757
4777
  const isMobileIOS = isMobileDevice && getBrowser() === "safari";
4758
4778
  const { internalValue, setInternalValue, inputFieldProps, insideInputFieldComponentProps, isOpen } = useComponentInputFieldDateProps(props, holderRef, isMobileIOS);
4779
+ const readyHours = useMemo6(
4780
+ () => hoursToRender?.filter((hour) => hour >= 0 && hour <= 23) ?? hours,
4781
+ [hoursToRender]
4782
+ );
4783
+ const readyMinutes = useMemo6(
4784
+ () => minutesToRender?.filter((hour) => hour >= 0 && hour <= 59) ?? minutes,
4785
+ [minutesToRender]
4786
+ );
4759
4787
  const onChange = useCallback10(
4760
4788
  (date) => {
4761
4789
  const newValue = date ? `${date}T${internalValue?.toString().split("T")[1] ?? defaultTimeAfterDatePick}` : "";
@@ -4764,6 +4792,32 @@ InputFieldComponent.dateTime = forwardRef11(function DateTime({ minDate, maxDate
4764
4792
  },
4765
4793
  [internalValue, defaultTimeAfterDatePick, inputFieldProps.onChangeValue]
4766
4794
  );
4795
+ const onBlur = useCallback10(
4796
+ (event) => {
4797
+ if (hoursToRender || minutesToRender) {
4798
+ const value = event.target.value;
4799
+ const hours2 = parseInt(value.split(":")[0] || "0");
4800
+ const minutes2 = parseInt(value.split(":")[1] || "0");
4801
+ const readyHour = readyHours.includes(hours2) ? hours2 : findClosestNumber(readyHours, hours2);
4802
+ const readyMinute = readyMinutes.includes(minutes2) ? minutes2 : findClosestNumber(readyMinutes, minutes2);
4803
+ const newTime = `${readyHour.toString().padStart(2, "0")}:${readyMinute.toString().padStart(2, "0")}`;
4804
+ const today = defaultDateAfterTimePick ?? `${(/* @__PURE__ */ new Date()).getFullYear()}-${((/* @__PURE__ */ new Date()).getMonth() + 1).toString().padStart(2, "0")}-${(/* @__PURE__ */ new Date()).getDate().toString().padStart(2, "0")}`;
4805
+ const newValue = `${(internalValue.trim() || today)?.toString().split("T")[0]}T${newTime}`;
4806
+ inputFieldProps.onChangeValue?.(newValue);
4807
+ }
4808
+ inputFieldProps.onBlur?.(event);
4809
+ },
4810
+ [
4811
+ defaultDateAfterTimePick,
4812
+ internalValue,
4813
+ hoursToRender,
4814
+ minutesToRender,
4815
+ readyHours,
4816
+ readyMinutes,
4817
+ inputFieldProps.onChangeValue,
4818
+ inputFieldProps.onBlur
4819
+ ]
4820
+ );
4767
4821
  const onClickHour = useCallback10(
4768
4822
  (hour) => {
4769
4823
  const newTime = `${hour.toString().padStart(2, "0")}:${internalValue?.toString().split(":")[1] || "00"}`;
@@ -4832,7 +4886,7 @@ InputFieldComponent.dateTime = forwardRef11(function DateTime({ minDate, maxDate
4832
4886
  height: `calc(100% - ${16 + theme2.styles.gap / 2}px)`,
4833
4887
  overflowY: "auto",
4834
4888
  tabIndex: -1,
4835
- children: hours.map((hour) => {
4889
+ children: readyHours.map((hour) => {
4836
4890
  const isSelected = hour.toString() === valueHour;
4837
4891
  return /* @__PURE__ */ jsx17(
4838
4892
  Div_default.row,
@@ -4866,7 +4920,7 @@ InputFieldComponent.dateTime = forwardRef11(function DateTime({ minDate, maxDate
4866
4920
  height: `calc(100% - ${16 + theme2.styles.gap / 2}px)`,
4867
4921
  overflowY: "auto",
4868
4922
  tabIndex: -1,
4869
- children: minutes.map((minute) => {
4923
+ children: readyMinutes.map((minute) => {
4870
4924
  const isSelected = minute.toString() === valueMinute;
4871
4925
  return /* @__PURE__ */ jsx17(
4872
4926
  Div_default.row,
@@ -4899,17 +4953,56 @@ InputFieldComponent.dateTime = forwardRef11(function DateTime({ minDate, maxDate
4899
4953
  holderRef,
4900
4954
  ref,
4901
4955
  ...props,
4902
- ...inputFieldProps
4956
+ ...inputFieldProps,
4957
+ onBlur
4903
4958
  }
4904
4959
  );
4905
4960
  });
4906
- InputFieldComponent.time = forwardRef11(function Time({ ...props }, ref) {
4961
+ InputFieldComponent.time = forwardRef11(function Time({ hoursToRender, minutesToRender, minTime, maxTime, ...props }, ref) {
4907
4962
  const theme2 = useTheme19();
4908
4963
  const holderRef = useRef5(null);
4909
4964
  const selectedHourRef = useRef5(null);
4910
4965
  const selectedMinuteRef = useRef5(null);
4911
4966
  const isMobileIOS = isMobileDevice && getBrowser() === "safari";
4967
+ const minHours = minTime ? parseInt(minTime.split(":")[0]) : void 0;
4968
+ const minMinutes = minTime ? parseInt(minTime.split(":")[1]) : void 0;
4969
+ const maxHours = maxTime ? parseInt(maxTime.split(":")[0]) : void 0;
4970
+ const maxMinutes = maxTime ? parseInt(maxTime.split(":")[1]) : void 0;
4912
4971
  const { internalValue, setInternalValue, inputFieldProps, insideInputFieldComponentProps, isOpen } = useComponentInputFieldDateProps(props, holderRef, isMobileIOS);
4972
+ const readyHours = useMemo6(
4973
+ () => hoursToRender?.filter((hour) => hour >= 0 && hour <= 23) ?? hours,
4974
+ [hoursToRender]
4975
+ );
4976
+ const readyMinutes = useMemo6(
4977
+ () => minutesToRender?.filter((hour) => hour >= 0 && hour <= 59) ?? minutes,
4978
+ [minutesToRender]
4979
+ );
4980
+ const onBlur = useCallback10(
4981
+ (event) => {
4982
+ if (!!minHours || !!maxHours || !!minMinutes || !!maxMinutes || hoursToRender || minutesToRender) {
4983
+ const value = event.target.value;
4984
+ const hours2 = parseInt(value.split(":")[0] || "0");
4985
+ const minutes2 = parseInt(value.split(":")[1] || "0");
4986
+ const readyHour = readyHours.includes(hours2) ? hours2 : findClosestNumber(readyHours, hours2);
4987
+ const readyMinute = readyMinutes.includes(minutes2) ? minutes2 : findClosestNumber(readyMinutes, minutes2);
4988
+ const newValue = `${(minHours && readyHour < minHours ? minHours : maxHours && readyHour > maxHours ? maxHours : readyHour).toString().padStart(2, "0")}:${(minMinutes && readyMinute < minMinutes ? minMinutes : maxHours && readyMinute > maxHours ? maxHours : readyMinute).toString().padStart(2, "0")}`;
4989
+ inputFieldProps.onChangeValue?.(newValue);
4990
+ }
4991
+ inputFieldProps.onBlur?.(event);
4992
+ },
4993
+ [
4994
+ minHours,
4995
+ maxHours,
4996
+ minMinutes,
4997
+ maxMinutes,
4998
+ hoursToRender,
4999
+ minutesToRender,
5000
+ readyHours,
5001
+ readyMinutes,
5002
+ inputFieldProps.onChangeValue,
5003
+ inputFieldProps.onBlur
5004
+ ]
5005
+ );
4913
5006
  const onClickHour = useCallback10(
4914
5007
  (hour) => {
4915
5008
  const value = `${hour.toString().padStart(2, "0")}:${internalValue?.toString().split(":")[1] || "00"}`;
@@ -4962,8 +5055,9 @@ InputFieldComponent.time = forwardRef11(function Time({ ...props }, ref) {
4962
5055
  height: "100%",
4963
5056
  overflowY: "auto",
4964
5057
  tabIndex: -1,
4965
- children: hours.map((hour) => {
5058
+ children: readyHours.map((hour) => {
4966
5059
  const isSelected = hour.toString() === valueHour;
5060
+ const isDisabled = minHours && hour < minHours || maxHours && hour > maxHours;
4967
5061
  return /* @__PURE__ */ jsx17(
4968
5062
  Div_default.row,
4969
5063
  {
@@ -4972,10 +5066,11 @@ InputFieldComponent.time = forwardRef11(function Time({ ...props }, ref) {
4972
5066
  color: isSelected ? theme2.colors.base : theme2.colors.textPrimary,
4973
5067
  backgroundColor: isSelected ? theme2.colors.primary : theme2.colors.backgroundContent,
4974
5068
  filterHover: "brightness(0.9)",
4975
- cursor: "pointer",
5069
+ cursor: isDisabled ? "not-allowed" : "pointer",
4976
5070
  padding: `${theme2.styles.space / 2}px ${theme2.styles.space + theme2.styles.gap}px`,
5071
+ opacity: isDisabled ? 0.6 : void 0,
4977
5072
  value: hour,
4978
- onClickWithValue: onClickHour,
5073
+ onClickWithValue: !isDisabled ? onClickHour : void 0,
4979
5074
  ref: isSelected ? selectedHourRef : void 0,
4980
5075
  children: /* @__PURE__ */ jsx17(Text_default, { textAlign: "center", children: hour.toString().padStart(2, "0") })
4981
5076
  },
@@ -4992,8 +5087,9 @@ InputFieldComponent.time = forwardRef11(function Time({ ...props }, ref) {
4992
5087
  height: "100%",
4993
5088
  overflowY: "auto",
4994
5089
  tabIndex: -1,
4995
- children: minutes.map((minute) => {
5090
+ children: readyMinutes.map((minute) => {
4996
5091
  const isSelected = minute.toString() === valueMinute;
5092
+ const isDisabled = minMinutes && minute < minMinutes || maxMinutes && minute > maxMinutes;
4997
5093
  return /* @__PURE__ */ jsx17(
4998
5094
  Div_default.row,
4999
5095
  {
@@ -5002,10 +5098,11 @@ InputFieldComponent.time = forwardRef11(function Time({ ...props }, ref) {
5002
5098
  color: isSelected ? theme2.colors.base : theme2.colors.textPrimary,
5003
5099
  backgroundColor: isSelected ? theme2.colors.primary : theme2.colors.backgroundContent,
5004
5100
  filterHover: "brightness(0.9)",
5005
- cursor: "pointer",
5101
+ cursor: isDisabled ? "not-allowed" : "pointer",
5006
5102
  padding: `${theme2.styles.space / 2}px ${theme2.styles.space + theme2.styles.gap}px`,
5103
+ opacity: isDisabled ? 0.6 : void 0,
5007
5104
  value: minute,
5008
- onClickWithValue: onClickMinute,
5105
+ onClickWithValue: !isDisabled ? onClickMinute : void 0,
5009
5106
  ref: isSelected ? selectedMinuteRef : void 0,
5010
5107
  children: /* @__PURE__ */ jsx17(Text_default, { textAlign: "center", children: minute.toString().padStart(2, "0") })
5011
5108
  },
@@ -5021,7 +5118,8 @@ InputFieldComponent.time = forwardRef11(function Time({ ...props }, ref) {
5021
5118
  ref,
5022
5119
  ...props,
5023
5120
  ...inputFieldProps,
5024
- minWidth: buttonWidth * 2 + 2
5121
+ minWidth: buttonWidth * 2 + 2,
5122
+ onBlur
5025
5123
  }
5026
5124
  );
5027
5125
  });