@dloizides/ui-tables 1.10.0 → 1.11.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.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { memo, useState, useMemo, useCallback, useRef, useLayoutEffect, useEffect } from 'react';
2
- import { StyleSheet, View, Text, Platform, useWindowDimensions, Pressable } from 'react-native';
1
+ import React4, { memo, useState, useMemo, useCallback, useRef, useLayoutEffect, useEffect } from 'react';
2
+ import { StyleSheet, View, Text, Platform, useWindowDimensions, Pressable, Switch, TextInput } from 'react-native';
3
3
  import { useUi } from '@dloizides/ui-feedback';
4
4
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
5
5
  import { createPortal } from 'react-dom';
@@ -70,8 +70,10 @@ var TABLE_TEST_IDS = {
70
70
  results: "ui-results",
71
71
  pager: "ui-pager",
72
72
  pagerInfo: "ui-pager-info",
73
+ pagerFirst: "ui-pager-first",
73
74
  pagerPrev: "ui-pager-prev",
74
- pagerNext: "ui-pager-next"
75
+ pagerNext: "ui-pager-next",
76
+ pagerLast: "ui-pager-last"
75
77
  };
76
78
  var rowTestID = (tableTestID, key) => `${tableTestID}-${TABLE_TEST_IDS.rowInfix}-${key}`;
77
79
  var rowDetailTestID = (tableTestID, key) => `${tableTestID}-${TABLE_TEST_IDS.rowDetailInfix}-${key}`;
@@ -102,10 +104,14 @@ var TABLE_I18N = {
102
104
  selectClearHint: "uiTables.select.clearHint",
103
105
  results: "uiTables.results",
104
106
  pagerInfo: "uiTables.pager.info",
107
+ pagerFirst: "uiTables.pager.first",
105
108
  pagerPrev: "uiTables.pager.prev",
106
109
  pagerNext: "uiTables.pager.next",
110
+ pagerLast: "uiTables.pager.last",
111
+ pagerFirstHint: "uiTables.pager.firstHint",
107
112
  pagerPrevHint: "uiTables.pager.prevHint",
108
113
  pagerNextHint: "uiTables.pager.nextHint",
114
+ pagerLastHint: "uiTables.pager.lastHint",
109
115
  pagerRows: "uiTables.pager.rows",
110
116
  pagerRowsOptionHint: "uiTables.pager.rowsOptionHint",
111
117
  /** Accessible hint for the dropdown-variant rows-per-page trigger button. */
@@ -1087,11 +1093,9 @@ function SizeDropdown({
1087
1093
  ] });
1088
1094
  }
1089
1095
  var FIRST_PAGE = 1;
1090
- var EN_DASH = "\u2013";
1091
1096
  var DISABLED_OPACITY = 0.4;
1092
1097
  var FULL_OPACITY = 1;
1093
1098
  var TRANSPARENT2 = "transparent";
1094
- var BOLD = "700";
1095
1099
  var PAGER_HIT_SLOP = { top: 10, bottom: 10 };
