-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathsample.tsx
More file actions
171 lines (144 loc) · 5.21 KB
/
sample.tsx
File metadata and controls
171 lines (144 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { useState, useRef } from "react";
import { createComponent } from "@ui5/webcomponents-base/dist/createComponent.js";
import DynamicDateRangeClass from "@ui5/webcomponents/dist/DynamicDateRange.js";
import StepInputClass from "@ui5/webcomponents/dist/StepInput.js";
import LabelClass from "@ui5/webcomponents/dist/Label.js";
import "@ui5/webcomponents/dist/dynamic-date-range-options/Today.js";
import { jsx, jsxs } from "@ui5/webcomponents-base/jsx-runtime";
const DynamicDateRange = createComponent(DynamicDateRangeClass);
/**
* Custom option: Today +/- Days
*
* This option allows the user to define a date range relative to today
* by specifying how many days to add (future) and subtract (past).
* For example, +3 / -2 would produce a range from 2 days ago to 3 days from now.
*/
class TodayPlusMinusDays {
get text() {
return "Today +/- Days";
}
get operator() {
return "TODAYPLUSMINUSDAYS";
}
get icon() {
return "date-time";
}
parse(value) {
const match = value.match(/Today\s*\+(\d+)\s*\/\s*-(\d+)\s*Days/i);
if (match) {
return {
operator: this.operator,
values: [Number.parseInt(match[1]), Number.parseInt(match[2])],
};
}
return undefined;
}
format(value) {
// When called with no values (initial selection)
if (!value?.values || value.values.length === 0) {
return "Today +1 / -1 Days";
}
// When called with Date objects (from currentValueText via toDates)
if (value.values[0] instanceof Date) {
const startDate = value.values[0];
const endDate = value.values[1] || value.values[0];
return `${startDate.toLocaleDateString()} - ${endDate.toLocaleDateString()}`;
}
// Normal case: values are numbers from StepInput
const plusDays = value.values[0] ?? 0;
const minusDays = value.values[1] ?? 0;
return `Today +${plusDays} / -${minusDays} Days`;
}
toDates(value) {
const plusDays = value?.values?.[0] ?? 0;
const minusDays = value?.values?.[1] ?? 0;
const startDate = new Date();
startDate.setDate(startDate.getDate() - minusDays);
startDate.setHours(0, 0, 0, 0);
const endDate = new Date();
endDate.setDate(endDate.getDate() + plusDays);
endDate.setHours(23, 59, 59, 999);
return [startDate, endDate];
}
isValidString(value) {
return /^Today\s*\+\d+\s*\/\s*-\d+\s*Days$/i.test(value);
}
handleSelectionChange(e, currentValue) {
const target = e.target;
const plusDays = currentValue?.values?.[0] ?? 1;
const minusDays = currentValue?.values?.[1] ?? 1;
if (target.getAttribute("data-field") === "plus") {
return {
operator: this.operator,
values: [Number(target.value), minusDays],
};
}
if (target.getAttribute("data-field") === "minus") {
return {
operator: this.operator,
values: [plusDays, Number(target.value)],
};
}
return currentValue;
}
template(this: DynamicDateRangeClass) {
const currentValue = this.currentValue;
const plusDays = currentValue?.values?.[0] ?? 1;
const minusDays = currentValue?.values?.[1] ?? 1;
return jsxs("div", {
class: "ui5-last-next-container ui5-last-next-container-padded",
children: [
jsxs("div", {
class: "ui5-ddr-input-container ui5-ddr-input-container-right-aligned",
children: [
jsx(LabelClass, { class: "ui5-ddr-label", children: "Days After Today" }, "plus-label"),
jsx(StepInputClass, {
value: plusDays,
min: 0,
max: 999,
"data-field": "plus",
onChange: this.handleSelectionChange,
}, "plus-input"),
jsx(LabelClass, { class: "ui5-ddr-label", children: "Days Before Today" }, "minus-label"),
jsx(StepInputClass, {
value: minusDays,
min: 0,
max: 999,
"data-field": "minus",
onChange: this.handleSelectionChange,
}, "minus-input"),
],
}, "inner"),
],
}, "outer");
}
}
DynamicDateRangeClass.register("TODAYPLUSMINUSDAYS", TodayPlusMinusDays);
function App() {
const ddrRef = useRef<DynamicDateRangeClass>(null);
const [selectedValue, setSelectedValue] = useState("");
const [convertedDates, setConvertedDates] = useState("");
const handleChange = (e: CustomEvent) => {
const value = (e.target as DynamicDateRangeClass).value;
setSelectedValue(JSON.stringify(value));
const dates = ddrRef.current!.toDates(value);
setConvertedDates(dates.map((date: Date) => date.toLocaleString()).join(" - "));
};
return (
<>
<DynamicDateRange
ref={ddrRef}
id="dynamicDateRange"
options="TODAY, TODAYPLUSMINUSDAYS"
onChange={handleChange}
/>
<div style={{ marginTop: "20px" }}>
<label htmlFor="selectedValue">Selected Value:</label>
<input id="selectedValue" type="text" readOnly style={{ width: "300px" }} value={selectedValue} />
<label htmlFor="convertedDates" style={{ marginLeft: "20px" }}>Converted Dates:</label>
<input id="convertedDates" type="text" readOnly style={{ width: "300px" }} value={convertedDates} />
</div>
</>
);
}
export default App;