schummar-table 0.48.1 → 0.49.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.js CHANGED
@@ -6,7 +6,7 @@ import { castDraft } from "immer";
6
6
  import { ClassNames, css } from "@emotion/react";
7
7
  import { Fragment as Fragment$1, jsx, jsxs } from "@emotion/react/jsx-runtime";
8
8
  import { nanoid } from "nanoid";
9
- import { useDayzed } from "dayzed";
9
+ import { addDays, differenceInCalendarMonths, isBefore, isToday, startOfDay as startOfDay$1 } from "date-fns";
10
10
 
11
11
  //#region src/misc/tableContext.ts
12
12
  const TableContext = createContext(null);
@@ -921,6 +921,430 @@ function CombinedFilter({ columnIds: inputColumnIds,...props }) {
921
921
  }, columnId)) });
922
922
  }
923
923
 
924
+ //#endregion
925
+ //#region src/vendor/dayzed/utils.ts
926
+ /**
927
+ * This is intended to be used to compose event handlers
928
+ * They are executed in order until one of them calls
929
+ * `event.preventDefault()`. Not sure this is the best
930
+ * way to do this, but it seems legit...
931
+ * @param {Function} fns the event hanlder functions
932
+ * @return {Function} the event handler to add to an element
933
+ */
934
+ function composeEventHandlers(...fns) {
935
+ return (event, ...args) => fns.some((fn) => {
936
+ if (fn) fn(event, ...args);
937
+ return event.defaultPrevented;
938
+ });
939
+ }
940
+ /**
941
+ * Throws a helpful error message for required properties. Useful
942
+ * to be used as a default in destructuring or object params.
943
+ * @param {String} fnName the function name
944
+ * @param {String} propName the prop name
945
+ */
946
+ function requiredProp(fnName, propName) {
947
+ throw new Error(`The property "${propName}" is required in "${fnName}"`);
948
+ }
949
+ /**
950
+ * Takes a calendars array and figures out the number of months to subtract
951
+ * based on the current offset and the minDate allowed.
952
+ * @param {Object} param The param object
953
+ * @param {Array} param.calendars The calendars array created by the getCalendars function
954
+ * @param {Number} param.offset The num of months to be subtracted
955
+ * @param {Date} param.minDate The earliest date we are allow to subtract back to
956
+ * @returns {Number} The number of months to subtract
957
+ */
958
+ function subtractMonth({ calendars, offset, minDate }) {
959
+ if (offset > 1 && minDate) {
960
+ const { firstDayOfMonth } = calendars[0];
961
+ const diffInMonths = differenceInCalendarMonths(firstDayOfMonth, minDate);
962
+ if (diffInMonths < offset) offset = diffInMonths;
963
+ }
964
+ return offset;
965
+ }
966
+ /**
967
+ * Takes a calendars array and figures out the number of months to add
968
+ * based on the current offset and the maxDate allowed.
969
+ * @param {Object} param The param object
970
+ * @param {Array} param.calendars The calendars array created by the getCalendars function
971
+ * @param {Number} param.offset The num of months to be added
972
+ * @param {Date} param.maxDate The furthest date we are allow to add forward to
973
+ * @returns {Number} The number of months to add
974
+ */
975
+ function addMonth({ calendars, offset, maxDate }) {
976
+ if (offset > 1 && maxDate) {
977
+ const { lastDayOfMonth } = calendars[calendars.length - 1];
978
+ const diffInMonths = differenceInCalendarMonths(maxDate, lastDayOfMonth);
979
+ if (diffInMonths < offset) offset = diffInMonths;
980
+ }
981
+ return offset;
982
+ }
983
+ /**
984
+ * Takes a calendars array and figures out if the back button should be
985
+ * disabled based on the minDate allowed.
986
+ * @param {Object} param The param object
987
+ * @param {Array} param.calendars The calendars array created by the getCalendars function
988
+ * @param {Date} param.minDate The earliest date available
989
+ * @returns {Boolean} Whether the back button should be disabled.
990
+ */
991
+ function isBackDisabled({ calendars, minDate }) {
992
+ if (!minDate) return false;
993
+ const { firstDayOfMonth } = calendars[0];
994
+ if (isBefore(addDays(firstDayOfMonth, -1), minDate)) return true;
995
+ return false;
996
+ }
997
+ /**
998
+ * Takes a calendars array and figures out if the forward button should be
999
+ * disabled based on the maxDate allowed.
1000
+ * @param {Object} param The param object
1001
+ * @param {Array} param.calendars The calendars array created by the getCalendars function
1002
+ * @param {Date} param.maxDate The furthest date available
1003
+ * @returns {Boolean} Whether the forward button should be disabled.
1004
+ */
1005
+ function isForwardDisabled({ calendars, maxDate }) {
1006
+ if (!maxDate) return false;
1007
+ const { lastDayOfMonth } = calendars[calendars.length - 1];
1008
+ if (isBefore(maxDate, addDays(lastDayOfMonth, 1))) return true;
1009
+ return false;
1010
+ }
1011
+ /**
1012
+ * Figures out the months data needed based off the number of monthsToDisplay
1013
+ * and other options provided.
1014
+ * @param {Object} param The param object
1015
+ * @param {Date} param.date The date to start the calendar at
1016
+ * @param {Array.<Date>} param.selected An array of dates currently selected
1017
+ * @param {Number} param.monthsToDisplay The number of months to return in the calendar view
1018
+ * @param {Number} param.offset The number of months to offset based off the param.date given
1019
+ * @param {Date} param.minDate The earliest date available
1020
+ * @param {Date} param.maxDate The furthest date available
1021
+ * @param {Number} param.firstDayOfWeek First day of week, 0-6 (Sunday to Saturday)
1022
+ * @param {Bool} param.showOutsideDays Flag to fill front and back weeks with dates from adjacent months
1023
+ * @returns {Array.<Object>} An array of objects with month data
1024
+ */
1025
+ function getCalendars({ date, selected, monthsToDisplay, offset, minDate, maxDate, firstDayOfWeek, showOutsideDays }) {
1026
+ const months = [];
1027
+ const startDate = getStartDate(date, minDate, maxDate);
1028
+ for (let i = 0; i < monthsToDisplay; i++) {
1029
+ const calendarDates = getMonths({
1030
+ month: startDate.getMonth() + i + offset,
1031
+ year: startDate.getFullYear(),
1032
+ selectedDates: selected,
1033
+ minDate,
1034
+ maxDate,
1035
+ firstDayOfWeek,
1036
+ showOutsideDays
1037
+ });
1038
+ months.push(calendarDates);
1039
+ }
1040
+ return months;
1041
+ }
1042
+ /**
1043
+ * Figures out the actual start date based on
1044
+ * the min and max dates available.
1045
+ * @param {Date} date The we want to start the calendar at
1046
+ * @param {Date} minDate The earliest date available to start at
1047
+ * @param {Date} maxDate The latest date available to start at
1048
+ * @returns {Date} The actual start date
1049
+ */
1050
+ function getStartDate(date, minDate, maxDate) {
1051
+ let startDate = startOfDay$1(date);
1052
+ if (minDate) {
1053
+ const minDateNormalized = startOfDay$1(minDate);
1054
+ if (isBefore(startDate, minDateNormalized)) startDate = minDateNormalized;
1055
+ }
1056
+ if (maxDate) {
1057
+ const maxDateNormalized = startOfDay$1(maxDate);
1058
+ if (isBefore(maxDateNormalized, startDate)) startDate = maxDateNormalized;
1059
+ }
1060
+ return startDate;
1061
+ }
1062
+ /**
1063
+ * Figures what week/day data to return for the given month
1064
+ * and year. Adds flags to day data if found in the given selectedDates,
1065
+ * if is selectable inside the given min and max dates, or is today.
1066
+ * @param {Object} param The param object
1067
+ * @param {Number} param.month The month to grab data for
1068
+ * @param {Number} param.year The year to grab data for
1069
+ * @param {Array.<Date>} param.selectedDates An array of dates currently selected
1070
+ * @param {Date} param.minDate The earliest date available
1071
+ * @param {Date} param.maxDate The furthest date available
1072
+ * @param {Number} param.firstDayOfWeek First day of week, 0-6 (Sunday to Saturday)
1073
+ * @param {Bool} param.showOutsideDays Flag to fill front and back weeks with dates from adjacent months
1074
+ * @returns {Object} The data for the selected month/year
1075
+ */
1076
+ function getMonths({ month, year, selectedDates, minDate, maxDate, firstDayOfWeek, showOutsideDays }) {
1077
+ const daysMonthYear = getNumDaysMonthYear(month, year);
1078
+ const daysInMonth = daysMonthYear.daysInMonth;
1079
+ month = daysMonthYear.month;
1080
+ year = daysMonthYear.year;
1081
+ const dates = [];
1082
+ for (let day = 1; day <= daysInMonth; day++) {
1083
+ const date = new Date(year, month, day);
1084
+ const dateObj = {
1085
+ date,
1086
+ selected: isSelected(selectedDates, date),
1087
+ selectable: isSelectable(minDate, maxDate, date),
1088
+ today: isToday(date),
1089
+ prevMonth: false,
1090
+ nextMonth: false
1091
+ };
1092
+ dates.push(dateObj);
1093
+ }
1094
+ const firstDayOfMonth = new Date(year, month, 1);
1095
+ const lastDayOfMonth = new Date(year, month, daysInMonth);
1096
+ const frontWeekBuffer = fillFrontWeek({
1097
+ firstDayOfMonth,
1098
+ minDate,
1099
+ maxDate,
1100
+ selectedDates,
1101
+ firstDayOfWeek,
1102
+ showOutsideDays
1103
+ });
1104
+ const backWeekBuffer = fillBackWeek({
1105
+ lastDayOfMonth,
1106
+ minDate,
1107
+ maxDate,
1108
+ selectedDates,
1109
+ firstDayOfWeek,
1110
+ showOutsideDays
1111
+ });
1112
+ dates.unshift(...frontWeekBuffer);
1113
+ dates.push(...backWeekBuffer);
1114
+ const weeks = getWeeks(dates);
1115
+ return {
1116
+ firstDayOfMonth,
1117
+ lastDayOfMonth,
1118
+ month,
1119
+ year,
1120
+ weeks
1121
+ };
1122
+ }
1123
+ /**
1124
+ * Fill front week with either empty buffer or dates from previous month,
1125
+ * depending on showOutsideDays flag
1126
+ * @param {Object} param The param object
1127
+ * @param {Array.<Date>} param.selectedDates An array of dates currently selected
1128
+ * @param {Date} param.minDate The earliest date available
1129
+ * @param {Date} param.maxDate The furthest date available
1130
+ * @param {Date} param.firstDayOfMonth First day of the month
1131
+ * @param {Number} param.firstDayOfWeek First day of week, 0-6 (Sunday to Saturday)
1132
+ * @param {Bool} param.showOutsideDays Flag to fill front and back weeks with dates from adjacent months
1133
+ * @returns {Array.<Date>} Buffer to fill front week
1134
+ */
1135
+ function fillFrontWeek({ firstDayOfMonth, minDate, maxDate, selectedDates, firstDayOfWeek, showOutsideDays }) {
1136
+ const dates = [];
1137
+ let firstDay = (firstDayOfMonth.getDay() + 7 - firstDayOfWeek) % 7;
1138
+ if (showOutsideDays) {
1139
+ const lastDayOfPrevMonth = addDays(firstDayOfMonth, -1);
1140
+ const prevDate = lastDayOfPrevMonth.getDate();
1141
+ const prevDateMonth = lastDayOfPrevMonth.getMonth();
1142
+ const prevDateYear = lastDayOfPrevMonth.getFullYear();
1143
+ let counter = 0;
1144
+ while (counter < firstDay) {
1145
+ const date = new Date(prevDateYear, prevDateMonth, prevDate - counter);
1146
+ const dateObj = {
1147
+ date,
1148
+ selected: isSelected(selectedDates, date),
1149
+ selectable: isSelectable(minDate, maxDate, date),
1150
+ today: false,
1151
+ prevMonth: true,
1152
+ nextMonth: false
1153
+ };
1154
+ dates.unshift(dateObj);
1155
+ counter++;
1156
+ }
1157
+ } else while (firstDay > 0) {
1158
+ dates.unshift("");
1159
+ firstDay--;
1160
+ }
1161
+ return dates;
1162
+ }
1163
+ /**
1164
+ * Fill back weeks with either empty buffer or dates from next month,
1165
+ * depending on showOutsideDays flag
1166
+ * @param {Object} param The param object
1167
+ * @param {Array.<Date>} param.selectedDates An array of dates currently selected
1168
+ * @param {Date} param.minDate The earliest date available
1169
+ * @param {Date} param.maxDate The furthest date available
1170
+ * @param {Date} param.lastDayOfMonth Last day of the month
1171
+ * @param {Number} param.firstDayOfWeek First day of week, 0-6 (Sunday to Saturday)
1172
+ * @param {Bool} param.showOutsideDays Flag to fill front and back weeks with dates from adjacent months
1173
+ * @returns {Array.<Date>} Buffer to fill back week
1174
+ */
1175
+ function fillBackWeek({ lastDayOfMonth, minDate, maxDate, selectedDates, firstDayOfWeek, showOutsideDays }) {
1176
+ const dates = [];
1177
+ let lastDay = (lastDayOfMonth.getDay() + 7 - firstDayOfWeek) % 7;
1178
+ if (showOutsideDays) {
1179
+ const firstDayOfNextMonth = addDays(lastDayOfMonth, 1);
1180
+ const nextDateMonth = firstDayOfNextMonth.getMonth();
1181
+ const nextDateYear = firstDayOfNextMonth.getFullYear();
1182
+ let counter = 0;
1183
+ while (counter < 6 - lastDay) {
1184
+ const date = new Date(nextDateYear, nextDateMonth, 1 + counter);
1185
+ const dateObj = {
1186
+ date,
1187
+ selected: isSelected(selectedDates, date),
1188
+ selectable: isSelectable(minDate, maxDate, date),
1189
+ today: false,
1190
+ prevMonth: false,
1191
+ nextMonth: true
1192
+ };
1193
+ dates.push(dateObj);
1194
+ counter++;
1195
+ }
1196
+ } else while (lastDay < 6) {
1197
+ dates.push("");
1198
+ lastDay++;
1199
+ }
1200
+ return dates;
1201
+ }
1202
+ /**
1203
+ * Normalizes month (could be overflow) and year pairs and returns the
1204
+ * normalized month and year along with the number of days in the month.
1205
+ * @param {Number} month The month to normalize
1206
+ * @param {Number} year The year to normalize
1207
+ * @returns {Object} The normalized month and year along with the number of days in the month
1208
+ */
1209
+ function getNumDaysMonthYear(month, year) {
1210
+ const normalizedMonthYear = new Date(year, month, 1);
1211
+ month = normalizedMonthYear.getMonth();
1212
+ year = normalizedMonthYear.getFullYear();
1213
+ return {
1214
+ daysInMonth: 32 - new Date(year, month, 32).getDate(),
1215
+ month,
1216
+ year
1217
+ };
1218
+ }
1219
+ /**
1220
+ * Takes an array of dates, and turns them into a multi dimensional
1221
+ * array with 7 entries for each week.
1222
+ * @param {Array.<Object>} dates An array of dates
1223
+ * @returns {Array} The weeks as a multi dimensional array
1224
+ */
1225
+ function getWeeks(dates) {
1226
+ const weeksLength = Math.ceil(dates.length / 7);
1227
+ const weeks = [];
1228
+ for (let i = 0; i < weeksLength; i++) {
1229
+ weeks[i] = [];
1230
+ for (let x = 0; x < 7; x++) weeks[i].push(dates[i * 7 + x]);
1231
+ }
1232
+ return weeks;
1233
+ }
1234
+ /**
1235
+ * Normalizes dates to the beginning of the day,
1236
+ * then checks to see if the day given is found
1237
+ * in the selectedDates.
1238
+ * @param {Array.<Date>} selectedDates An array of dates currently selected
1239
+ * @param {Date} date The date to search with against selectedDates
1240
+ * @returns {Boolean} Whether day is found in selectedDates
1241
+ */
1242
+ function isSelected(selectedDates, date) {
1243
+ selectedDates = Array.isArray(selectedDates) ? selectedDates : [selectedDates];
1244
+ return selectedDates.some((selectedDate) => {
1245
+ if (selectedDate instanceof Date && startOfDay$1(selectedDate).getTime() === startOfDay$1(date).getTime()) return true;
1246
+ return false;
1247
+ });
1248
+ }
1249
+ /**
1250
+ * Checks to see if the date given is
1251
+ * between the min and max dates.
1252
+ * @param {Date} minDate The earliest date available
1253
+ * @param {Date} maxDate The furthest date available
1254
+ * @param {Date} date The date to compare with
1255
+ * @returns {Boolean} Whether the date is between min and max date
1256
+ */
1257
+ function isSelectable(minDate, maxDate, date) {
1258
+ if (minDate && isBefore(date, minDate) || maxDate && isBefore(maxDate, date)) return false;
1259
+ return true;
1260
+ }
1261
+
1262
+ //#endregion
1263
+ //#region src/vendor/dayzed/dayzed.ts
1264
+ function isOffsetControlled(propOffset) {
1265
+ return propOffset !== void 0;
1266
+ }
1267
+ function getOffset(prop, state) {
1268
+ return isOffsetControlled(prop) ? prop : state;
1269
+ }
1270
+ function getDateProps(onDateSelected, { onClick, dateObj = requiredProp("getDateProps", "dateObj"),...rest } = {}) {
1271
+ return {
1272
+ onClick: composeEventHandlers(onClick, (event) => {
1273
+ onDateSelected?.(dateObj, event);
1274
+ }),
1275
+ disabled: !dateObj.selectable,
1276
+ "aria-label": dateObj.date.toDateString(),
1277
+ "aria-pressed": dateObj.selected,
1278
+ role: "button",
1279
+ ...rest
1280
+ };
1281
+ }
1282
+ function getBackProps({ minDate, offsetMonth, handleOffsetChanged }, { onClick, offset = 1, calendars = requiredProp("getBackProps", "calendars"),...rest } = {}) {
1283
+ return {
1284
+ onClick: composeEventHandlers(onClick, () => {
1285
+ handleOffsetChanged(offsetMonth - subtractMonth({
1286
+ calendars,
1287
+ offset,
1288
+ minDate
1289
+ }));
1290
+ }),
1291
+ disabled: isBackDisabled({
1292
+ calendars,
1293
+ minDate
1294
+ }),
1295
+ "aria-label": `Go back ${offset} month${offset === 1 ? "" : "s"}`,
1296
+ ...rest
1297
+ };
1298
+ }
1299
+ function getForwardProps({ maxDate, offsetMonth, handleOffsetChanged }, { onClick, offset = 1, calendars = requiredProp("getForwardProps", "calendars"),...rest } = {}) {
1300
+ return {
1301
+ onClick: composeEventHandlers(onClick, () => {
1302
+ handleOffsetChanged(offsetMonth + addMonth({
1303
+ calendars,
1304
+ offset,
1305
+ maxDate
1306
+ }));
1307
+ }),
1308
+ disabled: isForwardDisabled({
1309
+ calendars,
1310
+ maxDate
1311
+ }),
1312
+ "aria-label": `Go forward ${offset} month${offset === 1 ? "" : "s"}`,
1313
+ ...rest
1314
+ };
1315
+ }
1316
+ function useDayzed({ date = /* @__PURE__ */ new Date(), maxDate, minDate, monthsToDisplay = 1, firstDayOfWeek = 0, showOutsideDays = false, offset, onDateSelected, onOffsetChanged = () => {}, selected }) {
1317
+ const [stateOffset, setStateOffset] = useState(0);
1318
+ const offsetMonth = getOffset(offset, stateOffset);
1319
+ function handleOffsetChanged(newOffset) {
1320
+ if (!isOffsetControlled(offset)) setStateOffset(newOffset);
1321
+ onOffsetChanged(newOffset);
1322
+ }
1323
+ return {
1324
+ calendars: getCalendars({
1325
+ date,
1326
+ selected: Array.isArray(selected) ? selected : selected ? [selected] : [],
1327
+ monthsToDisplay,
1328
+ minDate,
1329
+ maxDate,
1330
+ offset: offsetMonth,
1331
+ firstDayOfWeek,
1332
+ showOutsideDays
1333
+ }),
1334
+ getDateProps: getDateProps.bind(null, onDateSelected),
1335
+ getBackProps: getBackProps.bind(null, {
1336
+ minDate,
1337
+ offsetMonth,
1338
+ handleOffsetChanged
1339
+ }),
1340
+ getForwardProps: getForwardProps.bind(null, {
1341
+ maxDate,
1342
+ offsetMonth,
1343
+ handleOffsetChanged
1344
+ })
1345
+ };
1346
+ }
1347
+
924
1348
  //#endregion
