@northlight/ui 2.37.0 → 2.38.1

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,
@@ -7870,8 +7881,13 @@
7870
7881
  firstDayOfWeek,
7871
7882
  onSave,
7872
7883
  buttonLabel = "Save",
7884
+ clearButtonLabel = "Clear",
7873
7885
  setIsOpen = () => {
7874
- }
7886
+ },
7887
+ savedDateRange = value,
7888
+ defaultDateRange = value,
7889
+ CustomResetButton,
7890
+ onCancelChanges
7875
7891
  } = props;
7876
7892
  const ref = React.useRef();
7877
7893
  const { group } = react.useMultiStyleConfig("DatePicker");
@@ -7886,10 +7902,19 @@
7886
7902
  minValue: ramda.isNil(minValue) ? void 0 : date.parseDate(minValue),
7887
7903
  maxValue: ramda.isNil(maxValue) ? void 0 : date.parseDate(maxValue)
7888
7904
  };
7889
- const state = datepicker$1.useDateRangePickerState(__spreadProps$e(__spreadValues$1p(__spreadValues$1p({}, props), parsedProps), {
7905
+ const state = datepicker$1.useDateRangePickerState({
7906
+ value: parsedProps.value,
7907
+ onChange: parsedProps.onChange,
7908
+ minValue: parsedProps.minValue,
7909
+ maxValue: parsedProps.maxValue,
7910
+ isDisabled,
7911
+ isInvalid,
7912
+ placeholderValue: props.placeholderValue,
7913
+ isDateUnavailable: props.isDateUnavailable,
7914
+ allowsNonContiguousRanges: props.allowsNonContiguousRanges,
7890
7915
  shouldCloseOnSelect: false,
7891
7916
  hideTimeZone: true
7892
- }));
7917
+ });
7893
7918
  React.useEffect(() => {
7894
7919
  setIsOpen(state.isOpen);
7895
7920
  }, [state.isOpen]);
@@ -7901,20 +7926,64 @@
7901
7926
  dialogProps,
7902
7927
  calendarProps
7903
7928
  } = datepicker.useDateRangePicker(
7904
- __spreadProps$e(__spreadValues$1p(__spreadValues$1p({}, props), parsedProps), {
7905
- minValue: parsedProps.minValue || date.parseDate("1994-03-08")
7906
- }),
7929
+ {
7930
+ onChange: (date) => {
7931
+ onChangeCallback({
7932
+ startDate: date == null ? void 0 : date.start.toString(),
7933
+ endDate: date == null ? void 0 : date.end.toString()
7934
+ });
7935
+ },
7936
+ value: parseValue(value),
7937
+ minValue: parsedProps.minValue || date.parseDate("1994-03-08"),
7938
+ maxValue: parsedProps.maxValue,
7939
+ placeholderValue: props.placeholderValue,
7940
+ isDateUnavailable: props.isDateUnavailable,
7941
+ allowsNonContiguousRanges: props.allowsNonContiguousRanges,
7942
+ isDisabled,
7943
+ isInvalid,
7944
+ startName: props.startName,
7945
+ endName: props.endName
7946
+ },
7907
7947
  state,
7908
7948
  ref
7909
7949
  );
7910
7950
  const togglePopup = () => state.setOpen(!state.isOpen);
7911
7951
  const handleClose = () => {
7952
+ onChangeCallback(savedDateRange);
7912
7953
  state.setOpen(false);
7913
7954
  };
7914
7955
  react.useOutsideClick({
7915
7956
  ref,
7916
7957
  handler: () => state.setOpen(false)
7917
7958
  });
7959
+ const ResetButton = CustomResetButton || /* @__PURE__ */ React.createElement(
7960
+ IconButton,
7961
+ {
7962
+ "aria-label": "reset-date",
7963
+ variant: "danger",
7964
+ size: "sm",
7965
+ fontSize: "xs",
7966
+ hidden: !isClearable,
7967
+ isDisabled,
7968
+ onClick: resetDate,
7969
+ icon: /* @__PURE__ */ React.createElement(Icon, { as: NorthlightIcons.XCloseSolid })
7970
+ }
7971
+ );
7972
+ const isCurrentDateSaved = isDatesEqual(value, savedDateRange);
7973
+ const isCurrentDateDefault = isDatesEqual(value, defaultDateRange);
7974
+ const cancelDateChange = () => {
7975
+ if (onCancelChanges) {
7976
+ onCancelChanges();
7977
+ } else {
7978
+ resetDate();
7979
+ }
7980
+ };
7981
+ const handleSave = () => {
7982
+ onSave == null ? void 0 : onSave();
7983
+ handleClose();
7984
+ };
7985
+ const shouldShowResetButton = !state.isOpen && isCurrentDateSaved && !isCurrentDateDefault;
7986
+ const shouldShowSaveAndCancelButtons = !state.isOpen && !isCurrentDateSaved;
7918
7987
  return /* @__PURE__ */ React.createElement(
7919
7988
  Popover,
7920
7989
  {
@@ -7936,19 +8005,28 @@
7936
8005
  isDisabled,
7937
8006
  handleClick: togglePopup
7938
8007
  })
7939
- ))), /* @__PURE__ */ React.createElement(
8008
+ ))), shouldShowResetButton && ResetButton, shouldShowSaveAndCancelButtons && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(
7940
8009
  IconButton,
7941
8010
  {
7942
- "aria-label": "reset-date",
7943
- variant: "danger",
8011
+ "aria-label": "cancel-date-change",
8012
+ variant: "ghost",
7944
8013
  size: "sm",
7945
8014
  fontSize: "xs",
7946
- onClick: resetDate,
7947
- hidden: !isClearable,
8015
+ onClick: cancelDateChange,
7948
8016
  isDisabled,
7949
8017
  icon: /* @__PURE__ */ React.createElement(Icon, { as: NorthlightIcons.XCloseSolid })
7950
8018
  }
7951
- ))),
8019
+ ), /* @__PURE__ */ React.createElement(
8020
+ IconButton,
8021
+ {
8022
+ "aria-label": "save-date",
8023
+ variant: "brand",
8024
+ size: "sm",
8025
+ fontSize: "xs",
8026
+ onClick: handleSave,
8027
+ icon: /* @__PURE__ */ React.createElement(Icon, { as: NorthlightIcons.CheckSolid })
8028
+ }
8029
+ )))),
7952
8030
  /* @__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(
7953
8031
  RangeCalendar,
7954
8032
  __spreadProps$e(__spreadValues$1p({}, calendarProps), {
@@ -7959,6 +8037,8 @@
7959
8037
  isClearable,
7960
8038
  firstDayOfWeek,
7961
8039
  onSave,
8040
+ onCancel: onCancelChanges,
8041
+ clearButtonLabel,
7962
8042
  buttonLabel
7963
8043
  })
7964
8044
  )))))