@ceed/cds 1.5.5 → 1.5.6-next.2

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.
@@ -1,3 +1 @@
1
- export declare function useControlledState<T>(controlledValue: T | undefined, defaultValue: T, onChange?: (value: T) => void, options?: {
2
- disableStrict?: boolean;
3
- }): [T, (value: T | ((prev: T) => T)) => void];
1
+ export declare function useControlledState<T>(controlledValue: T | undefined, defaultValue: T, onChange?: (value: T) => void): [T, (value: T | ((prev: T) => T)) => void, boolean];
package/dist/index.cjs CHANGED
@@ -406,27 +406,28 @@ var IconButton_default = IconButton;
406
406
 
407
407
  // src/hooks/use-controlled-state/index.ts
408
408
  var import_react5 = require("react");
409
- function useControlledState(controlledValue, defaultValue, onChange, options) {
409
+ function useControlledState(controlledValue, defaultValue, onChange) {
410
410
  const { current: isControlled } = (0, import_react5.useRef)(controlledValue !== void 0);
411
- const [displayValue, setDisplayValue] = (0, import_react5.useState)(
411
+ const [internalValue, setInternalValue] = (0, import_react5.useState)(
412
412
  controlledValue ?? defaultValue
413
413
  );
414
+ const displayValue = isControlled ? controlledValue : internalValue;
414
415
  (0, import_react5.useEffect)(() => {
415
416
  if (isControlled) {
416
- setDisplayValue(controlledValue ?? defaultValue);
417
+ setInternalValue(controlledValue);
417
418
  }
418
- }, [controlledValue, defaultValue, isControlled]);
419
+ }, [isControlled, controlledValue]);
419
420
  const handleChange = (0, import_react5.useCallback)(
420
421
  (value) => {
421
422
  const newValue = typeof value === "function" ? value(displayValue) : value;
422
- if (options?.disableStrict || !isControlled) {
423
- setDisplayValue(newValue);
423
+ if (!isControlled) {
424
+ setInternalValue(newValue);
424
425
  }
425
426
  onChange?.(newValue);
426
427
  },
427
- [isControlled, onChange, displayValue, options]
428
+ [isControlled, onChange, displayValue]
428
429
  );
429
- return [displayValue, handleChange];
430
+ return [displayValue, handleChange, isControlled];
430
431
  }
431
432
 
432
433
  // src/components/Autocomplete/Autocomplete.tsx
@@ -2349,8 +2350,7 @@ var DatePicker = (0, import_react19.forwardRef)(
2349
2350
  (0, import_react19.useCallback)(
2350
2351
  (value2) => onChange?.({ target: { name: props.name, value: value2 } }),
2351
2352
  [props.name, onChange]
2352
- ),
2353
- { disableStrict: true }
2353
+ )
2354
2354
  );
2355
2355
  const [displayValue, setDisplayValue] = (0, import_react19.useState)(
2356
2356
  () => value ? formatValueString(parseDate(value, format), displayFormat) : ""
@@ -4127,8 +4127,7 @@ var DateRangePicker = (0, import_react26.forwardRef)(
4127
4127
  (0, import_react26.useCallback)(
4128
4128
  (value2) => onChange?.({ target: { name: props.name, value: value2 } }),
4129
4129
  [props.name, onChange]
4130
- ),
4131
- { disableStrict: true }
4130
+ )
4132
4131
  );
4133
4132
  const [anchorEl, setAnchorEl] = (0, import_react26.useState)(null);
4134
4133
  const open = Boolean(anchorEl);
@@ -4616,7 +4615,6 @@ var MonthPicker = (0, import_react33.forwardRef)(
4616
4615
  * NOTE: 현재 CalendarDate는 YYYY-MM-DD로 개발하고 있어 date를 포함하여 value를 관리한다.
4617
4616
  * @see https://ecubelabs.atlassian.net/wiki/spaces/SW/pages/1938784349/Date#CalendarDate
4618
4617
  * @see https://github.com/Ecube-Labs/hds/pull/145
4619
- *
4620
4618
  */
4621
4619
  format = "YYYY/MM/DD",
4622
4620
  displayFormat = "YYYY/MM",
@@ -4626,17 +4624,31 @@ var MonthPicker = (0, import_react33.forwardRef)(
4626
4624
  } = props;
4627
4625
  const innerRef = (0, import_react33.useRef)(null);
4628
4626
  const buttonRef = (0, import_react33.useRef)(null);
4629
- const [value, setValue] = useControlledState(
4627
+ const [value, setValue, isControlled] = useControlledState(
4630
4628
  props.value,
4631
4629
  props.defaultValue || "",
4632
4630
  (0, import_react33.useCallback)(
4633
4631
  (value2) => onChange?.({ target: { name: props.name, value: value2 } }),
4634
4632
  [props.name, onChange]
4635
- ),
4636
- { disableStrict: true }
4633
+ )
4634
+ );
4635
+ const getFormattedDisplayValue = (0, import_react33.useCallback)(
4636
+ (inputValue) => {
4637
+ if (!inputValue) return "";
4638
+ try {
4639
+ return formatValueString3(
4640
+ parseDate3(inputValue, format),
4641
+ displayFormat,
4642
+ locale
4643
+ );
4644
+ } catch (e) {
4645
+ return inputValue;
4646
+ }
4647
+ },
4648
+ [format, displayFormat, locale]
4637
4649
  );
4638
4650
  const [displayValue, setDisplayValue] = (0, import_react33.useState)(
4639
- () => value ? formatValueString3(parseDate3(value, format), displayFormat, locale) : ""
4651
+ () => getFormattedDisplayValue(value)
4640
4652
  );
4641
4653
  const [anchorEl, setAnchorEl] = (0, import_react33.useState)(null);
4642
4654
  const open = Boolean(anchorEl);
@@ -4649,26 +4661,17 @@ var MonthPicker = (0, import_react33.forwardRef)(
4649
4661
  innerRef
4650
4662
  ]);
