@trackunit/react-form-components 0.0.140 → 0.0.142

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/index.cjs.js CHANGED
@@ -14760,6 +14760,117 @@ const Schedule = ({ className, dataTestId, schedule, onChange, invalidKeys = []
14760
14760
  }) }));
14761
14761
  };
14762
14762
 
14763
+ const weekDay = {
14764
+ Monday: "monday",
14765
+ Tuesday: "tuesday",
14766
+ Wednesday: "wednesday",
14767
+ Thursday: "thursday",
14768
+ Friday: "friday",
14769
+ Saturday: "saturday",
14770
+ Sunday: "sunday",
14771
+ };
14772
+ exports.ScheduleVariant = void 0;
14773
+ (function (ScheduleVariant) {
14774
+ ScheduleVariant["ALL_DAYS"] = "all";
14775
+ ScheduleVariant["WEEKDAYS"] = "week";
14776
+ ScheduleVariant["CUSTOM"] = "custom";
14777
+ })(exports.ScheduleVariant || (exports.ScheduleVariant = {}));
14778
+ /**
14779
+ * Parse a string of week range schedule string to human readable schedule range.
14780
+ *
14781
+ * @param {string} scheduleString String of week schedule
14782
+ * @returns {WeekSchedule} Week schedule range
14783
+ */
14784
+ const parseSchedule = (scheduleString) => {
14785
+ if (!scheduleString) {
14786
+ return {
14787
+ variant: exports.ScheduleVariant.ALL_DAYS,
14788
+ schedule: [],
14789
+ };
14790
+ }
14791
+ const schedule = scheduleString.split(",").map(daySchedule => {
14792
+ const [day, timeRange] = daySchedule.split("#");
14793
+ const [timeFrom, timeTo] = timeRange.split("-");
14794
+ const isAllDay = timeFrom === "00:00" && timeTo === "24:00";
14795
+ return {
14796
+ day: Number(day),
14797
+ range: isAllDay
14798
+ ? undefined
14799
+ : {
14800
+ timeFrom,
14801
+ timeTo,
14802
+ },
14803
+ isAllDay,
14804
+ };
14805
+ });
14806
+ const filteredSchedule = schedule
14807
+ .filter(daySchedule => daySchedule.range !== null && daySchedule.range !== undefined)
14808
+ .map(daySchedule => ({ day: daySchedule.day, range: daySchedule.range, isAllDay: daySchedule.isAllDay }));
14809
+ let variant;
14810
+ switch (schedule.length) {
14811
+ case 7:
14812
+ const areEqual = schedule.every((day, _, collection) => {
14813
+ var _a, _b, _c, _d, _e, _f;
14814
+ return ((_b = (_a = collection === null || collection === void 0 ? void 0 : collection[0]) === null || _a === void 0 ? void 0 : _a.range) === null || _b === void 0 ? void 0 : _b.timeFrom) === ((_c = day === null || day === void 0 ? void 0 : day.range) === null || _c === void 0 ? void 0 : _c.timeFrom) &&
14815
+ ((_e = (_d = collection === null || collection === void 0 ? void 0 : collection[0]) === null || _d === void 0 ? void 0 : _d.range) === null || _e === void 0 ? void 0 : _e.timeTo) === ((_f = day === null || day === void 0 ? void 0 : day.range) === null || _f === void 0 ? void 0 : _f.timeTo);
14816
+ });
14817
+ if (areEqual) {
14818
+ variant = exports.ScheduleVariant.ALL_DAYS;
14819
+ }
14820
+ else {
14821
+ variant = exports.ScheduleVariant.CUSTOM;
14822
+ }
14823
+ break;
14824
+ case 5:
14825
+ const days = [1, 2, 3, 4, 5];
14826
+ const hasConsecutiveDays = schedule.every(({ day }, index) => day === days[index]);
14827
+ if (hasConsecutiveDays) {
14828
+ variant = exports.ScheduleVariant.WEEKDAYS;
14829
+ }
14830
+ else {
14831
+ variant = exports.ScheduleVariant.CUSTOM;
14832
+ }
14833
+ break;
14834
+ default:
14835
+ return {
14836
+ variant: exports.ScheduleVariant.CUSTOM,
14837
+ schedule: filteredSchedule,
14838
+ };
14839
+ }
14840
+ return {
14841
+ variant,
14842
+ schedule: filteredSchedule,
14843
+ };
14844
+ };
14845
+ /**
14846
+ * Serialize week schedule to string schedule
14847
+ *
14848
+ * @param {WeekSchedule} weekSchedule Week schedule range
14849
+ * @returns {string} Schedule string
14850
+ */
14851
+ const serializeSchedule = (weekSchedule) => {
14852
+ return weekSchedule.schedule
14853
+ .filter(({ range, day, isAllDay }) => {
14854
+ const hasRange = (range === null || range === void 0 ? void 0 : range.timeFrom) && (range === null || range === void 0 ? void 0 : range.timeTo);
14855
+ switch (weekSchedule.variant) {
14856
+ case exports.ScheduleVariant.WEEKDAYS:
14857
+ return day <= 5 && hasRange;
14858
+ case exports.ScheduleVariant.ALL_DAYS:
14859
+ return day <= 7 && hasRange;
14860
+ case exports.ScheduleVariant.CUSTOM:
14861
+ default:
14862
+ return hasRange || isAllDay;
14863
+ }
14864
+ })
14865
+ .map(({ day, range, isAllDay }) => {
14866
+ if (isAllDay) {
14867
+ return `${day}#00:00-24:00`;
14868
+ }
14869
+ return `${day}#${range.timeFrom}-${range.timeTo}`;
14870
+ })
14871
+ .join(",");
14872
+ };
14873
+
14763
14874
  /**
14764
14875
  * A thin wrapper around the `BaseInput` component for text input fields.
14765
14876
  *
@@ -15087,5 +15198,8 @@ exports.cvaSelectPrefix = cvaSelectPrefix;
15087
15198
  exports.cvaSelectXIcon = cvaSelectXIcon;
15088
15199
  exports.getOrderedOptions = getOrderedOptions;
15089
15200
  exports.isMultiValue = isMultiValue;
15201
+ exports.parseSchedule = parseSchedule;
15202
+ exports.serializeSchedule = serializeSchedule;
15090
15203
  exports.useCustomComponents = useCustomComponents;
15091
15204
  exports.usePhoneInput = usePhoneInput;
15205
+ exports.weekDay = weekDay;
package/index.esm.js CHANGED
@@ -14734,6 +14734,117 @@ const Schedule = ({ className, dataTestId, schedule, onChange, invalidKeys = []
14734
14734
  }) }));
14735
14735
  };
14736
14736
 
14737
+ const weekDay = {
14738
+ Monday: "monday",
14739
+ Tuesday: "tuesday",
14740
+ Wednesday: "wednesday",
14741
+ Thursday: "thursday",
14742
+ Friday: "friday",
14743
+ Saturday: "saturday",
14744
+ Sunday: "sunday",
14745
+ };
14746
+ var ScheduleVariant;
14747
+ (function (ScheduleVariant) {
14748
+ ScheduleVariant["ALL_DAYS"] = "all";
14749
+ ScheduleVariant["WEEKDAYS"] = "week";
14750
+ ScheduleVariant["CUSTOM"] = "custom";
14751
+ })(ScheduleVariant || (ScheduleVariant = {}));
14752
+ /**
14753
+ * Parse a string of week range schedule string to human readable schedule range.
14754
+ *
14755
+ * @param {string} scheduleString String of week schedule
14756
+ * @returns {WeekSchedule} Week schedule range
14757
+ */
14758
+ const parseSchedule = (scheduleString) => {
14759
+ if (!scheduleString) {
14760
+ return {
14761
+ variant: ScheduleVariant.ALL_DAYS,
14762
+ schedule: [],
14763
+ };
14764
+ }
14765
+ const schedule = scheduleString.split(",").map(daySchedule => {
14766
+ const [day, timeRange] = daySchedule.split("#");
14767
+ const [timeFrom, timeTo] = timeRange.split("-");
14768
+ const isAllDay = timeFrom === "00:00" && timeTo === "24:00";
14769
+ return {
14770
+ day: Number(day),
14771
+ range: isAllDay
14772
+ ? undefined
14773
+ : {
14774
+ timeFrom,
14775
+ timeTo,
14776
+ },
14777
+ isAllDay,
14778
+ };
14779
+ });
14780
+ const filteredSchedule = schedule
14781
+ .filter(daySchedule => daySchedule.range !== null && daySchedule.range !== undefined)
14782
+ .map(daySchedule => ({ day: daySchedule.day, range: daySchedule.range, isAllDay: daySchedule.isAllDay }));
14783
+ let variant;
14784
+ switch (schedule.length) {
14785
+ case 7:
14786
+ const areEqual = schedule.every((day, _, collection) => {
14787
+ var _a, _b, _c, _d, _e, _f;
14788
+ return ((_b = (_a = collection === null || collection === void 0 ? void 0 : collection[0]) === null || _a === void 0 ? void 0 : _a.range) === null || _b === void 0 ? void 0 : _b.timeFrom) === ((_c = day === null || day === void 0 ? void 0 : day.range) === null || _c === void 0 ? void 0 : _c.timeFrom) &&
14789
+ ((_e = (_d = collection === null || collection === void 0 ? void 0 : collection[0]) === null || _d === void 0 ? void 0 : _d.range) === null || _e === void 0 ? void 0 : _e.timeTo) === ((_f = day === null || day === void 0 ? void 0 : day.range) === null || _f === void 0 ? void 0 : _f.timeTo);
14790
+ });
14791
+ if (areEqual) {
14792
+ variant = ScheduleVariant.ALL_DAYS;
14793
+ }
14794
+ else {
14795
+ variant = ScheduleVariant.CUSTOM;
14796
+ }
14797
+ break;
14798
+ case 5:
14799
+ const days = [1, 2, 3, 4, 5];
14800
+ const hasConsecutiveDays = schedule.every(({ day }, index) => day === days[index]);
14801
+ if (hasConsecutiveDays) {
14802
+ variant = ScheduleVariant.WEEKDAYS;
14803
+ }
14804
+ else {
14805
+ variant = ScheduleVariant.CUSTOM;
14806
+ }
14807
+ break;
14808
+ default:
14809
+ return {
14810
+ variant: ScheduleVariant.CUSTOM,
14811
+ schedule: filteredSchedule,
14812
+ };
14813
+ }
14814
+ return {
14815
+ variant,
14816
+ schedule: filteredSchedule,
14817
+ };
14818
+ };
14819
+ /**
14820
+ * Serialize week schedule to string schedule
14821
+ *
14822
+ * @param {WeekSchedule} weekSchedule Week schedule range
14823
+ * @returns {string} Schedule string
14824
+ */
14825
+ const serializeSchedule = (weekSchedule) => {
14826
+ return weekSchedule.schedule
14827
+ .filter(({ range, day, isAllDay }) => {
14828
+ const hasRange = (range === null || range === void 0 ? void 0 : range.timeFrom) && (range === null || range === void 0 ? void 0 : range.timeTo);
14829
+ switch (weekSchedule.variant) {
14830
+ case ScheduleVariant.WEEKDAYS:
14831
+ return day <= 5 && hasRange;
14832
+ case ScheduleVariant.ALL_DAYS:
14833
+ return day <= 7 && hasRange;
14834
+ case ScheduleVariant.CUSTOM:
14835
+ default:
14836
+ return hasRange || isAllDay;
14837
+ }
14838
+ })
14839
+ .map(({ day, range, isAllDay }) => {
14840
+ if (isAllDay) {
14841
+ return `${day}#00:00-24:00`;
14842
+ }
14843
+ return `${day}#${range.timeFrom}-${range.timeTo}`;
14844
+ })
14845
+ .join(",");
14846
+ };
14847
+
14737
14848
  /**
14738
14849
  * A thin wrapper around the `BaseInput` component for text input fields.
14739
14850
  *
@@ -15008,4 +15119,4 @@ const UploadField = forwardRef((_a, ref) => {
15008
15119
  */
