@sustaina/shared-ui 1.61.0 → 1.62.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
@@ -5651,7 +5651,7 @@ function MonthCal({
5651
5651
  setMenuYear(year);
5652
5652
  }
5653
5653
  }
5654
- }, [selectedMonthDate, menuYear]);
5654
+ }, [selectedMonthDate]);
5655
5655
  React.useEffect(() => {
5656
5656
  if (typeof minYear === "number" && menuYear < minYear) {
5657
5657
  setMenuYear(minYear);
@@ -17125,60 +17125,11 @@ var Input2 = React.forwardRef(
17125
17125
  }
17126
17126
  );
17127
17127
  Input2.displayName = "Input";
17128
- function useFillDecimalOnBlur({
17129
- enabled,
17130
- decimalScale,
17131
- value,
17132
- defaultValue
17133
- }) {
17134
- const blurScale = enabled ? decimalScale ?? 2 : void 0;
17135
- const isUserEditingRef = React.useRef(false);
17136
- const [formattedValue, setFormattedValue] = React.useState(() => {
17137
- if (!enabled) return void 0;
17138
- const scale = decimalScale ?? 2;
17139
- const initial = parseToNumber(value) ?? parseToNumber(defaultValue);
17140
- if (initial !== void 0) return truncateToFixed(initial, scale);
17141
- return void 0;
17142
- });
17143
- React.useEffect(() => {
17144
- if (!enabled) return;
17145
- const parsed = parseToNumber(value);
17146
- if (parsed !== void 0) {
17147
- if (blurScale !== void 0 && !isUserEditingRef.current) {
17148
- setFormattedValue(truncateToFixed(parsed, blurScale));
17149
- }
17150
- } else if (value === null || value === "") {
17151
- setFormattedValue(void 0);
17152
- }
17153
- }, [value, enabled, blurScale]);
17154
- const onEdit = React.useCallback(
17155
- (values, sourceInfo) => {
17156
- if (!enabled || sourceInfo.source !== "event") return;
17157
- isUserEditingRef.current = true;
17158
- setFormattedValue(values.value || void 0);
17159
- },
17160
- [enabled]
17161
- );
17162
- const onBlur = React.useCallback(
17163
- (truncatedStr) => {
17164
- isUserEditingRef.current = false;
17165
- if (!enabled || blurScale === void 0) return;
17166
- setFormattedValue(truncatedStr);
17167
- },
17168
- [enabled, blurScale]
17169
- );
17170
- const resetEditing = React.useCallback(() => {
17171
- isUserEditingRef.current = false;
17172
- }, []);
17173
- return { formattedValue, blurScale, onEdit, onBlur, resetEditing, setFormattedValue };
17174
- }
17175
17128
  function useStepper({ value, step, min, max, disabled, onStep }) {
17176
- const [changed, setChanged] = React.useState(false);
17177
17129
  const changeValue = React.useCallback(
17178
17130
  (delta) => {
17179
17131
  const current = value ?? 0;
17180
17132
  const clamped = clamp(current + delta, min, max);
17181
- setChanged(true);
17182
17133
  onStep(clamped);
17183
17134
  },
17184
17135
  [value, max, min, onStep]
@@ -17187,7 +17138,7 @@ function useStepper({ value, step, min, max, disabled, onStep }) {
17187
17138
  const decrement = React.useCallback(() => changeValue(-step), [changeValue, step]);
17188
17139
  const isIncrementDisabled = disabled || max !== void 0 && (value ?? 0) >= max;
17189
17140
  const isDecrementDisabled = disabled || min !== void 0 && (value ?? 0) <= min;
17190
- return { changed, increment, decrement, isIncrementDisabled, isDecrementDisabled };
17141
+ return { increment, decrement, isIncrementDisabled, isDecrementDisabled };
17191
17142
  }
17192
17143
  var InputNumber = ({
17193
17144
  customInputProps,
@@ -17211,26 +17162,37 @@ var InputNumber = ({
17211
17162
  ...props
17212
17163
  }) => {
17213
17164
  const blurFormatEnabled = fillDecimalOnBlur || truncateDecimalOnBlur;
17214
- const [internalValue, setInternalValue] = React.useState(
17165
+ const blurScale = blurFormatEnabled ? decimalScaleProp ?? 2 : void 0;
17166
+ const [displayOverride, setDisplayOverride] = React.useState(() => {
17167
+ if (!blurFormatEnabled) return void 0;
17168
+ const scale = decimalScaleProp ?? 2;
17169
+ const initial = parseToNumber(value) ?? parseToNumber(defaultValue);
17170
+ if (initial !== void 0) return truncateToFixed(initial, scale);
17171
+ return void 0;
17172
+ });
17173
+ const [numericValue, setNumericValue] = React.useState(
17215
17174
  () => parseToNumber(value) ?? parseToNumber(defaultValue)
17216
17175
  );
17217
- const internalValueRef = React.useRef(internalValue);
17218
- const rawValueRef = React.useRef("");
17219
- const isBlurClampedRef = React.useRef(false);
17220
- React.useEffect(() => {
17221
- const parsed = parseToNumber(value);
17222
- if (parsed !== void 0) {
17223
- internalValueRef.current = parsed;
17224
- setInternalValue(parsed);
17225
- }
17226
- isBlurClampedRef.current = false;
17227
- }, [value]);
17228
- const autoFormat = useFillDecimalOnBlur({
17229
- enabled: blurFormatEnabled,
17230
- decimalScale: decimalScaleProp,
17231
- value,
17232
- defaultValue
17233
- });
17176
+ const isEditingRef = React.useRef(false);
17177
+ const rawRef = React.useRef("");
17178
+ const prevValueRef = React.useRef(value);
17179
+ if (value !== prevValueRef.current) {
17180
+ prevValueRef.current = value;
17181
+ if (value === "" || value === null) {
17182
+ if (displayOverride !== void 0) setDisplayOverride(void 0);
17183
+ if (numericValue !== void 0) setNumericValue(void 0);
17184
+ isEditingRef.current = false;
17185
+ } else if (!isEditingRef.current) {
17186
+ const num = parseToNumber(value);
17187
+ if (num !== numericValue) setNumericValue(num);
17188
+ if (blurFormatEnabled && num !== void 0) {
17189
+ const formatted = truncateToFixed(num, blurScale);
17190
+ if (formatted !== displayOverride) setDisplayOverride(formatted);
17191
+ } else {
17192
+ if (displayOverride !== void 0) setDisplayOverride(void 0);
17193
+ }
17194
+ }
17195
+ }
17234
17196
  const isAllowed = React.useMemo(() => {
17235
17197
  if (maxIntegerDigits === void 0 && !isAllowedProp) return void 0;
17236
17198
  return (values) => {
@@ -17243,80 +17205,73 @@ var InputNumber = ({
17243
17205
  return isAllowedProp ? isAllowedProp(values) : true;
17244
17206
  };
17245
17207
  }, [maxIntegerDigits, isAllowedProp]);
17246
- const notifyChange = React.useCallback(
17247
- (newValue, event) => {
17248
- internalValueRef.current = newValue;
17249
- setInternalValue(newValue);
17250
- onStepChange?.(newValue);
17251
- onValueChange?.(
17252
- { floatValue: newValue, formattedValue: String(newValue), value: String(newValue) },
17253
- createSourceInfo(event)
17254
- );
17255
- },
17256
- [onStepChange, onValueChange]
17257
- );
17258
17208
  const stepper = useStepper({
17259
- value: internalValue,
17209
+ value: numericValue,
17260
17210
  step,
17261
17211
  min,
17262
17212
  max,
17263
17213
  disabled,
17264
17214
  onStep: React.useCallback(
17265
17215
  (clamped) => {
17266
- setInternalValue(clamped);
17267
- if (autoFormat.blurScale !== void 0) {
17268
- autoFormat.setFormattedValue(truncateToFixed(clamped, autoFormat.blurScale));
17269
- }
17270
- notifyChange(clamped);
17216
+ setNumericValue(clamped);
17217
+ const formatted = blurScale !== void 0 ? truncateToFixed(clamped, blurScale) : String(clamped);
17218
+ setDisplayOverride(formatted);
17219
+ rawRef.current = String(clamped);
17220
+ onStepChange?.(clamped);
17221
+ onValueChange?.(
17222
+ { floatValue: clamped, formattedValue: formatted, value: String(clamped) },
17223
+ createSourceInfo()
17224
+ );
17271
17225
  },
17272
- [autoFormat, notifyChange]
17226
+ [blurScale, onStepChange, onValueChange]
17273
17227
  )
17274
17228
  });
17275
17229
  const handleValueChange = React.useCallback(
17276
17230
  (values, sourceInfo) => {
17277
- internalValueRef.current = values.floatValue;
17278
- rawValueRef.current = values.value;
17279
- setInternalValue(values.floatValue);
17280
17231
  if (sourceInfo.source === "event") {
17281
- isBlurClampedRef.current = false;
17232
+ isEditingRef.current = true;
17233
+ setDisplayOverride(values.value);
17282
17234
  }
17235
+ setNumericValue(values.floatValue);
17236
+ rawRef.current = values.value;
17283
17237
  onValueChange?.(values, sourceInfo);
17284
17238
  if (values.floatValue !== void 0) onStepChange?.(values.floatValue);
17285
- autoFormat.onEdit(values, sourceInfo);
17286
17239
  },
17287
- [onValueChange, onStepChange, autoFormat]
17240
+ [onValueChange, onStepChange]
17288
17241
  );
17289
17242
  const handleBlur = React.useCallback(
17290
17243
  (event) => {
17244
+ isEditingRef.current = false;
17291
17245
  onBlur?.(event);
17292
- const latestValue = internalValueRef.current;
17293
- if (latestValue === void 0) {
17294
- autoFormat.resetEditing();
17246
+ const currentNumeric = numericValue;
17247
+ if (currentNumeric === void 0) {
17248
+ setDisplayOverride("");
17295
17249
  return;
17296
17250
  }
17297
- const clamped = clamp(latestValue, min, max);
17298
- const wasClamped = clamped !== latestValue;
17251
+ const clamped = clamp(currentNumeric, min, max);
17252
+ const wasClamped = clamped !== currentNumeric;
17299
17253
  if (wasClamped) {
17300
- isBlurClampedRef.current = true;
17301
- internalValueRef.current = clamped;
17302
- rawValueRef.current = String(clamped);
17303
- setInternalValue(clamped);
17254
+ setNumericValue(clamped);
17255
+ rawRef.current = String(clamped);
17304
17256
  onValueChange?.(
17305
17257
  { floatValue: clamped, formattedValue: String(clamped), value: String(clamped) },
17306
17258
  createSourceInfo()
17307
17259
  );
17308
17260
  onStepChange?.(clamped);
17309
17261
  }
17310
- if (blurFormatEnabled && autoFormat.blurScale !== void 0) {
17311
- const rawStr = wasClamped ? String(clamped) : rawValueRef.current || String(clamped);
17312
- autoFormat.onBlur(truncateStringToFixed(rawStr, autoFormat.blurScale));
17262
+ if (blurFormatEnabled && blurScale !== void 0) {
17263
+ const rawStr = wasClamped ? String(clamped) : rawRef.current || String(clamped);
17264
+ setDisplayOverride(truncateStringToFixed(rawStr, blurScale));
17265
+ } else if (wasClamped) {
17266
+ setDisplayOverride(String(clamped));
17313
17267
  } else {
17314
- autoFormat.resetEditing();
17268
+ setDisplayOverride(void 0);
17315
17269
  }
17316
17270
  },
17317
- [onBlur, blurFormatEnabled, autoFormat, min, max, onValueChange, onStepChange]
17271
+ [onBlur, blurFormatEnabled, blurScale, numericValue, min, max, onValueChange, onStepChange]
17318
17272
  );
17319
- const effectiveValue = autoFormat.formattedValue !== void 0 ? autoFormat.formattedValue : isBlurClampedRef.current || stepper.changed || blurFormatEnabled ? internalValue : value;
17273
+ const isValueExplicitlyEmpty = value === "" || value === null;
17274
+ const effectiveValue = isValueExplicitlyEmpty ? "" : displayOverride !== void 0 ? displayOverride : value;
17320
17275
  const buttonClass = cn(
17321
17276
  "flex items-center justify-center h-3 w-5 transition-colors outline-none",
17322
17277
  "text-neutral-400 hover:text-neutral-600 active:text-neutral-900",
@@ -17332,7 +17287,7 @@ var InputNumber = ({
17332
17287
  onBlur: handleBlur,
17333
17288
  ...isAllowed && { isAllowed },
17334
17289
  ...truncateDecimalOnBlur ? {} : fillDecimalOnBlur ? { decimalScale: decimalScaleProp } : { decimalScale: decimalScaleProp, fixedDecimalScale: fixedDecimalScaleProp },
17335
- ...autoFormat.formattedValue !== void 0 && { valueIsNumericString: true },
17290
+ ...displayOverride !== void 0 && { valueIsNumericString: true },
17336
17291
  ...props,
17337
17292
  disabled,
17338
17293
  invalid,