@terreno/ui 0.13.3 → 0.14.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.
- package/dist/ActionSheet.d.ts +5 -5
- package/dist/ActionSheet.js +2 -2
- package/dist/ActionSheet.js.map +1 -1
- package/dist/Avatar.js +1 -1
- package/dist/Avatar.js.map +1 -1
- package/dist/Banner.js.map +1 -1
- package/dist/Box.js +2 -0
- package/dist/Box.js.map +1 -1
- package/dist/Button.d.ts +2 -2
- package/dist/Button.js +35 -23
- package/dist/Button.js.map +1 -1
- package/dist/Common.d.ts +16 -4
- package/dist/Common.js +4 -4
- package/dist/Common.js.map +1 -1
- package/dist/ConsentFormScreen.js +3 -3
- package/dist/ConsentFormScreen.js.map +1 -1
- package/dist/ConsentNavigator.d.ts +1 -1
- package/dist/ConsentNavigator.js +2 -1
- package/dist/ConsentNavigator.js.map +1 -1
- package/dist/CustomSelectField.js +3 -1
- package/dist/CustomSelectField.js.map +1 -1
- package/dist/DataTable.js +1 -1
- package/dist/DataTable.js.map +1 -1
- package/dist/DateTimeActionSheet.js +2 -1
- package/dist/DateTimeActionSheet.js.map +1 -1
- package/dist/DateTimeField.js +3 -2
- package/dist/DateTimeField.js.map +1 -1
- package/dist/DateUtilities.d.ts +25 -25
- package/dist/DateUtilities.js +31 -32
- package/dist/DateUtilities.js.map +1 -1
- package/dist/HeightField.js.map +1 -1
- package/dist/Hyperlink.js +19 -9
- package/dist/Hyperlink.js.map +1 -1
- package/dist/IconButton.js.map +1 -1
- package/dist/ImageBackground.d.ts +2 -5
- package/dist/ImageBackground.js +1 -1
- package/dist/ImageBackground.js.map +1 -1
- package/dist/MediaQuery.d.ts +4 -4
- package/dist/MediaQuery.js +8 -8
- package/dist/MediaQuery.js.map +1 -1
- package/dist/ModalSheet.d.ts +3 -2
- package/dist/ModalSheet.js +1 -1
- package/dist/ModalSheet.js.map +1 -1
- package/dist/OfflineBanner.d.ts +21 -0
- package/dist/OfflineBanner.js +25 -0
- package/dist/OfflineBanner.js.map +1 -0
- package/dist/OpenAPIContext.js +1 -1
- package/dist/OpenAPIContext.js.map +1 -1
- package/dist/Page.d.ts +1 -0
- package/dist/Page.js +7 -2
- package/dist/Page.js.map +1 -1
- package/dist/Pagination.js.map +1 -1
- package/dist/Permissions.js +3 -0
- package/dist/Permissions.js.map +1 -1
- package/dist/PickerSelect.d.ts +1 -1
- package/dist/PickerSelect.js +9 -6
- package/dist/PickerSelect.js.map +1 -1
- package/dist/SelectField.js +1 -1
- package/dist/SelectField.js.map +1 -1
- package/dist/SplitPage.js +7 -2
- package/dist/SplitPage.js.map +1 -1
- package/dist/SplitPage.native.js +4 -1
- package/dist/SplitPage.native.js.map +1 -1
- package/dist/TapToEdit.d.ts +1 -1
- package/dist/TapToEdit.js +12 -14
- package/dist/TapToEdit.js.map +1 -1
- package/dist/Toast.js.map +1 -1
- package/dist/ToastNotifications.js +2 -2
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/Tooltip.d.ts +24 -1
- package/dist/Tooltip.js +2 -2
- package/dist/Tooltip.js.map +1 -1
- package/dist/Unifier.d.ts +3 -3
- package/dist/Unifier.js +15 -12
- package/dist/Unifier.js.map +1 -1
- package/dist/Utilities.d.ts +12 -8
- package/dist/Utilities.js +13 -15
- package/dist/Utilities.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/signUp/PasswordRequirements.js +3 -3
- package/dist/signUp/PasswordRequirements.js.map +1 -1
- package/dist/table/TableHeaderCell.js +1 -9
- package/dist/table/TableHeaderCell.js.map +1 -1
- package/dist/table/tableContext.d.ts +1 -1
- package/dist/table/tableContext.js +2 -2
- package/dist/table/tableContext.js.map +1 -1
- package/package.json +2 -1
- package/src/ActionSheet.test.tsx +1 -0
- package/src/ActionSheet.tsx +8 -6
- package/src/Avatar.tsx +9 -2
- package/src/Badge.test.tsx +1 -0
- package/src/Banner.test.tsx +71 -0
- package/src/Banner.tsx +1 -1
- package/src/Box.test.tsx +1 -0
- package/src/Box.tsx +10 -6
- package/src/Button.test.tsx +35 -0
- package/src/Button.tsx +65 -34
- package/src/Common.ts +42 -19
- package/src/ConsentFormScreen.test.tsx +124 -0
- package/src/ConsentFormScreen.tsx +18 -6
- package/src/ConsentNavigator.test.tsx +1 -0
- package/src/ConsentNavigator.tsx +5 -3
- package/src/CustomSelectField.tsx +3 -1
- package/src/DataTable.test.tsx +218 -0
- package/src/DataTable.tsx +1 -1
- package/src/DateTimeActionSheet.tsx +7 -3
- package/src/DateTimeField.test.tsx +1 -0
- package/src/DateTimeField.tsx +3 -2
- package/src/DateUtilities.test.ts +111 -0
- package/src/DateUtilities.tsx +43 -44
- package/src/DecimalRangeActionSheet.test.tsx +28 -0
- package/src/ErrorBoundary.test.tsx +1 -0
- package/src/HeightActionSheet.test.tsx +16 -0
- package/src/HeightField.test.tsx +106 -1
- package/src/HeightField.tsx +2 -1
- package/src/Hyperlink.tsx +83 -52
- package/src/IconButton.tsx +1 -1
- package/src/ImageBackground.tsx +5 -6
- package/src/MediaQuery.ts +8 -8
- package/src/MobileAddressAutoComplete.test.tsx +20 -1
- package/src/ModalSheet.test.tsx +1 -5
- package/src/ModalSheet.tsx +15 -6
- package/src/NumberField.test.tsx +14 -0
- package/src/OfflineBanner.test.tsx +70 -0
- package/src/OfflineBanner.tsx +54 -0
- package/src/OpenAPIContext.tsx +3 -2
- package/src/Page.test.tsx +28 -0
- package/src/Page.tsx +18 -2
- package/src/Pagination.tsx +1 -1
- package/src/Permissions.ts +3 -0
- package/src/PickerSelect.tsx +20 -17
- package/src/SelectBadge.test.tsx +1 -0
- package/src/SelectField.tsx +1 -1
- package/src/Signature.test.tsx +1 -0
- package/src/SplitPage.native.tsx +2 -0
- package/src/SplitPage.tsx +6 -1
- package/src/TapToEdit.test.tsx +48 -0
- package/src/TapToEdit.tsx +13 -14
- package/src/Toast.tsx +1 -1
- package/src/ToastNotifications.test.tsx +738 -0
- package/src/ToastNotifications.tsx +3 -6
- package/src/Tooltip.test.tsx +586 -8
- package/src/Tooltip.tsx +2 -2
- package/src/Unifier.ts +20 -16
- package/src/Utilities.tsx +20 -19
- package/src/WebAddressAutocomplete.test.tsx +138 -0
- package/src/WebDropdownMenu.test.tsx +23 -0
- package/src/__snapshots__/AddressField.test.tsx.snap +3 -1
- package/src/__snapshots__/Button.test.tsx.snap +92 -50
- package/src/__snapshots__/CustomSelectField.test.tsx.snap +21 -7
- package/src/__snapshots__/DecimalRangeActionSheet.test.tsx.snap +14 -8
- package/src/__snapshots__/ErrorPage.test.tsx.snap +7 -4
- package/src/__snapshots__/Field.test.tsx.snap +18 -6
- package/src/__snapshots__/HeightActionSheet.test.tsx.snap +14 -8
- package/src/__snapshots__/HeightField.test.tsx.snap +35 -20
- package/src/__snapshots__/InfoModalIcon.test.tsx.snap +28 -16
- package/src/__snapshots__/Modal.test.tsx.snap +19 -10
- package/src/__snapshots__/ModalSheet.test.tsx.snap +0 -1
- package/src/__snapshots__/NumberPickerActionSheet.test.tsx.snap +14 -8
- package/src/__snapshots__/Page.test.tsx.snap +7 -4
- package/src/__snapshots__/SelectField.test.tsx.snap +18 -6
- package/src/__snapshots__/TerrenoProvider.test.tsx.snap +0 -2
- package/src/__snapshots__/TimezonePicker.test.tsx.snap +18 -6
- package/src/bunSetup.ts +25 -2
- package/src/index.tsx +2 -1
- package/src/login/LoginScreen.test.tsx +23 -1
- package/src/login/__snapshots__/LoginScreen.test.tsx.snap +15 -6
- package/src/signUp/PasswordRequirements.tsx +9 -6
- package/src/signUp/__snapshots__/PasswordRequirements.test.tsx.snap +50 -2
- package/src/signUp/__snapshots__/SignUpScreen.test.tsx.snap +35 -5
- package/src/table/TableHeaderCell.tsx +8 -11
- package/src/table/TableRow.test.tsx +31 -1
- package/src/table/__snapshots__/TableBadge.test.tsx.snap +3 -1
- package/src/table/__snapshots__/TableHeaderCell.test.tsx.snap +2 -0
- package/src/table/tableContext.tsx +2 -2
- package/src/types/react-native-swiper-flatlist.d.ts +1 -0
- package/src/useStoredState.test.tsx +47 -0
package/src/DataTable.test.tsx
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
1
2
|
import {describe, expect, it, mock} from "bun:test";
|
|
3
|
+
import {act} from "@testing-library/react-native";
|
|
2
4
|
|
|
3
5
|
import {DataTable} from "./DataTable";
|
|
4
6
|
import {Text} from "./Text";
|
|
@@ -130,4 +132,220 @@ describe("DataTable", () => {
|
|
|
130
132
|
const {toJSON} = renderWithTheme(<DataTable columns={sampleColumns} data={[]} />);
|
|
131
133
|
expect(toJSON()).toMatchSnapshot();
|
|
132
134
|
});
|
|
135
|
+
|
|
136
|
+
it("handles sort cycling: none -> asc -> desc -> none", () => {
|
|
137
|
+
const sortableColumns = [
|
|
138
|
+
{columnType: "text", sortable: true, title: "Name", width: 150},
|
|
139
|
+
{columnType: "text", sortable: false, title: "Age", width: 100},
|
|
140
|
+
];
|
|
141
|
+
const setSortColumn = mock((_sort?: {column: number; direction: string}) => {});
|
|
142
|
+
const {UNSAFE_getAllByType} = renderWithTheme(
|
|
143
|
+
<DataTable
|
|
144
|
+
columns={sortableColumns}
|
|
145
|
+
data={[[{value: "Alice"}, {value: "28"}]]}
|
|
146
|
+
setSortColumn={setSortColumn}
|
|
147
|
+
/>
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
const {Pressable: PressableComp} = require("react-native");
|
|
151
|
+
const pressables = UNSAFE_getAllByType(PressableComp);
|
|
152
|
+
// Find the sort pressable (has hitSlop=16)
|
|
153
|
+
const sortPressable = pressables.find(
|
|
154
|
+
(p: {props: {hitSlop?: number}}) => p.props.hitSlop === 16
|
|
155
|
+
);
|
|
156
|
+
expect(sortPressable).toBeTruthy();
|
|
157
|
+
|
|
158
|
+
const {fireEvent} = require("@testing-library/react-native");
|
|
159
|
+
// First click: none -> asc
|
|
160
|
+
fireEvent.press(sortPressable!);
|
|
161
|
+
expect(setSortColumn).toHaveBeenCalledWith({column: 0, direction: "asc"});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("handles sort from asc to desc", () => {
|
|
165
|
+
const sortableColumns = [{columnType: "text", sortable: true, title: "Name", width: 150}];
|
|
166
|
+
const setSortColumn = mock((_sort?: {column: number; direction: string}) => {});
|
|
167
|
+
const {UNSAFE_getAllByType} = renderWithTheme(
|
|
168
|
+
<DataTable
|
|
169
|
+
columns={sortableColumns}
|
|
170
|
+
data={[[{value: "Alice"}]]}
|
|
171
|
+
setSortColumn={setSortColumn}
|
|
172
|
+
sortColumn={{column: 0, direction: "asc"}}
|
|
173
|
+
/>
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const {Pressable: PressableComp} = require("react-native");
|
|
177
|
+
const pressables = UNSAFE_getAllByType(PressableComp);
|
|
178
|
+
const sortPressable = pressables.find(
|
|
179
|
+
(p: {props: {hitSlop?: number}}) => p.props.hitSlop === 16
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
const {fireEvent} = require("@testing-library/react-native");
|
|
183
|
+
fireEvent.press(sortPressable!);
|
|
184
|
+
expect(setSortColumn).toHaveBeenCalledWith({column: 0, direction: "desc"});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("handles sort from desc to none", () => {
|
|
188
|
+
const sortableColumns = [{columnType: "text", sortable: true, title: "Name", width: 150}];
|
|
189
|
+
const setSortColumn = mock((_sort?: {column: number; direction: string}) => {});
|
|
190
|
+
const {UNSAFE_getAllByType} = renderWithTheme(
|
|
191
|
+
<DataTable
|
|
192
|
+
columns={sortableColumns}
|
|
193
|
+
data={[[{value: "Alice"}]]}
|
|
194
|
+
setSortColumn={setSortColumn}
|
|
195
|
+
sortColumn={{column: 0, direction: "desc"}}
|
|
196
|
+
/>
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const {Pressable: PressableComp} = require("react-native");
|
|
200
|
+
const pressables = UNSAFE_getAllByType(PressableComp);
|
|
201
|
+
const sortPressable = pressables.find(
|
|
202
|
+
(p: {props: {hitSlop?: number}}) => p.props.hitSlop === 16
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const {fireEvent} = require("@testing-library/react-native");
|
|
206
|
+
fireEvent.press(sortPressable!);
|
|
207
|
+
expect(setSortColumn).toHaveBeenCalledWith(undefined);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("handles sort on non-sortable column (no-op)", () => {
|
|
211
|
+
const columns = [{columnType: "text", sortable: false, title: "Name", width: 150}];
|
|
212
|
+
const setSortColumn = mock(() => {});
|
|
213
|
+
renderWithTheme(
|
|
214
|
+
<DataTable columns={columns} data={[[{value: "Alice"}]]} setSortColumn={setSortColumn} />
|
|
215
|
+
);
|
|
216
|
+
// No sort pressable rendered for non-sortable columns, so no action needed
|
|
217
|
+
expect(setSortColumn).not.toHaveBeenCalled();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("syncs scroll between header and body via refs", () => {
|
|
221
|
+
const {UNSAFE_getAllByType} = renderWithTheme(
|
|
222
|
+
<DataTable columns={sampleColumns} data={sampleData} pinnedColumns={1} />
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const {ScrollView: ScrollViewComp} = require("react-native");
|
|
226
|
+
const scrollViews = UNSAFE_getAllByType(ScrollViewComp);
|
|
227
|
+
expect(scrollViews.length).toBeGreaterThan(0);
|
|
228
|
+
|
|
229
|
+
// Inject mock scrollTo on the refs so handleScroll branches execute
|
|
230
|
+
const mockScrollTo = mock((_opts: {animated: boolean; x: number}) => {});
|
|
231
|
+
for (const sv of scrollViews) {
|
|
232
|
+
if (sv.props.horizontal) {
|
|
233
|
+
const fiber = (sv as unknown as {_fiber?: {ref?: {current: unknown}}})._fiber;
|
|
234
|
+
if (fiber?.ref && typeof fiber.ref === "object") {
|
|
235
|
+
fiber.ref.current = {scrollTo: mockScrollTo};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const {fireEvent} = require("@testing-library/react-native");
|
|
241
|
+
// Find header scroll (onScroll passes isHeader=true)
|
|
242
|
+
const headerScroll = scrollViews.find(
|
|
243
|
+
(sv: {props: {horizontal?: boolean; showsHorizontalScrollIndicator?: boolean}}) =>
|
|
244
|
+
sv.props.horizontal && sv.props.showsHorizontalScrollIndicator === false
|
|
245
|
+
);
|
|
246
|
+
// Find body scroll (onScroll passes isHeader=false)
|
|
247
|
+
const bodyScroll = scrollViews.find(
|
|
248
|
+
(sv: {props: {horizontal?: boolean; showsHorizontalScrollIndicator?: boolean}}) =>
|
|
249
|
+
sv.props.horizontal && sv.props.showsHorizontalScrollIndicator === true
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
if (headerScroll) {
|
|
253
|
+
fireEvent.scroll(headerScroll, {
|
|
254
|
+
nativeEvent: {contentOffset: {x: 50, y: 0}},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
if (bodyScroll) {
|
|
258
|
+
fireEvent.scroll(bodyScroll, {
|
|
259
|
+
nativeEvent: {contentOffset: {x: 75, y: 0}},
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
expect(mockScrollTo).toHaveBeenCalled();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("renders with custom column component map", () => {
|
|
267
|
+
const CustomComponent = ({cellData}: {cellData: {value: unknown}}) => (
|
|
268
|
+
<Text>Custom: {String(cellData.value)}</Text>
|
|
269
|
+
);
|
|
270
|
+
const customColumns = [{columnType: "custom", title: "Custom Col", width: 150}];
|
|
271
|
+
const customData = [[{value: "test"}]];
|
|
272
|
+
const {getByText} = renderWithTheme(
|
|
273
|
+
<DataTable
|
|
274
|
+
columns={customColumns}
|
|
275
|
+
customColumnComponentMap={{custom: CustomComponent as any}}
|
|
276
|
+
data={customData}
|
|
277
|
+
/>
|
|
278
|
+
);
|
|
279
|
+
expect(getByText("Custom: test")).toBeTruthy();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("renders with infoModalText on column header", () => {
|
|
283
|
+
const columnsWithInfo = [
|
|
284
|
+
{columnType: "text", infoModalText: "**Help text**", title: "Name", width: 150},
|
|
285
|
+
];
|
|
286
|
+
const {toJSON} = renderWithTheme(
|
|
287
|
+
<DataTable columns={columnsWithInfo} data={[[{value: "Alice"}]]} />
|
|
288
|
+
);
|
|
289
|
+
expect(toJSON()).toBeTruthy();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("renders with cell highlight", () => {
|
|
293
|
+
const highlightData = [[{highlight: "primary", value: "Highlighted"}]];
|
|
294
|
+
const {toJSON} = renderWithTheme(
|
|
295
|
+
<DataTable columns={[{columnType: "text", title: "Name", width: 150}]} data={highlightData} />
|
|
296
|
+
);
|
|
297
|
+
expect(toJSON()).toBeTruthy();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it("renders with moreContentExtraData", () => {
|
|
301
|
+
const MoreContent = ({rowIndex, extraInfo}: {rowIndex: number; extraInfo?: string}) => (
|
|
302
|
+
<Text>
|
|
303
|
+
Row {rowIndex}: {extraInfo}
|
|
304
|
+
</Text>
|
|
305
|
+
);
|
|
306
|
+
const {toJSON} = renderWithTheme(
|
|
307
|
+
<DataTable
|
|
308
|
+
columns={sampleColumns}
|
|
309
|
+
data={sampleData}
|
|
310
|
+
moreContentComponent={MoreContent as any}
|
|
311
|
+
moreContentExtraData={[{extraInfo: "info1"}, {extraInfo: "info2"}, {extraInfo: "info3"}]}
|
|
312
|
+
/>
|
|
313
|
+
);
|
|
314
|
+
expect(toJSON()).toBeTruthy();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("opens and dismisses more content modal via MoreButtonCell press", async () => {
|
|
318
|
+
const MoreContent = ({rowIndex}: {rowIndex: number}) => <Text>Detail for row {rowIndex}</Text>;
|
|
319
|
+
const {UNSAFE_getAllByType} = renderWithTheme(
|
|
320
|
+
<DataTable
|
|
321
|
+
columns={sampleColumns}
|
|
322
|
+
data={sampleData}
|
|
323
|
+
moreContentComponent={MoreContent as any}
|
|
324
|
+
/>
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
const {Pressable: PressableComp} = require("react-native");
|
|
328
|
+
const {fireEvent} = require("@testing-library/react-native");
|
|
329
|
+
const pressables = UNSAFE_getAllByType(PressableComp);
|
|
330
|
+
|
|
331
|
+
// Find the info/eye icon pressable (MoreButtonCell has accessibilityHint="View details")
|
|
332
|
+
const moreBtn = pressables.find(
|
|
333
|
+
(p: {props: {accessibilityHint?: string}}) => p.props.accessibilityHint === "View details"
|
|
334
|
+
);
|
|
335
|
+
expect(moreBtn).toBeTruthy();
|
|
336
|
+
|
|
337
|
+
// Press to open modal
|
|
338
|
+
await act(async () => {
|
|
339
|
+
fireEvent.press(moreBtn!);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
// Find the modal dismiss pressable and press it
|
|
343
|
+
const {Modal: ModalComp} = require("./Modal");
|
|
344
|
+
const modals = UNSAFE_getAllByType(ModalComp);
|
|
345
|
+
if (modals.length > 0 && modals[0].props.onDismiss) {
|
|
346
|
+
await act(async () => {
|
|
347
|
+
modals[0].props.onDismiss();
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
});
|
|
133
351
|
});
|
package/src/DataTable.tsx
CHANGED
|
@@ -264,7 +264,7 @@ const DataTableHeaderCell: FC<DataTableHeaderCellProps> = ({
|
|
|
264
264
|
}}
|
|
265
265
|
>
|
|
266
266
|
{[
|
|
267
|
-
|
|
267
|
+
column.title ? (
|
|
268
268
|
<TableTitle align="left" key="data-table-header-title" title={column.title!} />
|
|
269
269
|
) : null,
|
|
270
270
|
<View key="data-table-header-tools" style={{alignItems: "center", flexDirection: "row"}}>
|
|
@@ -369,7 +369,11 @@ const DateCalendar = ({
|
|
|
369
369
|
const {theme} = useTheme();
|
|
370
370
|
|
|
371
371
|
const markedDates: {
|
|
372
|
-
[id: string]: {
|
|
372
|
+
[id: string]: {
|
|
373
|
+
selected: boolean;
|
|
374
|
+
selectedColor: string;
|
|
375
|
+
customStyles?: {container?: {backgroundColor?: string; borderRadius?: number}};
|
|
376
|
+
};
|
|
373
377
|
} = {};
|
|
374
378
|
|
|
375
379
|
// Check if the date is T00:00:00.000Z (it should be), otherwise treat it as a date in the
|
|
@@ -471,7 +475,7 @@ export const DateTimeActionSheet = ({
|
|
|
471
475
|
|
|
472
476
|
// If the value changes in the props, update the state for the date and time.
|
|
473
477
|
useEffect(() => {
|
|
474
|
-
let datetime;
|
|
478
|
+
let datetime: DateTime;
|
|
475
479
|
if (value) {
|
|
476
480
|
if (type === "date") {
|
|
477
481
|
datetime = DateTime.fromISO(value).toUTC().set({millisecond: 0, second: 0});
|
|
@@ -495,7 +499,7 @@ export const DateTimeActionSheet = ({
|
|
|
495
499
|
setHour(h);
|
|
496
500
|
setMinute(datetime.minute);
|
|
497
501
|
setAmPm(datetime.toFormat("a") === "AM" ? "am" : "pm");
|
|
498
|
-
setDate(datetime.toISO());
|
|
502
|
+
setDate(datetime.toISO() ?? "");
|
|
499
503
|
// Reset timezone when the sent date changes.
|
|
500
504
|
setTimezone(originalTimezone);
|
|
501
505
|
}, [value, originalTimezone, type]);
|
package/src/DateTimeField.tsx
CHANGED
|
@@ -418,6 +418,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
|
|
|
418
418
|
helperText,
|
|
419
419
|
}): React.ReactElement => {
|
|
420
420
|
const {theme} = useTheme();
|
|
421
|
+
// biome-ignore lint/suspicious/noExplicitAny: ActionSheet class is defined in ActionSheet.tsx which imports from Common.ts indirectly; using its type here would create a circular dependency
|
|
421
422
|
const dateActionSheetRef: React.RefObject<any> = React.createRef();
|
|
422
423
|
const [amPm, setAmPm] = useState<"am" | "pm">("am");
|
|
423
424
|
const [showDate, setShowDate] = useState(false);
|
|
@@ -567,7 +568,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
|
|
|
567
568
|
const dayVal = override?.day ?? day;
|
|
568
569
|
const yearVal = override?.year ?? year;
|
|
569
570
|
const hourVal = override?.hour ?? hour;
|
|
570
|
-
let date;
|
|
571
|
+
let date: DateTime;
|
|
571
572
|
if (type === "datetime") {
|
|
572
573
|
if (!monthVal || !dayVal || !yearVal || !hour || !minuteVal) {
|
|
573
574
|
return undefined;
|
|
@@ -635,7 +636,7 @@ export const DateTimeField: FC<DateTimeFieldProps> = ({
|
|
|
635
636
|
|
|
636
637
|
if (date.isValid) {
|
|
637
638
|
// Always return UTC ISO string
|
|
638
|
-
return date.toUTC().toISO();
|
|
639
|
+
return date.toUTC().toISO() ?? undefined;
|
|
639
640
|
}
|
|
640
641
|
return undefined;
|
|
641
642
|
},
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it, mock} from "bun:test";
|
|
2
2
|
import {
|
|
3
|
+
convertNullToUndefined,
|
|
4
|
+
getIsoDate,
|
|
5
|
+
getTimezoneOptions,
|
|
3
6
|
humanDate,
|
|
4
7
|
humanDateAndTime,
|
|
5
8
|
printDate,
|
|
@@ -454,5 +457,113 @@ describe("DateUtilities", () => {
|
|
|
454
457
|
// print without ago
|
|
455
458
|
expect(printSince("2019-12-23T11:00:00.000Z", {showAgo: false})).toBe("3 years");
|
|
456
459
|
});
|
|
460
|
+
|
|
461
|
+
it("should throw for invalid date", () => {
|
|
462
|
+
expect(() => printSince("not-a-date")).toThrow("printSince: Invalid date: not-a-date");
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
describe("humanDate – non-string input", () => {
|
|
467
|
+
it("should throw for non-string date", () => {
|
|
468
|
+
expect(() => humanDate(123 as unknown as string)).toThrow(
|
|
469
|
+
"humanDate: Invalid date type: number"
|
|
470
|
+
);
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
describe("humanDateAndTime – showTimezone false", () => {
|
|
475
|
+
it("should format time without timezone abbreviation", () => {
|
|
476
|
+
const result = humanDateAndTime("2022-12-24T12:00:00.000Z", {showTimezone: false});
|
|
477
|
+
expect(result).toBe("7:00 AM");
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it("should format tomorrow without timezone abbreviation", () => {
|
|
481
|
+
const result = humanDateAndTime("2022-12-25T12:00:00.000Z", {showTimezone: false});
|
|
482
|
+
expect(result).toBe("Tomorrow 7:00 AM");
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
describe("printDate – showTimezone warning", () => {
|
|
487
|
+
it("should still return the date when showTimezone is true", () => {
|
|
488
|
+
const result = printDate("2022-12-24T12:00:00.000Z", {showTimezone: true});
|
|
489
|
+
expect(result).toBe("12/24/2022");
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
describe("printDateRange – timeOnly with different dates", () => {
|
|
494
|
+
it("should warn but still return time range when dates differ", () => {
|
|
495
|
+
const result = printDateRange("2022-12-24T12:00:00.000Z", "2022-12-25T18:00:00.000Z", {
|
|
496
|
+
timeOnly: true,
|
|
497
|
+
timezone: "America/New_York",
|
|
498
|
+
});
|
|
499
|
+
expect(result).toBe("7:00 AM - 1:00 PM EST");
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
describe("convertNullToUndefined", () => {
|
|
504
|
+
it("should return the string when given a string", () => {
|
|
505
|
+
expect(convertNullToUndefined("hello")).toBe("hello");
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
it("should return undefined when given null", () => {
|
|
509
|
+
expect(convertNullToUndefined(null)).toBeUndefined();
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
describe("getIsoDate", () => {
|
|
514
|
+
it("should return undefined for undefined input", () => {
|
|
515
|
+
expect(getIsoDate(undefined)).toBeUndefined();
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
it("should return undefined for empty string", () => {
|
|
519
|
+
expect(getIsoDate("")).toBeUndefined();
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
it("should return ISO string for valid date", () => {
|
|
523
|
+
const result = getIsoDate("2022-12-24T12:00:00.000Z");
|
|
524
|
+
expect(result).toBe("2022-12-24T12:00:00.000Z");
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
describe("humanDate – non-Error thrown", () => {
|
|
529
|
+
it("should stringify a non-Error value thrown during date parsing", () => {
|
|
530
|
+
expect(() => humanDate(42 as unknown as string)).toThrow(
|
|
531
|
+
"humanDate: Invalid date type: number"
|
|
532
|
+
);
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
describe("getTimezoneOptions", () => {
|
|
537
|
+
it("returns US timezone options with full labels", () => {
|
|
538
|
+
const options = getTimezoneOptions("USA");
|
|
539
|
+
expect(options.length).toBe(7);
|
|
540
|
+
const labels = options.map((o) => o.label);
|
|
541
|
+
expect(labels).toContain("Eastern");
|
|
542
|
+
expect(labels).toContain("Pacific");
|
|
543
|
+
expect(labels).toContain("Arizona");
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it("returns US timezone options with short labels", () => {
|
|
547
|
+
const options = getTimezoneOptions("USA", true);
|
|
548
|
+
expect(options.length).toBe(7);
|
|
549
|
+
const azOption = options.find((o) => o.value === "America/Phoenix");
|
|
550
|
+
expect(azOption?.label).toBe("AZ");
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("returns worldwide timezone options", () => {
|
|
554
|
+
const options = getTimezoneOptions("Worldwide");
|
|
555
|
+
expect(options.length).toBeGreaterThan(7);
|
|
556
|
+
const values = options.map((o) => o.value);
|
|
557
|
+
expect(values).toContain("America/New_York");
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it("returns worldwide timezone options with short labels", () => {
|
|
561
|
+
const options = getTimezoneOptions("Worldwide", true);
|
|
562
|
+
expect(options.length).toBeGreaterThan(7);
|
|
563
|
+
options.forEach((o) => {
|
|
564
|
+
expect(typeof o.label).toBe("string");
|
|
565
|
+
expect(typeof o.value).toBe("string");
|
|
566
|
+
});
|
|
567
|
+
});
|
|
457
568
|
});
|
|
458
569
|
});
|
package/src/DateUtilities.tsx
CHANGED
|
@@ -7,7 +7,7 @@ const getErrorMessage = (error: unknown): string => {
|
|
|
7
7
|
return String(error);
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const getDate = (date: string, {timezone}: {timezone?: string} = {}): DateTime => {
|
|
11
11
|
if (!date) {
|
|
12
12
|
throw new Error("Passed undefined");
|
|
13
13
|
}
|
|
@@ -19,49 +19,49 @@ function getDate(date: string, {timezone}: {timezone?: string} = {}): DateTime {
|
|
|
19
19
|
throw new Error(`Invalid date: ${date}`);
|
|
20
20
|
}
|
|
21
21
|
return clonedDate;
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
|
|
24
|
-
export
|
|
24
|
+
export const isTomorrow = (date: string, {timezone}: {timezone?: string} = {}): boolean => {
|
|
25
25
|
const clonedDate = getDate(date, {timezone});
|
|
26
26
|
const now = timezone ? DateTime.now().setZone(timezone) : DateTime.now();
|
|
27
27
|
const diff = now.startOf("day").diff(clonedDate.startOf("day"), "days");
|
|
28
28
|
return diff.days <= -1 && diff.days > -2;
|
|
29
|
-
}
|
|
29
|
+
};
|
|
30
30
|
|
|
31
|
-
export
|
|
31
|
+
export const isYesterday = (date: string, {timezone}: {timezone?: string} = {}): boolean => {
|
|
32
32
|
const clonedDate = getDate(date, {timezone});
|
|
33
33
|
const now = timezone ? DateTime.now().setZone(timezone) : DateTime.now();
|
|
34
34
|
const diff = now.startOf("day").diff(clonedDate.startOf("day"), "days");
|
|
35
35
|
return diff.days <= 1 && diff.days > -1;
|
|
36
|
-
}
|
|
36
|
+
};
|
|
37
37
|
|
|
38
|
-
export
|
|
38
|
+
export const isToday = (date: string, {timezone}: {timezone?: string} = {}): boolean => {
|
|
39
39
|
const clonedDate = getDate(date, {timezone});
|
|
40
40
|
const now = timezone ? DateTime.now().setZone(timezone) : DateTime.now();
|
|
41
41
|
const diff = now.startOf("day").diff(clonedDate.startOf("day"), "days");
|
|
42
42
|
return diff.days === 0;
|
|
43
|
-
}
|
|
43
|
+
};
|
|
44
44
|
|
|
45
|
-
export
|
|
45
|
+
export const isThisYear = (date: string, {timezone}: {timezone?: string} = {}): boolean => {
|
|
46
46
|
const clonedDate = getDate(date, {timezone});
|
|
47
47
|
const now = timezone ? DateTime.now().setZone(timezone) : DateTime.now();
|
|
48
48
|
return clonedDate.year === now.year;
|
|
49
|
-
}
|
|
49
|
+
};
|
|
50
50
|
|
|
51
|
-
export
|
|
51
|
+
export const isWithinWeek = (date: string, {timezone}: {timezone?: string} = {}): boolean => {
|
|
52
52
|
const clonedDate = getDate(date, {timezone});
|
|
53
53
|
const now = timezone ? DateTime.now().setZone(timezone) : DateTime.now();
|
|
54
54
|
const diff = now.startOf("day").diff(clonedDate.startOf("day"), "days");
|
|
55
55
|
return diff.days > -7 && diff.days < 7;
|
|
56
|
-
}
|
|
56
|
+
};
|
|
57
57
|
|
|
58
58
|
// Prints a human friendly date, e.g. "Tomorrow", "Yesterday", "Monday", "June 19", "December 25,
|
|
59
59
|
// 2022".
|
|
60
|
-
export
|
|
60
|
+
export const humanDate = (
|
|
61
61
|
date: string,
|
|
62
62
|
{timezone, dontShowTime}: {timezone?: string; dontShowTime?: boolean} = {}
|
|
63
|
-
): string {
|
|
64
|
-
let clonedDate;
|
|
63
|
+
): string => {
|
|
64
|
+
let clonedDate: DateTime;
|
|
65
65
|
try {
|
|
66
66
|
clonedDate = getDate(date, {timezone});
|
|
67
67
|
} catch (error: unknown) {
|
|
@@ -87,15 +87,15 @@ export function humanDate(
|
|
|
87
87
|
// December 25, 2022
|
|
88
88
|
return clonedDate.toFormat("MMM d, yyyy");
|
|
89
89
|
}
|
|
90
|
-
}
|
|
90
|
+
};
|
|
91
91
|
|
|
92
92
|
// Prints a human friendly date and time, e.g. "Tomorrow 9:00 AM", "Yesterday 9:00 AM", "Monday
|
|
93
93
|
// 9:00 AM", "June 19 9:00 AM", "December 25, 2022 9:00 AM".
|
|
94
|
-
export
|
|
94
|
+
export const humanDateAndTime = (
|
|
95
95
|
date: string,
|
|
96
96
|
{timezone, showTimezone = true}: {timezone?: string; showTimezone?: boolean} = {}
|
|
97
|
-
): string {
|
|
98
|
-
let clonedDate;
|
|
97
|
+
): string => {
|
|
98
|
+
let clonedDate: DateTime;
|
|
99
99
|
try {
|
|
100
100
|
clonedDate = getDate(date, {timezone});
|
|
101
101
|
} catch (error: unknown) {
|
|
@@ -128,7 +128,7 @@ export function humanDateAndTime(
|
|
|
128
128
|
// December 25, 2022
|
|
129
129
|
return `${clonedDate.toFormat("MMM d, yyyy")} ${time}`;
|
|
130
130
|
}
|
|
131
|
-
}
|
|
131
|
+
};
|
|
132
132
|
|
|
133
133
|
// Print date in the format of M/D/YY, taking timezones into account.
|
|
134
134
|
export const printDate = (
|
|
@@ -162,7 +162,7 @@ export const printDate = (
|
|
|
162
162
|
return justDate.startOf("day").toFormat("M/d/yyyy");
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
let clonedDate;
|
|
165
|
+
let clonedDate: DateTime;
|
|
166
166
|
try {
|
|
167
167
|
clonedDate = getDate(date, {timezone});
|
|
168
168
|
} catch (error: unknown) {
|
|
@@ -199,7 +199,7 @@ export const printOnlyDate = (
|
|
|
199
199
|
};
|
|
200
200
|
|
|
201
201
|
// Print time in the format of HH:mm A, taking timezones into account.
|
|
202
|
-
export
|
|
202
|
+
export const printTime = (
|
|
203
203
|
date?: string,
|
|
204
204
|
{
|
|
205
205
|
timezone,
|
|
@@ -210,11 +210,11 @@ export function printTime(
|
|
|
210
210
|
showTimezone?: boolean;
|
|
211
211
|
defaultValue?: string;
|
|
212
212
|
} = {defaultValue: "Invalid Date", timezone: "America/New_York"}
|
|
213
|
-
): string {
|
|
213
|
+
): string => {
|
|
214
214
|
if (!date) {
|
|
215
215
|
return defaultValue ?? "Invalid Date";
|
|
216
216
|
}
|
|
217
|
-
let clonedDate;
|
|
217
|
+
let clonedDate: DateTime;
|
|
218
218
|
if (!timezone) {
|
|
219
219
|
throw new Error("printTime: timezone is required");
|
|
220
220
|
}
|
|
@@ -232,10 +232,10 @@ export function printTime(
|
|
|
232
232
|
} else {
|
|
233
233
|
return clonedDate.toLocaleString(DateTime.TIME_SIMPLE);
|
|
234
234
|
}
|
|
235
|
-
}
|
|
235
|
+
};
|
|
236
236
|
|
|
237
237
|
// Print date in the format of M/D/YY HH:mm A, taking timezones into account.
|
|
238
|
-
export
|
|
238
|
+
export const printDateAndTime = (
|
|
239
239
|
date?: string,
|
|
240
240
|
{
|
|
241
241
|
timezone,
|
|
@@ -246,11 +246,11 @@ export function printDateAndTime(
|
|
|
246
246
|
showTimezone?: boolean;
|
|
247
247
|
defaultValue?: string;
|
|
248
248
|
} = {}
|
|
249
|
-
): string {
|
|
249
|
+
): string => {
|
|
250
250
|
if (!date) {
|
|
251
251
|
return defaultValue ?? "Invalid Datetime";
|
|
252
252
|
}
|
|
253
|
-
let clonedDate;
|
|
253
|
+
let clonedDate: DateTime;
|
|
254
254
|
try {
|
|
255
255
|
clonedDate = getDate(date, {timezone});
|
|
256
256
|
} catch (error: unknown) {
|
|
@@ -268,12 +268,12 @@ export function printDateAndTime(
|
|
|
268
268
|
} else {
|
|
269
269
|
return clonedDate.toLocaleString(DateTime.DATETIME_SHORT);
|
|
270
270
|
}
|
|
271
|
-
}
|
|
271
|
+
};
|
|
272
272
|
|
|
273
273
|
// Prints a date range in the format of M/D/YY HH:mm A - M/D/YY HH:mm A EST, taking timezones into
|
|
274
274
|
// account. If the dates are the same, it will print the date only once, e.g. M/D/YY HH:mm A - HH:mm
|
|
275
275
|
// A EST.
|
|
276
|
-
export
|
|
276
|
+
export const printDateRange = (
|
|
277
277
|
start: string,
|
|
278
278
|
end: string,
|
|
279
279
|
{
|
|
@@ -281,7 +281,7 @@ export function printDateRange(
|
|
|
281
281
|
showTimezone = true,
|
|
282
282
|
timeOnly,
|
|
283
283
|
}: {timezone: string; showTimezone?: boolean; timeOnly?: boolean}
|
|
284
|
-
): string {
|
|
284
|
+
): string => {
|
|
285
285
|
const startDate = printDate(start, {showTimezone: false, timezone});
|
|
286
286
|
const endDate = printDate(end, {showTimezone: false, timezone});
|
|
287
287
|
|
|
@@ -299,15 +299,15 @@ export function printDateRange(
|
|
|
299
299
|
} else {
|
|
300
300
|
return `${startDate} ${startTime} - ${endDate} ${endTime}`;
|
|
301
301
|
}
|
|
302
|
-
}
|
|
302
|
+
};
|
|
303
303
|
|
|
304
304
|
// Print since nicely. If less than 2 months, print days, otherwise print months. If over 1 year,
|
|
305
305
|
// print years.
|
|
306
|
-
export
|
|
306
|
+
export const printSince = (
|
|
307
307
|
date: string,
|
|
308
308
|
{timezone, showAgo = true}: {timezone?: string; showAgo?: boolean} = {}
|
|
309
|
-
): string {
|
|
310
|
-
let clonedDate;
|
|
309
|
+
): string => {
|
|
310
|
+
let clonedDate: DateTime;
|
|
311
311
|
const ago = showAgo ? " ago" : "";
|
|
312
312
|
try {
|
|
313
313
|
clonedDate = getDate(date, {timezone});
|
|
@@ -326,20 +326,20 @@ export function printSince(
|
|
|
326
326
|
const years = Math.floor(now.diff(clonedDate, "years").years);
|
|
327
327
|
return `${years} ${years === 1 ? "year" : "years"}${ago}`;
|
|
328
328
|
}
|
|
329
|
-
}
|
|
329
|
+
};
|
|
330
330
|
|
|
331
|
-
export
|
|
331
|
+
export const convertNullToUndefined = (value: string | null): string | undefined => {
|
|
332
332
|
return value ?? undefined;
|
|
333
|
-
}
|
|
333
|
+
};
|
|
334
334
|
|
|
335
335
|
// Get the ISO date string from a date string. If the date string is undefined,
|
|
336
336
|
// return undefined instead of null so MongoDB can handle it.
|
|
337
|
-
export
|
|
337
|
+
export const getIsoDate = (date: string | undefined): string | undefined => {
|
|
338
338
|
if (!date) {
|
|
339
339
|
return undefined;
|
|
340
340
|
}
|
|
341
341
|
return convertNullToUndefined(DateTime.fromISO(date).toUTC().toISO());
|
|
342
|
-
}
|
|
342
|
+
};
|
|
343
343
|
|
|
344
344
|
const usTimezoneOptions = [
|
|
345
345
|
{label: "Eastern", value: "America/New_York"},
|
|
@@ -351,7 +351,7 @@ const usTimezoneOptions = [
|
|
|
351
351
|
{label: "Arizona", value: "America/Phoenix"},
|
|
352
352
|
];
|
|
353
353
|
|
|
354
|
-
export
|
|
354
|
+
export const getTimezoneOptions = (location: "USA" | "Worldwide", shortTimezone = false) => {
|
|
355
355
|
let timezones: [string, string][];
|
|
356
356
|
if (location === "USA") {
|
|
357
357
|
timezones = usTimezoneOptions.map((tz) => [tz.label, tz.value]);
|
|
@@ -366,9 +366,8 @@ export function getTimezoneOptions(location: "USA" | "Worldwide", shortTimezone
|
|
|
366
366
|
}
|
|
367
367
|
return timezones.map(([name, tz]) => {
|
|
368
368
|
const dateTime = DateTime.now().setZone(tz);
|
|
369
|
-
let tzAbbr = dateTime.toFormat("ZZZZ");
|
|
369
|
+
let tzAbbr = dateTime.toFormat("ZZZZ");
|
|
370
370
|
|
|
371
|
-
// Special case for Arizona which returns MST during standard time
|
|
372
371
|
if (tz === "America/Phoenix") {
|
|
373
372
|
tzAbbr = "AZ";
|
|
374
373
|
}
|
|
@@ -378,4 +377,4 @@ export function getTimezoneOptions(location: "USA" | "Worldwide", shortTimezone
|
|
|
378
377
|
value: tz,
|
|
379
378
|
};
|
|
380
379
|
});
|
|
381
|
-
}
|
|
380
|
+
};
|