15009
15120
  setupLibraryTranslations();
15010
15121
 
15011
- export { ActionButton, BaseInput, Checkbox, CreatableSelect, DateField, DateInput, DropZone, EmailField, EmailInput, FormGroup, Label, MultiSelectMenuItem, NumberField, NumberInput, OptionCard, PhoneField, PhoneInput, RadioGroup, RadioItem, Schedule, Search, Select, SingleSelectMenuItem, TextArea, TextAreaField, TextField, TextInput, TimeRange, TimeRangeField, Toggle, UploadField, UploadInput, StateManagedSelect$1 as ValueType, cvaInput, cvaInputAddon, cvaInputAddonAfter, cvaInputAddonBefore, cvaInputBase, cvaInputBaseDisabled, cvaInputBaseInvalid, cvaInputField, cvaInputPrefix, cvaInputSuffix, cvaSelect, cvaSelectCounter, cvaSelectDynamicTagContainer, cvaSelectIcon, cvaSelectMenu, cvaSelectMenuList, cvaSelectPrefix, cvaSelectXIcon, getOrderedOptions, isMultiValue, useCustomComponents, usePhoneInput };
15122
+ export { ActionButton, BaseInput, Checkbox, CreatableSelect, DateField, DateInput, DropZone, EmailField, EmailInput, FormGroup, Label, MultiSelectMenuItem, NumberField, NumberInput, OptionCard, PhoneField, PhoneInput, RadioGroup, RadioItem, Schedule, ScheduleVariant, Search, Select, SingleSelectMenuItem, TextArea, TextAreaField, TextField, TextInput, TimeRange, TimeRangeField, Toggle, UploadField, UploadInput, StateManagedSelect$1 as ValueType, cvaInput, cvaInputAddon, cvaInputAddonAfter, cvaInputAddonBefore, cvaInputBase, cvaInputBaseDisabled, cvaInputBaseInvalid, cvaInputField, cvaInputPrefix, cvaInputSuffix, cvaSelect, cvaSelectCounter, cvaSelectDynamicTagContainer, cvaSelectIcon, cvaSelectMenu, cvaSelectMenuList, cvaSelectPrefix, cvaSelectXIcon, getOrderedOptions, isMultiValue, parseSchedule, serializeSchedule, useCustomComponents, usePhoneInput, weekDay };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-form-components",
3
- "version": "0.0.140",
3
+ "version": "0.0.142",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -8,8 +8,8 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@trackunit/css-class-variance-utilities": "0.0.14",
11
- "@trackunit/i18n-library-translation": "0.0.77",
12
- "@trackunit/react-components": "0.1.157",
11
+ "@trackunit/i18n-library-translation": "0.0.78",
12
+ "@trackunit/react-components": "0.1.158",
13
13
  "@trackunit/shared-utils": "0.0.7",