4651
4663
  (0, import_react33.useEffect)(() => {
4652
- if (value === "") {
4653
- setDisplayValue("");
4654
- return;
4655
- }
4656
- const formattedValue = formatValueString3(
4657
- parseDate3(value, format),
4658
- displayFormat,
4659
- locale
4660
- );
4661
- setDisplayValue(formattedValue);
4662
- }, [displayFormat, format, value]);
4664
+ setDisplayValue(getFormattedDisplayValue(value));
4665
+ }, [value, getFormattedDisplayValue]);
4663
4666
  const handleChange = (0, import_react33.useCallback)(
4664
4667
  (event) => {
4665
- const value2 = event.target.value;
4666
- setDisplayValue(
4667
- value2 ? formatValueString3(parseDate3(value2, format), displayFormat, locale) : value2
4668
- );
4669
- setValue(value2);
4668
+ const newValue = event.target.value;
4669
+ setValue(newValue);
4670
+ if (!isControlled) {
4671
+ setDisplayValue(getFormattedDisplayValue(newValue));
4672
+ }
4670
4673
  },
4671
- [displayFormat, format, setValue, locale]
4674
+ [setValue, getFormattedDisplayValue, isControlled]
4672
4675
  );
4673
4676
  const handleCalendarToggle = (0, import_react33.useCallback)(
4674
4677
  (event) => {
@@ -4920,8 +4923,7 @@ var MonthRangePicker = (0, import_react34.forwardRef)(
4920
4923
  (0, import_react34.useCallback)(
4921
4924
  (value2) => onChange?.({ target: { name: props.name, value: value2 } }),
4922
4925
  [props.name, onChange]
4923
- ),
4924
- { disableStrict: true }
4926
+ )
4925
4927
  );
4926
4928
  const [anchorEl, setAnchorEl] = (0, import_react34.useState)(null);
4927
4929
  const open = Boolean(anchorEl);
package/dist/index.js CHANGED
@@ -325,27 +325,28 @@ var IconButton_default = IconButton;
325
325
 
326
326
  // src/hooks/use-controlled-state/index.ts
327
327
  import { useState, useCallback, useEffect, useRef } from "react";
328
- function useControlledState(controlledValue, defaultValue, onChange, options) {
328
+ function useControlledState(controlledValue, defaultValue, onChange) {
329
329
  const { current: isControlled } = useRef(controlledValue !== void 0);
330
- const [displayValue, setDisplayValue] = useState(
330
+ const [internalValue, setInternalValue] = useState(
331
331
  controlledValue ?? defaultValue
332
332
  );
333
+ const displayValue = isControlled ? controlledValue : internalValue;
333
334
  useEffect(() => {
334
335
  if (isControlled) {
335
- setDisplayValue(controlledValue ?? defaultValue);
336
+ setInternalValue(controlledValue);
336
337
  }
337
- }, [controlledValue, defaultValue, isControlled]);
338
+ }, [isControlled, controlledValue]);
338
339
  const handleChange = useCallback(
339
340
  (value) => {
340
341
  const newValue = typeof value === "function" ? value(displayValue) : value;
341
- if (options?.disableStrict || !isControlled) {
342
- setDisplayValue(newValue);
342
+ if (!isControlled) {
343
+ setInternalValue(newValue);
343
344
  }
344
345
  onChange?.(newValue);
345
346
  },
346
- [isControlled, onChange, displayValue, options]
347
+ [isControlled, onChange, displayValue]
347
348
  );
348
- return [displayValue, handleChange];
349
+ return [displayValue, handleChange, isControlled];
349
350
  }
350
351
 
351
352
  // src/components/Autocomplete/Autocomplete.tsx
@@ -2311,8 +2312,7 @@ var DatePicker = forwardRef6(
2311
2312
  useCallback7(
2312
2313
  (value2) => onChange?.({ target: { name: props.name, value: value2 } }),
2313
2314
  [props.name, onChange]
2314
- ),
2315
- { disableStrict: true }
2315
+ )
2316
2316
  );
2317
2317
  const [displayValue, setDisplayValue] = useState5(
2318
2318
  () => value ? formatValueString(parseDate(value, format), displayFormat) : ""
@@ -4103,8 +4103,7 @@ var DateRangePicker = forwardRef8(
4103
4103
  useCallback10(
4104
4104
  (value2) => onChange?.({ target: { name: props.name, value: value2 } }),
4105
4105
  [props.name, onChange]
4106
- ),
4107
- { disableStrict: true }
4106
+ )
4108
4107
  );
4109
4108
  const [anchorEl, setAnchorEl] = useState7(null);
4110
4109
  const open = Boolean(anchorEl);
@@ -4612,7 +4611,6 @@ var MonthPicker = forwardRef9(
4612
4611
  * NOTE: 현재 CalendarDate는 YYYY-MM-DD로 개발하고 있어 date를 포함하여 value를 관리한다.
4613
4612
  * @see https://ecubelabs.atlassian.net/wiki/spaces/SW/pages/1938784349/Date#CalendarDate
4614
4613
  * @see https://github.com/Ecube-Labs/hds/pull/145
4615
- *
4616
4614
  */
4617
4615
  format = "YYYY/MM/DD",
4618
4616
  displayFormat = "YYYY/MM",
@@ -4622,17 +4620,31 @@ var MonthPicker = forwardRef9(
4622
4620
  } = props;
4623
4621
  const innerRef = useRef6(null);
4624
4622
  const buttonRef = useRef6(null);
4625
- const [value, setValue] = useControlledState(
4623
+ const [value, setValue, isControlled] = useControlledState(
4626
4624
  props.value,
4627
4625
  props.defaultValue || "",
4628
4626
  useCallback11(
4629
4627
  (value2) => onChange?.({ target: { name: props.name, value: value2 } }),
4630
4628
  [props.name, onChange]
4631
- ),
4632
- { disableStrict: true }
4629
+ )
4630
+ );
4631
+ const getFormattedDisplayValue = useCallback11(
4632
+ (inputValue) => {
4633
+ if (!inputValue) return "";
4634
+ try {
4635
+ return formatValueString3(
4636
+ parseDate3(inputValue, format),
4637
+ displayFormat,
4638
+ locale
4639
+ );
4640
+ } catch (e) {
4641
+ return inputValue;
4642
+ }
4643
+ },
4644
+ [format, displayFormat, locale]
4633
4645
  );
4634
4646
  const [displayValue, setDisplayValue] = useState9(
4635
- () => value ? formatValueString3(parseDate3(value, format), displayFormat, locale) : ""
4647
+ () => getFormattedDisplayValue(value)
4636
4648
  );
4637
4649
  const [anchorEl, setAnchorEl] = useState9(null);
4638
4650
  const open = Boolean(anchorEl);
@@ -4645,26 +4657,17 @@ var MonthPicker = forwardRef9(
4645
4657
  innerRef
4646
4658
  ]);
4647
4659
  useEffect8(() => {
4648
- if (value === "") {
4649
- setDisplayValue("");
4650
- return;
4651
- }
4652
- const formattedValue = formatValueString3(
4653
- parseDate3(value, format),
4654
- displayFormat,
4655
- locale
4656
- );
4657
- setDisplayValue(formattedValue);
4658
- }, [displayFormat, format, value]);
4660
+ setDisplayValue(getFormattedDisplayValue(value));
4661
+ }, [value, getFormattedDisplayValue]);
4659
4662
  const handleChange = useCallback11(
4660
4663
  (event) => {
4661
- const value2 = event.target.value;
4662
- setDisplayValue(
4663
- value2 ? formatValueString3(parseDate3(value2, format), displayFormat, locale) : value2
4664
- );
4665
- setValue(value2);
4664
+ const newValue = event.target.value;
4665
+ setValue(newValue);
4666
+ if (!isControlled) {
4667
+ setDisplayValue(getFormattedDisplayValue(newValue));
4668
+ }
4666
4669
  },
4667
- [displayFormat, format, setValue, locale]
4670
+ [setValue, getFormattedDisplayValue, isControlled]
4668
4671
  );
4669
4672
  const handleCalendarToggle = useCallback11(
4670
4673
  (event) => {
@@ -4924,8 +4927,7 @@ var MonthRangePicker = forwardRef10(
4924
4927
  useCallback12(
4925
4928
  (value2) => onChange?.({ target: { name: props.name, value: value2 } }),
4926
4929
  [props.name, onChange]
4927
- ),
4928
- { disableStrict: true }
4930
+ )
4929
4931
  );
4930
4932
  const [anchorEl, setAnchorEl] = useState10(null);
4931
4933
  const open = Boolean(anchorEl);