1096
1100
  function NavButton({ label, hint, disabled, onPress, color, border, testID, styleOverrides: o }) {
1097
1101
  return /* @__PURE__ */ jsx(
@@ -1110,6 +1114,136 @@ function NavButton({ label, hint, disabled, onPress, color, border, testID, styl
1110
1114
  }
1111
1115
  );
1112
1116
  }
1117
+ function RowsControl({ pageSize, pageSizeOptions, onPageSizeChange, isDropdown, testID, styleOverrides: o }) {
1118
+ const { theme, t } = useUi();
1119
+ const { colors, palette } = theme;
1120
+ const brand = palette.primary["500"];
1121
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
1122
+ /* @__PURE__ */ jsx(Text, { style: [isDropdown ? chromeStyles.pagerRowsLabelPlain : chromeStyles.pagerRowsLabel, { color: colors.textSecondary }, o?.pagerRowsLabel], children: t(TABLE_I18N.pagerRows) }),
1123
+ isDropdown ? /* @__PURE__ */ jsx(
1124
+ SizeDropdown,
1125
+ {
1126
+ pageSize,
1127
+ pageSizeOptions,
1128
+ onPageSizeChange,
1129
+ testID,
1130
+ triggerHint: t(TABLE_I18N.pagerRowsTriggerHint),
1131
+ optionHint: t(TABLE_I18N.pagerRowsOptionHint),
1132
+ textColor: colors.text,
1133
+ mutedColor: colors.textSecondary,
1134
+ borderColor: colors.border,
1135
+ surfaceColor: colors.surface,
1136
+ brandColor: brand,
1137
+ triggerStyle: o?.sizePill,
1138
+ triggerTextStyle: o?.sizePillText
1139
+ }
1140
+ ) : /* @__PURE__ */ jsx(View, { style: [chromeStyles.sizeGroup, o?.sizeGroup], children: pageSizeOptions.map((size) => {
1141
+ const active = size === pageSize;
1142
+ return /* @__PURE__ */ jsx(
1143
+ Pressable,
1144
+ {
1145
+ testID: `${testID}-size-${size}`,
1146
+ accessibilityRole: "button",
1147
+ accessibilityLabel: String(size),
1148
+ accessibilityHint: t(TABLE_I18N.pagerRowsOptionHint),
1149
+ accessibilityState: { selected: active },
1150
+ hitSlop: PAGER_HIT_SLOP,
1151
+ onPress: () => onPageSizeChange(size),
1152
+ style: [chromeStyles.sizePill, { borderColor: active ? brand : colors.border, backgroundColor: active ? brand : TRANSPARENT2 }, o?.sizePill],
1153
+ children: /* @__PURE__ */ jsx(Text, { style: [chromeStyles.sizePillText, { color: active ? colors.surface : colors.textSecondary }, o?.sizePillText], children: size })
1154
+ },
1155
+ size
1156
+ );
1157
+ }) })
1158
+ ] });
1159
+ }
1160
+ function PagerNav({
1161
+ pageSize,
1162
+ pageSizeOptions,
1163
+ onPageSizeChange,
1164
+ onPageChange,
1165
+ safePage,
1166
+ lastPage,
1167
+ isCompact,
1168
+ isDropdown,
1169
+ showFirstLast,
1170
+ testID,
1171
+ styleOverrides: o
1172
+ }) {
1173
+ const { theme, t } = useUi();
1174
+ const { colors } = theme;
1175
+ const atFirst = safePage <= FIRST_PAGE;
1176
+ const atLast = safePage >= lastPage;
1177
+ const showJumps = showFirstLast && !isCompact;
1178
+ return /* @__PURE__ */ jsxs(View, { style: [chromeStyles.pagerNav, o?.pagerNav], children: [
1179
+ !isCompact && /* @__PURE__ */ jsx(
1180
+ RowsControl,
1181
+ {
1182
+ pageSize,
1183
+ pageSizeOptions,
1184
+ onPageSizeChange,
1185
+ isDropdown,
1186
+ testID,
1187
+ styleOverrides: o
1188
+ }
1189
+ ),
1190
+ showJumps && /* @__PURE__ */ jsx(
1191
+ NavButton,
1192
+ {
1193
+ label: t(TABLE_I18N.pagerFirst),
1194
+ hint: t(TABLE_I18N.pagerFirstHint),
1195
+ disabled: atFirst,
1196
+ onPress: () => onPageChange(FIRST_PAGE),
1197
+ color: colors.text,
1198
+ border: colors.border,
1199
+ styleOverrides: o,
1200
+ testID: TABLE_TEST_IDS.pagerFirst
1201
+ }
1202
+ ),
1203
+ /* @__PURE__ */ jsx(
1204
+ NavButton,
1205
+ {
1206
+ label: t(TABLE_I18N.pagerPrev),
1207
+ hint: t(TABLE_I18N.pagerPrevHint),
1208
+ disabled: atFirst,
1209
+ onPress: () => onPageChange(safePage - 1),
1210
+ color: colors.text,
1211
+ border: colors.border,
1212
+ styleOverrides: o,
1213
+ testID: TABLE_TEST_IDS.pagerPrev
1214
+ }
1215
+ ),
1216
+ /* @__PURE__ */ jsx(
1217
+ NavButton,
1218
+ {
1219
+ label: t(TABLE_I18N.pagerNext),
1220
+ hint: t(TABLE_I18N.pagerNextHint),
1221
+ disabled: atLast,
1222
+ onPress: () => onPageChange(safePage + 1),
1223
+ color: colors.text,
1224
+ border: colors.border,
1225
+ styleOverrides: o,
1226
+ testID: TABLE_TEST_IDS.pagerNext
1227
+ }
1228
+ ),
1229
+ showJumps && /* @__PURE__ */ jsx(
1230
+ NavButton,
1231
+ {
1232
+ label: t(TABLE_I18N.pagerLast),
1233
+ hint: t(TABLE_I18N.pagerLastHint),
1234
+ disabled: atLast,
1235
+ onPress: () => onPageChange(lastPage),
1236
+ color: colors.text,
1237
+ border: colors.border,
1238
+ styleOverrides: o,
1239
+ testID: TABLE_TEST_IDS.pagerLast
1240
+ }
1241
+ )
1242
+ ] });
1243
+ }
1244
+ var FIRST_PAGE2 = 1;
1245
+ var EN_DASH = "\u2013";
1246
+ var BOLD = "700";
1113
1247
  function Pager({
1114
1248
  page,
1115
1249
  pageSize,
@@ -1119,24 +1253,30 @@ function Pager({
1119
1253
  pageSizeOptions = DEFAULT_PAGE_SIZE_OPTIONS,
1120
1254
  rowsVariant = "pills",
1121
1255
  unitLabel,
1256
+ unitLabelSingular,
1257
+ showFirstLast = false,
1258
+ responsive = false,
1259
+ stackBreakpoint = CARD_STACK_BREAKPOINT,
1122
1260
  infoPrefix,
1123
1261
  boldNumbers = false,
1124
1262
  styleOverrides: o,
1125
1263
  testID = TABLE_TEST_IDS.pager
1126
1264
  }) {
1127
1265
  const { theme, t } = useUi();
1128
- const { colors, palette } = theme;
1129
- const brand = palette.primary["500"];
1266
+ const { colors } = theme;
1267
+ const { width } = useWindowDimensions();
1130
1268
  const isDropdown = rowsVariant === "dropdown";
1131
- const lastPage = Math.max(FIRST_PAGE, Math.ceil(total / pageSize));
1132
- const safePage = Math.min(Math.max(page, FIRST_PAGE), lastPage);
1269
+ const isCompact = responsive && width < stackBreakpoint;
1270
+ const lastPage = Math.max(FIRST_PAGE2, Math.ceil(total / pageSize));
1271
+ const safePage = Math.min(Math.max(page, FIRST_PAGE2), lastPage);
1133
1272
  const from = total === 0 ? 0 : (safePage - 1) * pageSize + 1;
1134
1273
  const to = Math.min(safePage * pageSize, total);
1135
1274
  const hasUnit = unitLabel !== void 0 && unitLabel !== "";
1275
+ const resolvedUnit = total === 1 && unitLabelSingular !== void 0 && unitLabelSingular !== "" ? unitLabelSingular : unitLabel;
1136
1276
  const hasPrefix = infoPrefix !== void 0 && infoPrefix !== "";
1137
1277
  const prefix = hasPrefix ? `${infoPrefix} ` : "";
1138
1278
  const ofWord = t(TABLE_I18N.pagerInfo);
1139
- const unitSuffix = hasUnit ? ` ${unitLabel}` : "";
1279
+ const unitSuffix = hasUnit ? ` ${resolvedUnit}` : "";
1140
1280
  const count = `${from.toLocaleString()}${EN_DASH}${to.toLocaleString()} ${ofWord} ${total.toLocaleString()}`;
1141
1281
  const info = `${prefix}${count}${unitSuffix}`;
1142
1282
  const boldStyle = { color: colors.text, fontWeight: BOLD };
@@ -1150,79 +1290,704 @@ function Pager({
1150
1290
  /* @__PURE__ */ jsx(Text, { style: boldStyle, children: total.toLocaleString() }),
1151
1291
  unitSuffix
1152
1292
  ] }) : info }),