14
14
  "@trackunit/ui-icons": "0.0.76",
15
15
  "date-fns": "2.29.3",
@@ -0,0 +1,39 @@
1
+ import { TimeRangeInput } from "../TimeRange";
2
+ export declare const weekDay: {
3
+ readonly Monday: "monday";
4
+ readonly Tuesday: "tuesday";
5
+ readonly Wednesday: "wednesday";
6
+ readonly Thursday: "thursday";
7
+ readonly Friday: "friday";
8
+ readonly Saturday: "saturday";
9
+ readonly Sunday: "sunday";
10
+ };
11
+ export type WeekDay = (typeof weekDay)[keyof typeof weekDay];
12
+ export declare enum ScheduleVariant {
13
+ ALL_DAYS = "all",
14
+ WEEKDAYS = "week",
15
+ CUSTOM = "custom"
16
+ }
17
+ export interface DaySchedule {
18
+ day: number;
19
+ range: TimeRangeInput;
20
+ isAllDay: boolean;
21
+ }
22
+ export interface WeekSchedule {
23
+ variant: ScheduleVariant;
24
+ schedule: DaySchedule[];
25
+ }
26
+ /**
27
+ * Parse a string of week range schedule string to human readable schedule range.
28
+ *
29
+ * @param {string} scheduleString String of week schedule
30
+ * @returns {WeekSchedule} Week schedule range
31
+ */
32
+ export declare const parseSchedule: (scheduleString: string) => WeekSchedule;
33
+ /**
34
+ * Serialize week schedule to string schedule
35
+ *
36
+ * @param {WeekSchedule} weekSchedule Week schedule range
37
+ * @returns {string} Schedule string
38
+ */
39
+ export declare const serializeSchedule: (weekSchedule: WeekSchedule) => string;
package/src/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export * from "./components/PhoneField/PhoneField";
15
15
  export * from "./components/PhoneInput/PhoneInput";
16
16
  export * from "./components/RadioGroup";
17
17
  export * from "./components/Schedule/Schedule";
18
+ export * from "./components/Schedule/ScheduleParser";
18
19
  export * from "./components/Search";
19
20
  export * from "./components/Select";
20
21
  export * from "./components/TextArea";