@northlight/ui 2.36.12 → 2.38.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.
@@ -7742,7 +7742,9 @@
7742
7742
  maxValue,
7743
7743
  firstDayOfWeek,
7744
7744
  onSave,
7745
- buttonLabel = "Save"
7745
+ buttonLabel = "Save",
7746
+ clearButtonLabel = "Clear",
7747
+ onCancel
7746
7748
  } = props;
7747
7749
  const { locale } = i18n.useLocale();
7748
7750
  const ref = React.useRef(null);
@@ -7773,6 +7775,14 @@
7773
7775
  locale,
7774
7776
  createCalendar: () => new date.GregorianCalendar()
7775
7777
  });
7778
+ const handleOnReset = () => {
7779
+ if (onCancel) {
7780
+ onCancel();
7781
+ } else {
7782
+ resetDate();
7783
+ }
7784
+ handleClose();
7785
+ };
7776
7786
  const focusDateRange = (dateRange) => {
7777
7787
  if (dateRange && dateRange.start && dateRange.end) {
7778
7788
  calendarOneState.setFocusedDate(dateRange.start);
@@ -7814,7 +7824,7 @@
7814
7824
  range: value,
7815
7825
  firstDayOfWeek
7816
7826
  }
7817
- ))), /* @__PURE__ */ React.createElement(react.HStack, { pt: "2", alignSelf: "end" }, isClearable && /* @__PURE__ */ React.createElement(Button, { onClick: resetDate, variant: "ghost", size: "sm" }, "Clear"), /* @__PURE__ */ React.createElement(Button, { variant: "brand", onClick: handleSave, size: "sm" }, buttonLabel))))))));
7827
+ ))), /* @__PURE__ */ React.createElement(react.HStack, { pt: "2", alignSelf: "end" }, isClearable && /* @__PURE__ */ React.createElement(Button, { onClick: handleOnReset, variant: "ghost", size: "sm" }, clearButtonLabel), /* @__PURE__ */ React.createElement(Button, { variant: "brand", onClick: handleSave, size: "sm" }, buttonLabel))))))));
7818
7828
  };
7819
7829
 
7820
7830
  const isValidDateRange = (value) => ramda.is(Object, value) && ramda.has("startDate", value) && ramda.has("endDate", value) && ramda.is(String, value.startDate) && ramda.is(String, value.endDate);
@@ -7852,6 +7862,7 @@
7852
7862
  }
7853
7863
  return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
7854
7864
  };
7865
+ const isDatesEqual = (date1, date2) => (date1 == null ? void 0 : date1.startDate) === (date2 == null ? void 0 : date2.startDate) && (date1 == null ? void 0 : date1.endDate) === (date2 == null ? void 0 : date2.endDate);
7855
7866
  const DateRangePicker = (props) => {
7856
7867
  const {
7857
7868
  isDisabled,
@@ -7869,7 +7880,13 @@
7869
7880
  renderInPortal = false,
7870
7881
  firstDayOfWeek,
7871
7882
  onSave,
7872
- buttonLabel = "Save"
7883
+ buttonLabel = "Save",
7884
+ clearButtonLabel = "Clear",
7885
+ setIsOpen = () => {
7886
+ },
7887
+ savedDateRange = value,
7888
+ defaultDateRange = value,
7889
+ CustomResetButton
7873
7890
  } = props;
7874
7891
  const ref = React.useRef();
7875
7892
  const { group } = react.useMultiStyleConfig("DatePicker");
@@ -7884,10 +7901,22 @@
7884
7901
  minValue: ramda.isNil(minValue) ? void 0 : date.parseDate(minValue),
7885
7902
  maxValue: ramda.isNil(maxValue) ? void 0 : date.parseDate(maxValue)
7886
7903
  };
7887
- const state = datepicker$1.useDateRangePickerState(__spreadProps$e(__spreadValues$1p(__spreadValues$1p({}, props), parsedProps), {
7904
+ const state = datepicker$1.useDateRangePickerState({
7905
+ value: parsedProps.value,
7906
+ onChange: parsedProps.onChange,
7907
+ minValue: parsedProps.minValue,
7908
+ maxValue: parsedProps.maxValue,
7909
+ isDisabled,
7910
+ isInvalid,
7911
+ placeholderValue: props.placeholderValue,
7912
+ isDateUnavailable: props.isDateUnavailable,
7913
+ allowsNonContiguousRanges: props.allowsNonContiguousRanges,
7888
7914
  shouldCloseOnSelect: false,
7889
7915
  hideTimeZone: true
7890
- }));
7916
+ });
7917
+ React.useEffect(() => {
7918
+ setIsOpen(state.isOpen);
7919
+ }, [state.isOpen]);
7891
7920
  const {
7892
7921
  groupProps,
7893
7922
  startFieldProps,
@@ -7896,20 +7925,60 @@
7896
7925
  dialogProps,
7897
7926
  calendarProps
7898
7927
  } = datepicker.useDateRangePicker(
7899
- __spreadProps$e(__spreadValues$1p(__spreadValues$1p({}, props), parsedProps), {
7900
- minValue: parsedProps.minValue || date.parseDate("1994-03-08")
7901
- }),
7928
+ {
7929
+ onChange: (date) => {
7930
+ onChangeCallback({
7931
+ startDate: date == null ? void 0 : date.start.toString(),
7932
+ endDate: date == null ? void 0 : date.end.toString()
7933
+ });
7934
+ },
7935
+ value: parseValue(value),
7936
+ minValue: parsedProps.minValue || date.parseDate("1994-03-08"),
7937
+ maxValue: parsedProps.maxValue,
7938
+ placeholderValue: props.placeholderValue,
7939
+ isDateUnavailable: props.isDateUnavailable,
7940
+ allowsNonContiguousRanges: props.allowsNonContiguousRanges,
7941
+ isDisabled,
7942
+ isInvalid,
7943
+ startName: props.startName,
7944
+ endName: props.endName
7945
+ },
7902
7946
  state,
7903
7947
  ref
7904
7948
  );