1153
- /* @__PURE__ */ jsxs(View, { style: [chromeStyles.pagerNav, o?.pagerNav], children: [
1154
- /* @__PURE__ */ jsx(
1155
- Text,
1156
- {
1157
- style: [isDropdown ? chromeStyles.pagerRowsLabelPlain : chromeStyles.pagerRowsLabel, { color: colors.textSecondary }, o?.pagerRowsLabel],
1158
- children: t(TABLE_I18N.pagerRows)
1159
- }
1160
- ),
1161
- isDropdown ? /* @__PURE__ */ jsx(
1162
- SizeDropdown,
1293
+ /* @__PURE__ */ jsx(
1294
+ PagerNav,
1295
+ {
1296
+ pageSize,
1297
+ pageSizeOptions,
1298
+ onPageSizeChange,
1299
+ onPageChange,
1300
+ safePage,
1301
+ lastPage,
1302
+ isCompact,
1303
+ isDropdown,
1304
+ showFirstLast,
1305
+ testID,
1306
+ styleOverrides: o
1307
+ }
1308
+ )
1309
+ ] });
1310
+ }
1311
+ var FIELD_GAP = 4;
1312
+ var LABEL_FONT = 11;
1313
+ var LABEL_LETTER_SPACING2 = 0.4;
1314
+ var INPUT_RADIUS = 8;
1315
+ var INPUT_PAD_H = 12;
1316
+ var INPUT_PAD_V = 10;
1317
+ var INPUT_FONT = 14;
1318
+ var INPUT_BORDER = 1;
1319
+ var DATE_ROW_GAP = 8;
1320
+ var CHEVRON_FONT2 = 10;
1321
+ var MENU_TOP_GAP3 = 4;
1322
+ var MENU_RADIUS = 8;
1323
+ var MENU_PAD_V2 = 4;
1324
+ var MENU_OPT_PAD_H = 12;
1325
+ var MENU_OPT_PAD_V2 = 8;
1326
+ var MENU_MIN_WIDTH2 = 160;
1327
+ var MENU_MAX_HEIGHT = 260;
1328
+ var MENU_Z = 1e3;
1329
+ var MENU_ELEVATION2 = 8;
1330
+ var MENU_BOX_SHADOW2 = "0px 2px 8px rgba(0, 0, 0, 0.15)";
1331
+ var OPTION_FONT = 14;
1332
+ var ERROR_FONT = 12;
1333
+ var ERROR_GAP = 3;
1334
+ var SWITCH_ROW_GAP = 8;
1335
+ var ACTION_PAD_H = 14;
1336
+ var ACTION_PAD_V = 8;
1337
+ var ACTION_RADIUS = 8;
1338
+ var ACTION_FONT = 13;
1339
+ var filterStyles = StyleSheet.create({
1340
+ field: { gap: FIELD_GAP },
1341
+ label: { fontSize: LABEL_FONT, fontWeight: "700", letterSpacing: LABEL_LETTER_SPACING2, textTransform: "uppercase" },
1342
+ input: {
1343
+ borderWidth: INPUT_BORDER,
1344
+ borderRadius: INPUT_RADIUS,
1345
+ paddingHorizontal: INPUT_PAD_H,
1346
+ paddingVertical: INPUT_PAD_V,
1347
+ fontSize: INPUT_FONT
1348
+ },
1349
+ /** The select trigger: a bordered field box matching the text inputs + a chevron. */
1350
+ selectTrigger: {
1351
+ flexDirection: "row",
1352
+ alignItems: "center",
1353
+ justifyContent: "space-between",
1354
+ gap: DATE_ROW_GAP,
1355
+ borderWidth: INPUT_BORDER,
1356
+ borderRadius: INPUT_RADIUS,
1357
+ paddingHorizontal: INPUT_PAD_H,
1358
+ paddingVertical: INPUT_PAD_V
1359
+ },
1360
+ selectTriggerText: { fontSize: INPUT_FONT, flexShrink: 1 },
1361
+ chevron: { fontSize: CHEVRON_FONT2 },
1362
+ dateRow: { flexDirection: "row", gap: DATE_ROW_GAP },
1363
+ dateCol: { gap: FIELD_GAP, flex: 1 },
1364
+ subLabel: { fontSize: LABEL_FONT, letterSpacing: LABEL_LETTER_SPACING2 },
1365
+ anchor: { position: "relative" },
1366
+ menu: {
1367
+ position: "absolute",
1368
+ top: "100%",
1369
+ left: 0,
1370
+ marginTop: MENU_TOP_GAP3,
1371
+ minWidth: MENU_MIN_WIDTH2,
1372
+ maxHeight: MENU_MAX_HEIGHT,
1373
+ borderWidth: INPUT_BORDER,
1374
+ borderRadius: MENU_RADIUS,
1375
+ paddingVertical: MENU_PAD_V2,
1376
+ zIndex: MENU_Z,
1377
+ boxShadow: MENU_BOX_SHADOW2,
1378
+ elevation: MENU_ELEVATION2,
1379
+ overflow: "hidden"
1380
+ },
1381
+ option: { paddingHorizontal: MENU_OPT_PAD_H, paddingVertical: MENU_OPT_PAD_V2 },
1382
+ optionText: { fontSize: OPTION_FONT },
1383
+ error: { fontSize: ERROR_FONT, marginTop: ERROR_GAP },
1384
+ switchRow: { flexDirection: "row", alignItems: "center", gap: SWITCH_ROW_GAP, paddingVertical: FIELD_GAP },
1385
+ action: { paddingHorizontal: ACTION_PAD_H, paddingVertical: ACTION_PAD_V, borderRadius: ACTION_RADIUS, borderWidth: INPUT_BORDER },
1386
+ actionText: { fontSize: ACTION_FONT, fontWeight: "600" }
1387
+ });
1388
+ var FILTER_HIT_SLOP = { top: 8, bottom: 8 };
1389
+ var DISABLED_OPACITY2 = 0.4;
1390
+ var FULL_OPACITY2 = 1;
1391
+ var TRANSPARENT3 = "transparent";
1392
+ function ActionButton({ label, hint, tone, disabled = false, onPress, testID }) {
1393
+ const { theme } = useUi();
1394
+ const { colors, palette } = theme;
1395
+ const isPrimary = tone === "primary";
1396
+ const brand = palette.primary["500"];
1397
+ return /* @__PURE__ */ jsx(
1398
+ Pressable,
1399
+ {
1400
+ accessibilityRole: "button",
1401
+ accessibilityLabel: label,
1402
+ accessibilityHint: hint,
1403
+ accessibilityState: { disabled },
1404
+ disabled,
1405
+ hitSlop: FILTER_HIT_SLOP,
1406
+ onPress,
1407
+ style: [
1408
+ filterStyles.action,
1163
1409
  {
1164
- pageSize,
1165
- pageSizeOptions,
1166
- onPageSizeChange,
1167
- testID,
1168
- triggerHint: t(TABLE_I18N.pagerRowsTriggerHint),
1169
- optionHint: t(TABLE_I18N.pagerRowsOptionHint),
1170
- textColor: colors.text,
1171
- mutedColor: colors.textSecondary,
1172
- borderColor: colors.border,
1173
- surfaceColor: colors.surface,
1174
- brandColor: brand,
1175
- triggerStyle: o?.sizePill,
1176
- triggerTextStyle: o?.sizePillText
1410
+ borderColor: isPrimary ? brand : colors.border,
1411
+ backgroundColor: isPrimary ? brand : TRANSPARENT3,
1412
+ opacity: disabled ? DISABLED_OPACITY2 : FULL_OPACITY2
1177
1413
  }
1178
- ) : /* @__PURE__ */ jsx(View, { style: [chromeStyles.sizeGroup, o?.sizeGroup], children: pageSizeOptions.map((size) => {
1179
- const active = size === pageSize;
1414
+ ],
1415
+ testID,
1416
+ children: /* @__PURE__ */ jsx(Text, { style: [filterStyles.actionText, { color: isPrimary ? colors.surface : colors.text }], children: label })
1417
+ }
1418
+ );
1419
+ }
1420
+
1421
+ // src/Filters/constants.ts
1422
+ var FILTERS_TEST_ID = "ui-filters-declarative";
1423
+ var FIELD_MIN_WIDTH = {
1424
+ select: 150,
1425
+ text: 200,
1426
+ number: 130,
1427
+ dateRange: 260,
1428
+ typeahead: 200,
1429
+ boolean: 150
1430
+ };
1431
+ var DEFAULT_TYPEAHEAD_MIN_CHARS = 1;
1432
+ var DEFAULT_TYPEAHEAD_MAX_SUGGESTIONS = 8;
1433
+ var fieldTestID = (barTestID, key) => `${barTestID}-field-${key}`;
1434
+ var applyTestID = (barTestID) => `${barTestID}-apply`;
1435
+ var clearTestID = (barTestID) => `${barTestID}-clear`;
1436
+ var FILTERS_I18N = {
1437
+ apply: "uiTables.filters.apply",
1438
+ applyHint: "uiTables.filters.applyHint",
1439
+ clear: "uiTables.filters.clear",
1440
+ clearHint: "uiTables.filters.clearHint",
1441
+ selectPlaceholder: "uiTables.filters.selectPlaceholder",
1442
+ dropdownHint: "uiTables.filters.dropdownHint",
1443
+ optionHint: "uiTables.filters.optionHint",
1444
+ from: "uiTables.filters.from",
1445
+ to: "uiTables.filters.to"
1446
+ };
1447
+ var GROW_FLEX = 1;
1448
+ function FieldShell({ field, barTestID, children }) {
1449
+ const { theme } = useUi();
1450
+ const minWidth = field.minWidth ?? FIELD_MIN_WIDTH[field.kind];
1451
+ const grow = field.grow === true ? { flexGrow: GROW_FLEX } : null;
1452
+ return /* @__PURE__ */ jsxs(
1453
+ View,
1454
+ {
1455
+ style: [filterStyles.field, { minWidth }, grow, field.style],
1456
+ testID: field.testID ?? fieldTestID(barTestID, field.key),
1457
+ children: [
1458
+ /* @__PURE__ */ jsx(Text, { style: [filterStyles.label, { color: theme.colors.textSecondary }], children: field.label }),
1459
+ children
1460
+ ]
1461
+ }
1462
+ );
1463
+ }
1464
+ function BooleanField({ field, barTestID, value, onChange }) {
1465
+ const { theme } = useUi();
1466
+ const { colors, palette } = theme;
1467
+ const testID = field.testID ?? fieldTestID(barTestID, field.key);
1468
+ return /* @__PURE__ */ jsxs(View, { style: filterStyles.switchRow, testID, children: [
1469
+ /* @__PURE__ */ jsx(Text, { style: [filterStyles.subLabel, { color: colors.text }], children: field.label }),
1470
+ /* @__PURE__ */ jsx(
1471
+ Switch,
1472
+ {
1473
+ accessibilityLabel: field.label,
1474
+ accessibilityHint: field.accessibilityHint,
1475
+ accessibilityRole: "switch",
1476
+ accessibilityState: { checked: value },
1477
+ trackColor: { false: colors.border, true: palette.primary["500"] },
1478
+ thumbColor: colors.surface,
1479
+ value,
1480
+ onValueChange: onChange,
1481
+ testID: `${testID}-switch`
1482
+ }
1483
+ )
1484
+ ] });
1485
+ }
1486
+ var WEB_DATE_PROPS = typeof document !== "undefined" ? { type: "date" } : {};
1487
+ function DateInput(p) {
1488
+ return /* @__PURE__ */ jsxs(View, { style: filterStyles.dateCol, children: [
1489
+ /* @__PURE__ */ jsx(Text, { style: [filterStyles.subLabel, { color: p.muted }], children: p.label }),
1490
+ /* @__PURE__ */ jsx(
1491
+ TextInput,
1492
+ {
1493
+ accessibilityLabel: p.label,
1494
+ accessibilityHint: p.hint,
1495
+ placeholder: p.placeholder,
1496
+ placeholderTextColor: p.muted,
1497
+ value: p.value,
1498
+ onChangeText: p.onChangeText,
1499
+ onSubmitEditing: p.onSubmit,
1500
+ style: [filterStyles.input, { borderColor: p.border, backgroundColor: p.surface, color: p.color }],
1501
+ testID: `${p.testID}-${p.side}`,
1502
+ ...WEB_DATE_PROPS
1503
+ }
1504
+ )
1505
+ ] });
1506
+ }
1507
+ function DateRangeField({ field, barTestID, value, onChange, onSubmit, fromLabel, toLabel }) {
1508
+ const { theme } = useUi();
1509
+ const { colors } = theme;
1510
+ const testID = field.testID ?? fieldTestID(barTestID, field.key);
1511
+ return /* @__PURE__ */ jsxs(View, { style: filterStyles.dateRow, testID: `${testID}-range`, children: [
1512
+ /* @__PURE__ */ jsx(
1513
+ DateInput,
1514
+ {
1515
+ side: "from",
1516
+ label: field.fromLabel ?? fromLabel,
1517
+ placeholder: field.fromPlaceholder,
1518
+ value: value.from,
1519
+ color: colors.text,
1520
+ border: colors.border,
1521
+ surface: colors.surface,
1522
+ muted: colors.textSecondary,
1523
+ hint: field.accessibilityHint,
1524
+ testID,
1525
+ onChangeText: (from) => onChange({ from, to: value.to }),
1526
+ onSubmit
1527
+ }
1528
+ ),
1529
+ /* @__PURE__ */ jsx(
1530
+ DateInput,
1531
+ {
1532
+ side: "to",
1533
+ label: field.toLabel ?? toLabel,
1534
+ placeholder: field.toPlaceholder,
1535
+ value: value.to,
1536
+ color: colors.text,
1537
+ border: colors.border,
1538
+ surface: colors.surface,
1539
+ muted: colors.textSecondary,
1540
+ hint: field.accessibilityHint,
1541
+ testID,
1542
+ onChangeText: (to) => onChange({ from: value.from, to }),
1543
+ onSubmit
1544
+ }
1545
+ )
1546
+ ] });
1547
+ }
1548
+ var IS_WEB3 = typeof document !== "undefined";
1549
+ var ESCAPE_KEY = "Escape";
1550
+ var ACTIVE_WEIGHT = "700";
1551
+ var IDLE_WEIGHT = "400";
1552
+ function useDismiss(anchorRef, menuRef, onDismiss) {
1553
+ useEffect(() => {
1554
+ if (!IS_WEB3) return void 0;
1555
+ const onMouseDown = (event) => {
1556
+ const anchorNode = anchorRef.current;
1557
+ const menuNode = menuRef.current;
1558
+ const target = event.target instanceof Node ? event.target : null;
1559
+ const insideAnchor = anchorNode !== null && target !== null && anchorNode.contains(target);
1560
+ const insideMenu = menuNode !== null && target !== null && menuNode.contains(target);
1561
+ if (!insideAnchor && !insideMenu) onDismiss();
1562
+ };
1563
+ const onKeyDown = (event) => {
1564
+ if (event.key === ESCAPE_KEY) onDismiss();
1565
+ };
1566
+ document.addEventListener("mousedown", onMouseDown);
1567
+ document.addEventListener("keydown", onKeyDown);
1568
+ return () => {
1569
+ document.removeEventListener("mousedown", onMouseDown);
1570
+ document.removeEventListener("keydown", onKeyDown);
1571
+ };
1572
+ }, [anchorRef, menuRef, onDismiss]);
1573
+ }
1574
+ function AnchoredMenu({
1575
+ options,
1576
+ selectedValue,
1577
+ onSelect,
1578
+ onDismiss,
1579
+ colors,
1580
+ optionHint,
1581
+ testID,
1582
+ anchorRef
1583
+ }) {
1584
+ const menuRef = React4.useRef(null);
1585
+ useDismiss(anchorRef, menuRef, onDismiss);
1586
+ return /* @__PURE__ */ jsx(
1587
+ View,
1588
+ {
1589
+ ref: menuRef,
1590
+ accessibilityRole: "menu",
1591
+ style: [filterStyles.menu, { borderColor: colors.border, backgroundColor: colors.surface }],
1592
+ testID: `${testID}-menu`,
1593
+ children: options.map((opt) => {
1594
+ const active = opt.value === selectedValue;
1180
1595
  return /* @__PURE__ */ jsx(
1181
1596
  Pressable,
1182
1597
  {
1183
- testID: `${testID}-size-${size}`,
1184
- accessibilityRole: "button",
1185
- accessibilityLabel: String(size),
1186
- accessibilityHint: t(TABLE_I18N.pagerRowsOptionHint),
1598
+ accessibilityRole: "menuitem",
1599
+ accessibilityLabel: opt.label,
1600
+ accessibilityHint: optionHint,
1187
1601
  accessibilityState: { selected: active },
1188
- hitSlop: PAGER_HIT_SLOP,
1189
- onPress: () => onPageSizeChange(size),
1190
- style: [chromeStyles.sizePill, { borderColor: active ? brand : colors.border, backgroundColor: active ? brand : TRANSPARENT2 }, o?.sizePill],
1191
- children: /* @__PURE__ */ jsx(Text, { style: [chromeStyles.sizePillText, { color: active ? colors.surface : colors.textSecondary }, o?.sizePillText], children: size })
1602
+ onPress: () => onSelect(opt.value),
1603
+ style: filterStyles.option,
1604
+ testID: `${testID}-option-${opt.value}`,
1605
+ children: /* @__PURE__ */ jsx(Text, { style: [filterStyles.optionText, { color: active ? colors.brand : colors.text, fontWeight: active ? ACTIVE_WEIGHT : IDLE_WEIGHT }], children: opt.label })
1192
1606
  },
1193
- size
1607
+ opt.value
1194
1608
  );
1195
- }) }),
1196
- /* @__PURE__ */ jsx(
1197
- NavButton,
1198
- {
1199
- label: t(TABLE_I18N.pagerPrev),
1200
- hint: t(TABLE_I18N.pagerPrevHint),
1201
- disabled: safePage <= FIRST_PAGE,
1202
- onPress: () => onPageChange(safePage - 1),
1203
- color: colors.text,
1204
- border: colors.border,
1205
- styleOverrides: o,
1206
- testID: TABLE_TEST_IDS.pagerPrev
1207
- }
1208
- ),
1209
- /* @__PURE__ */ jsx(
1210
- NavButton,
1211
- {
1212
- label: t(TABLE_I18N.pagerNext),
1213
- hint: t(TABLE_I18N.pagerNextHint),
1214
- disabled: safePage >= lastPage,
1215
- onPress: () => onPageChange(safePage + 1),
1216
- color: colors.text,
1217
- border: colors.border,
1218
- styleOverrides: o,
1219
- testID: TABLE_TEST_IDS.pagerNext
1609
+ })
1610
+ }
1611
+ );
1612
+ }
1613
+ var CHEVRON_DOWN2 = "\u25BE";
1614
+ var CHEVRON_UP2 = "\u25B4";
1615
+ function SelectField({ field, barTestID, value, onChange, placeholder, dropdownHint, optionHint }) {
1616
+ const { theme } = useUi();
1617
+ const { colors, palette } = theme;
1618
+ const anchorRef = useRef(null);
1619
+ const [isOpen, setIsOpen] = useState(false);
1620
+ const testID = field.testID ?? fieldTestID(barTestID, field.key);
1621
+ const selectedLabel = useMemo(() => {
1622
+ const found = field.options.find((o) => o.value === value);
1623
+ return found?.label ?? field.placeholder ?? placeholder;
1624
+ }, [field.options, field.placeholder, placeholder, value]);
1625
+ const close = useCallback(() => {
1626
+ setIsOpen(false);
1627
+ }, []);
1628
+ const toggle = useCallback(() => {
1629
+ setIsOpen((prev) => !prev);
1630
+ }, []);
1631
+ const select = useCallback(
1632
+ (next) => {
1633
+ onChange(next);
1634
+ setIsOpen(false);
1635
+ },
1636
+ [onChange]
1637
+ );
1638
+ return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: filterStyles.anchor, children: [
1639
+ /* @__PURE__ */ jsxs(
1640
+ Pressable,
1641
+ {
1642
+ accessibilityRole: "button",
1643
+ accessibilityLabel: field.label,
1644
+ accessibilityHint: field.accessibilityHint ?? dropdownHint,
1645
+ accessibilityState: { expanded: isOpen },
1646
+ "aria-expanded": isOpen,
1647
+ onPress: toggle,
1648
+ style: [filterStyles.selectTrigger, { borderColor: colors.border, backgroundColor: colors.surface }],
1649
+ testID: `${testID}-trigger`,
1650
+ children: [
1651
+ /* @__PURE__ */ jsx(Text, { numberOfLines: 1, style: [filterStyles.selectTriggerText, { color: value === "" ? colors.textSecondary : colors.text }], children: selectedLabel }),
1652
+ /* @__PURE__ */ jsx(Text, { "aria-hidden": true, style: [filterStyles.chevron, { color: colors.textSecondary }], children: isOpen ? CHEVRON_UP2 : CHEVRON_DOWN2 })
1653
+ ]
1654
+ }
1655
+ ),
1656
+ isOpen ? /* @__PURE__ */ jsx(
1657
+ AnchoredMenu,
1658
+ {
1659
+ options: field.options,
1660
+ selectedValue: value,
1661
+ onSelect: select,
1662
+ onDismiss: close,
1663
+ colors: { text: colors.text, border: colors.border, surface: colors.surface, brand: palette.primary["500"] },
1664
+ optionHint,
1665
+ testID,
1666
+ anchorRef
1667
+ }
1668
+ ) : null
1669
+ ] });
1670
+ }
1671
+ function TextField({ field, barTestID, value, onChange, onSubmit }) {
1672
+ const { theme } = useUi();
1673
+ const { colors } = theme;
1674
+ const isNumber = field.kind === "number";
1675
+ return /* @__PURE__ */ jsx(
1676
+ TextInput,
1677
+ {
1678
+ accessibilityLabel: field.label,
1679
+ accessibilityHint: field.accessibilityHint,
1680
+ keyboardType: isNumber ? "numeric" : "default",
1681
+ placeholder: field.placeholder,
1682
+ placeholderTextColor: colors.textSecondary,
1683
+ value,
1684
+ onChangeText: onChange,
1685
+ onSubmitEditing: onSubmit,
1686
+ style: [filterStyles.input, { borderColor: colors.border, backgroundColor: colors.surface, color: colors.text }],
1687
+ testID: `${field.testID ?? fieldTestID(barTestID, field.key)}-input`
1688
+ }
1689
+ );
1690
+ }
1691
+
1692
+ // src/Filters/utils/suggest.ts
1693
+ var RANK_VALUE_EXACT = 0;
1694
+ var RANK_VALUE_PREFIX = 1;
1695
+ var RANK_LABEL_PREFIX = 2;
1696
+ var RANK_LABEL_CONTAINS = 3;
1697
+ var NO_MATCH = -1;
1698
+ function rankOf(option, needle) {
1699
+ const value = option.value.toLowerCase();
1700
+ const label = option.label.toLowerCase();
1701
+ if (value === needle) return RANK_VALUE_EXACT;
1702
+ if (value.startsWith(needle)) return RANK_VALUE_PREFIX;
1703
+ if (label.startsWith(needle)) return RANK_LABEL_PREFIX;
1704
+ if (label.includes(needle)) return RANK_LABEL_CONTAINS;
1705
+ return NO_MATCH;
1706
+ }
1707
+ function suggestOptions(options, query, minChars, limit) {
1708
+ const needle = query.trim().toLowerCase();
1709
+ if (needle.length < minChars) return [];
1710
+ const scored = [];
1711
+ for (const option of options) {
1712
+ const rank = rankOf(option, needle);
1713
+ if (rank !== NO_MATCH) scored.push({ option, rank });
1714
+ }
1715
+ scored.sort((a, b) => a.rank !== b.rank ? a.rank - b.rank : a.option.label.localeCompare(b.option.label));
1716
+ return scored.slice(0, limit).map((entry) => entry.option);
1717
+ }
1718
+ function isUnmatched(options, text) {
1719
+ const needle = text.trim().toLowerCase();
1720
+ if (needle === "") return false;
1721
+ return !options.some((o) => o.value.toLowerCase() === needle || o.label.toLowerCase() === needle);
1722
+ }
1723
+ var CLOSE_DELAY_MS = 140;
1724
+ function TypeaheadField({ field, barTestID, value, onChange, onSubmit, optionHint }) {
1725
+ const { theme } = useUi();
1726
+ const { colors, palette, semantic } = theme;
1727
+ const anchorRef = useRef(null);
1728
+ const [isOpen, setIsOpen] = useState(false);
1729
+ const testID = field.testID ?? fieldTestID(barTestID, field.key);
1730
+ const minChars = field.minChars ?? DEFAULT_TYPEAHEAD_MIN_CHARS;
1731
+ const maxSuggestions = field.maxSuggestions ?? DEFAULT_TYPEAHEAD_MAX_SUGGESTIONS;
1732
+ const exactPick = useMemo(
1733
+ () => field.options.some((o) => o.label === value),
1734
+ [field.options, value]
1735
+ );
1736
+ const suggestions = useMemo(
1737
+ () => exactPick ? [] : suggestOptions(field.options, value, minChars, maxSuggestions),
1738
+ [exactPick, field.options, value, minChars, maxSuggestions]
1739
+ );
1740
+ const showMenu = isOpen && suggestions.length > 0;
1741
+ const handleChange = useCallback(
1742
+ (next) => {
1743
+ onChange(next);
1744
+ setIsOpen(true);
1745
+ },
1746
+ [onChange]
1747
+ );
1748
+ const pick = useCallback(
1749
+ (optionValue) => {
1750
+ const opt = field.options.find((o) => o.value === optionValue);
1751
+ onChange(opt?.label ?? optionValue);
1752
+ setIsOpen(false);
1753
+ },
1754
+ [field.options, onChange]
1755
+ );
1756
+ const handleBlur = useCallback(() => {
1757
+ setTimeout(() => setIsOpen(false), CLOSE_DELAY_MS);
1758
+ }, []);
1759
+ const handleSubmit = useCallback(() => {
1760
+ setIsOpen(false);
1761
+ onSubmit();
1762
+ }, [onSubmit]);
1763
+ const hasError = field.error !== void 0 && field.error !== "";
1764
+ return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: filterStyles.anchor, children: [
1765
+ /* @__PURE__ */ jsx(
1766
+ TextInput,
1767
+ {
1768
+ accessibilityLabel: field.label,
1769
+ accessibilityHint: field.accessibilityHint,
1770
+ autoCapitalize: "none",
1771
+ autoCorrect: false,
1772
+ placeholder: field.placeholder,
1773
+ placeholderTextColor: colors.textSecondary,
1774
+ spellCheck: false,
1775
+ value,
1776
+ onChangeText: handleChange,
1777
+ onFocus: () => setIsOpen(true),
1778
+ onBlur: handleBlur,
1779
+ onSubmitEditing: handleSubmit,
1780
+ style: [filterStyles.input, { borderColor: hasError ? semantic.error["500"] : colors.border, backgroundColor: colors.surface, color: colors.text }],
1781
+ testID: `${testID}-input`
1782
+ }
1783
+ ),
1784
+ showMenu ? /* @__PURE__ */ jsx(
1785
+ AnchoredMenu,
1786
+ {
1787
+ options: suggestions,
1788
+ selectedValue: value,
1789
+ onSelect: pick,
1790
+ onDismiss: () => setIsOpen(false),
1791
+ colors: { text: colors.text, border: colors.border, surface: colors.surface, brand: palette.primary["500"] },
1792
+ optionHint,
1793
+ testID,
1794
+ anchorRef
1795
+ }
1796
+ ) : null,
1797
+ hasError ? /* @__PURE__ */ jsx(Text, { role: "alert", style: [filterStyles.error, { color: semantic.error["500"] }], testID: `${testID}-error`, children: field.error }) : null
1798
+ ] });
1799
+ }
1800
+ var EMPTY_RANGE = { from: "", to: "" };
1801
+ function asString(value) {
1802
+ return typeof value === "string" ? value : "";
1803
+ }
1804
+ function asBool(value) {
1805
+ return value === true;
1806
+ }
1807
+ function asRange(value) {
1808
+ if (typeof value === "object" && value !== null && "from" in value && "to" in value) return value;
1809
+ return EMPTY_RANGE;
1810
+ }
1811
+ function FilterFieldView({ field, barTestID, values, setField, onSubmit, strings, renderSelect }) {
1812
+ const raw = values[field.key];
1813
+ if (field.kind === "boolean") {
1814
+ return /* @__PURE__ */ jsx(BooleanField, { field, barTestID, value: asBool(raw), onChange: (v) => setField(field.key, v) });
1815
+ }
1816
+ const body = (() => {
1817
+ switch (field.kind) {
1818
+ case "select":
1819
+ if (renderSelect !== void 0) {
1820
+ return renderSelect({
1821
+ field,
1822
+ value: asString(raw),
1823
+ onChange: (v) => setField(field.key, v),
1824
+ testID: field.testID ?? fieldTestID(barTestID, field.key)
1825
+ });
1220
1826
  }
1221
- )
1222
- ] })
1827
+ return /* @__PURE__ */ jsx(
1828
+ SelectField,
1829
+ {
1830
+ field,
1831
+ barTestID,
1832
+ value: asString(raw),
1833
+ onChange: (v) => setField(field.key, v),
1834
+ placeholder: strings.selectPlaceholder,
1835
+ dropdownHint: strings.dropdownHint,
1836
+ optionHint: strings.optionHint
1837
+ }
1838
+ );
1839
+ case "typeahead":
1840
+ return /* @__PURE__ */ jsx(
1841
+ TypeaheadField,
1842
+ {
1843
+ field,
1844
+ barTestID,
1845
+ value: asString(raw),
1846
+ onChange: (v) => setField(field.key, v),
1847
+ onSubmit,
1848
+ optionHint: strings.optionHint
1849
+ }
1850
+ );
1851
+ case "dateRange":
1852
+ return /* @__PURE__ */ jsx(
1853
+ DateRangeField,
1854
+ {
1855
+ field,
1856
+ barTestID,
1857
+ value: asRange(raw),
1858
+ onChange: (v) => setField(field.key, v),
1859
+ onSubmit,
1860
+ fromLabel: strings.fromLabel,
1861
+ toLabel: strings.toLabel
1862
+ }
1863
+ );
1864
+ default:
1865
+ return /* @__PURE__ */ jsx(
1866
+ TextField,
1867
+ {
1868
+ field,
1869
+ barTestID,
1870
+ value: asString(raw),
1871
+ onChange: (v) => setField(field.key, v),
1872
+ onSubmit
1873
+ }
1874
+ );
1875
+ }
1876
+ })();
1877
+ return /* @__PURE__ */ jsx(FieldShell, { field, barTestID, children: body });
1878
+ }
1879
+ function Filters({
1880
+ fields,
1881
+ values,
1882
+ onChange,
1883
+ resultsCount,
1884
+ resultsLabel,
1885
+ onApply,
1886
+ applyLabel,
1887
+ applyHint,
1888
+ applyDisabled = false,
1889
+ onClear,
1890
+ clearLabel,
1891
+ clearHint,
1892
+ actions,
1893
+ selectPlaceholder,
1894
+ fromLabel,
1895
+ toLabel,
1896
+ renderSelect,
1897
+ styleOverrides,
1898
+ testID = FILTERS_TEST_ID
1899
+ }) {
1900
+ const { t } = useUi();
1901
+ const setField = useCallback(
1902
+ (key, value) => {
1903
+ onChange({ ...values, [key]: value });
1904
+ },
1905
+ [onChange, values]
1906
+ );
1907
+ const onSubmit = useCallback(() => {
1908
+ if (onApply !== void 0 && !applyDisabled) onApply();
1909
+ }, [onApply, applyDisabled]);
1910
+ const strings = useMemo(
1911
+ () => ({
1912
+ selectPlaceholder: selectPlaceholder ?? t(FILTERS_I18N.selectPlaceholder),
1913
+ dropdownHint: t(FILTERS_I18N.dropdownHint),
1914
+ optionHint: t(FILTERS_I18N.optionHint),
1915
+ fromLabel: fromLabel ?? t(FILTERS_I18N.from),
1916
+ toLabel: toLabel ?? t(FILTERS_I18N.to)
1917
+ }),
1918
+ [selectPlaceholder, fromLabel, toLabel, t]
1919
+ );
1920
+ const barActions = /* @__PURE__ */ jsxs(Fragment, { children: [
1921
+ actions,
1922
+ onClear !== void 0 && /* @__PURE__ */ jsx(
1923
+ ActionButton,
1924
+ {
1925
+ label: clearLabel ?? t(FILTERS_I18N.clear),
1926
+ hint: clearHint ?? t(FILTERS_I18N.clearHint),
1927
+ tone: "ghost",
1928
+ onPress: onClear,
1929
+ testID: clearTestID(testID)
1930
+ }
1931
+ ),
1932
+ onApply !== void 0 && /* @__PURE__ */ jsx(
1933
+ ActionButton,
1934
+ {
1935
+ label: applyLabel ?? t(FILTERS_I18N.apply),
1936
+ hint: applyHint ?? t(FILTERS_I18N.applyHint),
1937
+ tone: "primary",
1938
+ disabled: applyDisabled,
1939
+ onPress: onApply,
1940
+ testID: applyTestID(testID)
1941
+ }
1942
+ )
1223
1943
  ] });
1944
+ const hasActions = actions !== void 0 || onClear !== void 0 || onApply !== void 0;
1945
+ return /* @__PURE__ */ jsx(
1946
+ FilterBar,
1947
+ {
1948
+ resultsCount,
1949
+ resultsLabel,
1950
+ actions: hasActions ? barActions : void 0,
1951
+ styleOverrides,
1952
+ testID,
1953
+ children: fields.map((field) => /* @__PURE__ */ jsx(
1954
+ FilterFieldView,
1955
+ {
1956
+ field,
1957
+ barTestID: testID,
1958
+ values,
1959
+ setField,
1960
+ onSubmit,
1961
+ strings,
1962
+ renderSelect
1963
+ },
1964
+ field.key
1965
+ ))
1966
+ }
1967
+ );
1968
+ }
1969
+ function shallowEqual(a, b) {
1970
+ return JSON.stringify(a) === JSON.stringify(b);
1971
+ }
1972
+ function useFilterDraft({ initial, onApply, onReset }) {
1973
+ const [draft, setDraft] = useState(initial);
1974
+ const [committed, setCommitted] = useState(initial);
1975
+ const setField = useCallback((key, value) => {
1976
+ setDraft((prev) => ({ ...prev, [key]: value }));
1977
+ }, []);
1978
+ const apply = useCallback(() => {
1979
+ setCommitted(draft);
1980
+ onApply?.(draft);
1981
+ }, [draft, onApply]);
1982
+ const reset = useCallback(() => {
1983
+ setDraft(initial);
1984
+ setCommitted(initial);
1985
+ onReset?.();
1986
+ }, [initial, onReset]);
1987
+ const dirty = useMemo(() => !shallowEqual(draft, committed), [draft, committed]);
1988
+ return { draft, committed, setField, setDraft, apply, reset, dirty };
1224
1989
  }
1225
1990
 
1226
- export { CARD_STACK_BREAKPOINT, DEFAULT_PAGE_SIZE_OPTIONS, DataTable, FilterBar, Pager, StatCard, TABLE_I18N, TABLE_TEST_IDS, rowDetailTestID, rowSelectTestID, rowTestID, selectAllTestID };
1991
+ export { CARD_STACK_BREAKPOINT, DEFAULT_PAGE_SIZE_OPTIONS, DEFAULT_TYPEAHEAD_MAX_SUGGESTIONS, DEFAULT_TYPEAHEAD_MIN_CHARS, DataTable, FIELD_MIN_WIDTH, FILTERS_I18N, FILTERS_TEST_ID, FilterBar, Filters, Pager, StatCard, TABLE_I18N, TABLE_TEST_IDS, applyTestID, clearTestID, fieldTestID, isUnmatched, rowDetailTestID, rowSelectTestID, rowTestID, selectAllTestID, suggestOptions, useFilterDraft };
1227
1992
  //# sourceMappingURL=index.mjs.map
1228
1993
  //# sourceMappingURL=index.mjs.map