925
1349
  //#region src/misc/defaults.ts
926
1350
  function defaults(first, ...more) {
@@ -1346,7 +1770,7 @@ function DatePicker(props) {
1346
1770
  children: label
1347
1771
  }, index);
1348
1772
  });
1349
- const { calendars, getBackProps, getForwardProps, getDateProps } = useDayzed({
1773
+ const { calendars, getBackProps: getBackProps$1, getForwardProps: getForwardProps$1, getDateProps: getDateProps$1 } = useDayzed({
1350
1774
  onDateSelected: () => void 0,
1351
1775
  firstDayOfWeek,
1352
1776
  showOutsideDays: true,
@@ -1445,7 +1869,7 @@ function DatePicker(props) {
1445
1869
  children: [
1446
1870
  /* @__PURE__ */ jsx(IconButton$1, {
1447
1871
  type: "button",
1448
- ...getBackProps({ calendars }),
1872
+ ...getBackProps$1({ calendars }),
1449
1873
  children: /* @__PURE__ */ jsx(ChevronRight$1, { css: { transform: "rotate3d(0, 0, 1, 180deg)" } })
1450
1874
  }),
1451
1875
  /* @__PURE__ */ jsxs("div", {
@@ -1458,7 +1882,7 @@ function DatePicker(props) {
1458
1882
  }),
1459
1883
  /* @__PURE__ */ jsx(IconButton$1, {
1460
1884
  type: "button",
1461
- ...getForwardProps({ calendars }),
1885
+ ...getForwardProps$1({ calendars }),
1462
1886
  children: /* @__PURE__ */ jsx(ChevronRight$1, {})
1463
1887
  })
1464
1888
  ]
@@ -1536,7 +1960,7 @@ function DatePicker(props) {
1536
1960
  selected,
1537
1961
  preSelected
1538
1962
  }),
1539
- ...getDateProps({ dateObj: dateObject }),
1963
+ ...getDateProps$1({ dateObj: dateObject }),
1540
1964
  onClick: () => {
1541
1965
  if (dirty) if (min) set("calendar", min, date);
1542
1966
  else set("calendar", date, max);
@@ -3219,7 +3643,7 @@ function ExpandControl({ itemId, hasDeferredChildren }) {
3219
3643
  function SelectComponent({ itemId }) {
3220
3644
  const table = useTableContext();
3221
3645
  const Checkbox$1 = useTheme((t) => t.components.Checkbox);
3222
- const isSelected = table.useState((state) => {
3646
+ const isSelected$1 = table.useState((state) => {
3223
3647
  const itemIds = itemId ? [itemId] : Array.from(state.activeItemsById.keys());
3224
3648
  return state.activeItemsById.size > 0 && itemIds.every((itemId$1) => state.selection.has(itemId$1));
3225
3649
  });
@@ -3236,9 +3660,9 @@ function SelectComponent({ itemId }) {
3236
3660
  range = item ? [item] : activeItems;
3237
3661
  }
3238
3662
  const newSelection = new Set(selection);
3239
- for (const item of range) if (isSelected) newSelection.delete(item.id);
3663
+ for (const item of range) if (isSelected$1) newSelection.delete(item.id);
3240
3664
  else newSelection.add(item.id);
3241
- if (selectSyncChildren && isSelected) {
3665
+ if (selectSyncChildren && isSelected$1) {
3242
3666
  for (const ancestor of getAncestors(activeItemsById, ...range)) newSelection.delete(ancestor);
3243
3667
  for (const descendant of getDescendants(...range)) newSelection.delete(descendant);
3244
3668
  }
@@ -3255,7 +3679,7 @@ function SelectComponent({ itemId }) {
3255
3679
  justifySelf: "start",
3256
3680
  color: "#c9cfda"
3257
3681
  },
3258
- checked: isSelected,
3682
+ checked: isSelected$1,
3259
3683
  onChange: toggle$1
3260
3684
  });
3261
3685
  }