7905
7949
  const togglePopup = () => state.setOpen(!state.isOpen);
7906
7950
  const handleClose = () => {
7951
+ onChangeCallback(savedDateRange);
7907
7952
  state.setOpen(false);
7908
7953
  };
7909
7954
  react.useOutsideClick({
7910
7955
  ref,
7911
7956
  handler: () => state.setOpen(false)
7912
7957
  });
7958
+ const ResetButton = CustomResetButton || /* @__PURE__ */ React.createElement(
7959
+ IconButton,
7960
+ {
7961
+ "aria-label": "reset-date",
7962
+ variant: "danger",
7963
+ size: "sm",
7964
+ fontSize: "xs",
7965
+ hidden: !isClearable,
7966
+ isDisabled,
7967
+ onClick: resetDate,
7968
+ icon: /* @__PURE__ */ React.createElement(Icon, { as: NorthlightIcons.XCloseSolid })
7969
+ }
7970
+ );
7971
+ const isCurrentDateSaved = isDatesEqual(value, savedDateRange);
7972
+ const isCurrentDateDefault = isDatesEqual(value, defaultDateRange);
7973
+ const cancelDateChange = () => {
7974
+ onChangeCallback(savedDateRange);
7975
+ };
7976
+ const handleSave = () => {
7977
+ onSave == null ? void 0 : onSave();
7978
+ handleClose();
7979
+ };
7980
+ const shouldShowResetButton = !state.isOpen && isCurrentDateSaved && !isCurrentDateDefault;
7981
+ const shouldShowSaveAndCancelButtons = !state.isOpen && !isCurrentDateSaved;
7913
7982
  return /* @__PURE__ */ React.createElement(
7914
7983
  Popover,
7915
7984
  {
@@ -7931,19 +8000,28 @@
7931
8000
  isDisabled,
7932
8001
  handleClick: togglePopup
7933
8002
  })
7934
- ))), /* @__PURE__ */ React.createElement(
8003
+ ))), shouldShowResetButton && ResetButton, shouldShowSaveAndCancelButtons && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(
7935
8004
  IconButton,
7936
8005
  {
7937
- "aria-label": "reset-date",
7938
- variant: "danger",
8006
+ "aria-label": "cancel-date-change",
8007
+ variant: "ghost",
7939
8008
  size: "sm",
7940
8009
  fontSize: "xs",
7941
- onClick: resetDate,
7942
- hidden: !isClearable,
8010
+ onClick: cancelDateChange,
7943
8011
  isDisabled,
7944
8012
  icon: /* @__PURE__ */ React.createElement(Icon, { as: NorthlightIcons.XCloseSolid })
7945
8013
  }
7946
- ))),
8014
+ ), /* @__PURE__ */ React.createElement(
8015
+ IconButton,
8016
+ {
8017
+ "aria-label": "save-date",
8018
+ variant: "brand",
8019
+ size: "sm",
8020
+ fontSize: "xs",
8021
+ onClick: handleSave,
8022
+ icon: /* @__PURE__ */ React.createElement(Icon, { as: NorthlightIcons.CheckSolid })
8023
+ }
8024
+ )))),
7947
8025
  /* @__PURE__ */ React.createElement(PortalWrapper, { renderInPortal }, state.isOpen && /* @__PURE__ */ React.createElement(react.PopoverContent, __spreadProps$e(__spreadValues$1p({}, dialogProps), { ref, w: "max-content" }), /* @__PURE__ */ React.createElement(focus.FocusScope, { contain: true, restoreFocus: true }, /* @__PURE__ */ React.createElement(DatePickerLocaleWrapper, { firstDayOfWeek }, /* @__PURE__ */ React.createElement(
7948
8026
  RangeCalendar,
7949
8027
  __spreadProps$e(__spreadValues$1p({}, calendarProps), {
@@ -7954,6 +8032,8 @@
7954
8032
  isClearable,
7955
8033
  firstDayOfWeek,
7956
8034
  onSave,
8035
+ onCancel: cancelDateChange,
8036
+ clearButtonLabel,
7957
8037
  buttonLabel
7958
8038
  })
7959
8039
  )))))