@guozhi5658/srp-table 1.1.3 → 1.1.4

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.
@@ -2410,6 +2410,10 @@ function InfoIcon(props) {
2410
2410
  React__default["default"].createElement("path", { d: "M8.5 11L8.5 6.5 6.5 6.5 6.5 7.5 7.5 7.5 7.5 11 6 11 6 12 10 12 10 11zM8 3.5c-.4 0-.8.3-.8.8S7.6 5 8 5c.4 0 .8-.3.8-.8S8.4 3.5 8 3.5z" }),
2411
2411
  React__default["default"].createElement("path", { d: "M8,15c-3.9,0-7-3.1-7-7s3.1-7,7-7s7,3.1,7,7S11.9,15,8,15z M8,2C4.7,2,2,4.7,2,8s2.7,6,6,6s6-2.7,6-6S11.3,2,8,2z" })));
2412
2412
  }
2413
+ function FilterIcon(props) {
2414
+ return (React__default["default"].createElement("svg", Object.assign({ focusable: "false", preserveAspectRatio: "xMidYMid meet", fill: "currentColor", width: "16", height: "16", viewBox: "0 0 16 16" }, props),
2415
+ React__default["default"].createElement("path", { d: "M2 3.25c0-.69.56-1.25 1.25-1.25h9.5c.69 0 1.25.56 1.25 1.25 0 .31-.12.61-.33.84L9.5 8.58V12a1 1 0 0 1-.45.83l-1.8 1.2A.75.75 0 0 1 6 13.4V8.58L2.33 4.09A1.25 1.25 0 0 1 2 3.25zm1.53.25L7.2 8v4.01l.8-.54V8l3.67-4.5H3.53z" })));
2416
+ }
2413
2417
  function CaretRightIcon(props) {
2414
2418
  return (React__default["default"].createElement("svg", Object.assign({ focusable: "false", preserveAspectRatio: "xMidYMid meet", fill: "currentColor", width: "16", height: "16", viewBox: "0 0 32 32" }, props),
2415
2419
  React__default["default"].createElement("path", { d: "M12 8L22 16 12 24z" })));
@@ -2422,6 +2426,7 @@ const icons = {
2422
2426
  Loading: LoadingIcon,
2423
2427
  CaretDown: CaretDownIcon,
2424
2428
  CaretRight: CaretRightIcon,
2429
+ Filter: FilterIcon,
2425
2430
  Info: InfoIcon,
2426
2431
  };
2427
2432
 
@@ -2383,6 +2383,10 @@ function InfoIcon(props) {
2383
2383
  React.createElement("path", { d: "M8.5 11L8.5 6.5 6.5 6.5 6.5 7.5 7.5 7.5 7.5 11 6 11 6 12 10 12 10 11zM8 3.5c-.4 0-.8.3-.8.8S7.6 5 8 5c.4 0 .8-.3.8-.8S8.4 3.5 8 3.5z" }),
2384
2384
  React.createElement("path", { d: "M8,15c-3.9,0-7-3.1-7-7s3.1-7,7-7s7,3.1,7,7S11.9,15,8,15z M8,2C4.7,2,2,4.7,2,8s2.7,6,6,6s6-2.7,6-6S11.3,2,8,2z" })));
2385
2385
  }
2386
+ function FilterIcon(props) {
2387
+ return (React.createElement("svg", Object.assign({ focusable: "false", preserveAspectRatio: "xMidYMid meet", fill: "currentColor", width: "16", height: "16", viewBox: "0 0 16 16" }, props),
2388
+ React.createElement("path", { d: "M2 3.25c0-.69.56-1.25 1.25-1.25h9.5c.69 0 1.25.56 1.25 1.25 0 .31-.12.61-.33.84L9.5 8.58V12a1 1 0 0 1-.45.83l-1.8 1.2A.75.75 0 0 1 6 13.4V8.58L2.33 4.09A1.25 1.25 0 0 1 2 3.25zm1.53.25L7.2 8v4.01l.8-.54V8l3.67-4.5H3.53z" })));
2389
+ }
2386
2390
  function CaretRightIcon(props) {
2387
2391
  return (React.createElement("svg", Object.assign({ focusable: "false", preserveAspectRatio: "xMidYMid meet", fill: "currentColor", width: "16", height: "16", viewBox: "0 0 32 32" }, props),
2388
2392
  React.createElement("path", { d: "M12 8L22 16 12 24z" })));
@@ -2395,6 +2399,7 @@ const icons = {
2395
2399
  Loading: LoadingIcon,
2396
2400
  CaretDown: CaretDownIcon,
2397
2401
  CaretRight: CaretRightIcon,
2402
+ Filter: FilterIcon,
2398
2403
  Info: InfoIcon,
2399
2404
  };
2400
2405
 
@@ -15,12 +15,14 @@ interface IconProps extends React.SVGProps<SVGElement> {
15
15
  }
16
16
  declare function CaretDownIcon(props: IconProps): JSX.Element;
17
17
  declare function InfoIcon(props: IconProps): JSX.Element;
18
+ declare function FilterIcon(props: IconProps): JSX.Element;
18
19
  declare function CaretRightIcon(props: IconProps): JSX.Element;
19
20
  declare function LoadingIcon(props: IconProps): JSX.Element;
20
21
  export declare const icons: {
21
22
  Loading: typeof LoadingIcon;
22
23
  CaretDown: typeof CaretDownIcon;
23
24
  CaretRight: typeof CaretRightIcon;
25
+ Filter: typeof FilterIcon;
24
26
  Info: typeof InfoIcon;
25
27
  };
26
28
  export {};
@@ -1,6 +1,14 @@
1
1
  import React, { ReactNode } from 'react';
2
2
  export type ArtColumnAlign = 'left' | 'center' | 'right';
3
3
  export type CellProps = React.TdHTMLAttributes<HTMLTableCellElement>;
4
+ export interface ArtColumnFilterProps {
5
+ /** 是否展示搜索输入框,默认为 true */
6
+ showSearch?: boolean;
7
+ /** 自定义筛选条件 */
8
+ filterOptions?(inputValue: string, record: any): boolean;
9
+ /** 预置的筛选项列表 */
10
+ dataSource?: string[];
11
+ }
4
12
  export interface ArtColumnStaticPart {
5
13
  /** 列的名称 */
6
14
  name: string;
@@ -22,6 +30,8 @@ export interface ArtColumnStaticPart {
22
30
  features?: {
23
31
  [key: string]: any;
24
32
  };
33
+ /** 表头筛选配置 */
34
+ filterProps?: ArtColumnFilterProps;
25
35
  }
26
36
  export interface ArtColumnDynamicPart {
27
37
  /** 自定义取数方法 */
@@ -0,0 +1,34 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { ArtColumn } from '../../interfaces';
3
+ import { TablePipeline } from '../pipeline';
4
+ import { FilterDataItem } from './filterModeUtils';
5
+ export type { FilterDataItem } from './filterModeUtils';
6
+ export interface FilterModeFeatureOptions {
7
+ /** (非受控用法) 默认的筛选条件 */
8
+ defaultFilterData?: FilterDataItem[];
9
+ /** (受控用法) 当前筛选条件 */
10
+ filterData?: FilterDataItem[];
11
+ /** 更新筛选条件的回调函数 */
12
+ onChangeFilterData?(nextFilterData: FilterDataItem[]): void;
13
+ /** 是否保持 dataSource 不变 */
14
+ keepDataSource?: boolean;
15
+ /** 弹层组件属性 */
16
+ popoverProps?: any;
17
+ /** 自定义筛选表头 */
18
+ FilterHeaderCell?: React.ComponentType<FilterHeaderCellProps>;
19
+ }
20
+ export interface FilterHeaderCellProps {
21
+ filterModeOptions: {
22
+ keepDataSource: boolean;
23
+ };
24
+ children: ReactNode;
25
+ column: ArtColumn;
26
+ filterValue: string;
27
+ active: boolean;
28
+ open: boolean;
29
+ setOpen(nextOpen: boolean): void;
30
+ filterPanel: ReactNode;
31
+ Popover: React.ComponentType<any>;
32
+ popoverProps?: any;
33
+ }
34
+ export declare function filterMode(opts?: FilterModeFeatureOptions): (pipeline: TablePipeline) => TablePipeline;
@@ -0,0 +1,10 @@
1
+ import { ArtColumn } from '../../interfaces';
2
+ export type FilterDataItem = {
3
+ code: string;
4
+ filterValue: any;
5
+ };
6
+ export declare function normalizeFilterData(filterData: FilterDataItem[]): FilterDataItem[];
7
+ export declare function updateFilterData(filterData: FilterDataItem[], code: string, nextFilterValue: any): FilterDataItem[];
8
+ export declare function getFilterableColumns(columns: ArtColumn[]): ArtColumn[];
9
+ export declare function filterDataSourceByFilterData(dataSource: any[], columns: ArtColumn[], filterData: FilterDataItem[]): any[];
10
+ export declare function resolveFilterDataSource(dataSource: any[], columns: ArtColumn[], filterData: FilterDataItem[], keepDataSource?: boolean): any[];
@@ -4,6 +4,7 @@ export { columnHover, ColumnHoverFeatureOptions } from './columnHover';
4
4
  export { columnRangeHover, ColumnRangeHoverFeatureOptions } from './columnRangeHover';
5
5
  export { columnResize, ColumnResizeFeatureOptions } from './columnResize';
6
6
  export { fillRemainingWidth } from './fillRemainingWidth';
7
+ export { filterMode, FilterDataItem, FilterHeaderCellProps, FilterModeFeatureOptions } from './filterMode';
7
8
  export { multiSelect, MultiSelectFeatureOptions } from './multiSelect';
8
9
  export { rowDetail, RowDetailFeatureOptions } from './rowDetail';
9
10
  export { rowPopover, RowPopoverFeatureOptions } from './rowPopover';
@@ -1,4 +1,4 @@
1
- import { l as groupBy2, e as always, i as isLeafNode, n as fromEntries, d as icons, g as getTreeDepth, _ as __rest, B as BaseTable, T as TablePipeline, j as treeMode } from './chunks/srp-table-pipeline-67cab92a.esm.js';
1
+ import { l as groupBy2, e as always, i as isLeafNode, n as fromEntries, d as icons, g as getTreeDepth, _ as __rest, B as BaseTable, T as TablePipeline, j as treeMode } from './chunks/srp-table-pipeline-eab4ee87.esm.js';
2
2
  import React from 'react';
3
3
  import { noop } from 'rxjs';
4
4
  import styled__default from 'styled-components';
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var pipeline = require('./chunks/srp-table-pipeline-f974e586.js');
5
+ var pipeline = require('./chunks/srp-table-pipeline-6f4336b2.js');
6
6
  var React = require('react');
7
7
  var rxjs = require('rxjs');
8
8
  var styled = require('styled-components');
@@ -1,5 +1,5 @@
1
- import { f as flatMap, g as getTreeDepth, i as isLeafNode, c as collectNodes, a as internals, S as SpanManager, C as Classes, b as appendFillRemainingWidthColumn, m as mergeCellProps, _ as __rest, d as icons, t as takeFillRemainingWidthColumn, e as always, h as arrayUtils, I as InlineFlexCell, E as ExpansionCell, j as treeMode, k as treeMetaSymbol } from './chunks/srp-table-pipeline-67cab92a.esm.js';
2
- export { B as BaseTable, C as Classes, T as TablePipeline, c as collectNodes, g as getTreeDepth, l as groupBy2, a as internals, i as isLeafNode, m as mergeCellProps, u as useTablePipeline } from './chunks/srp-table-pipeline-67cab92a.esm.js';
1
+ import { f as flatMap, g as getTreeDepth, i as isLeafNode, c as collectNodes, a as internals, S as SpanManager, C as Classes, b as appendFillRemainingWidthColumn, m as mergeCellProps, _ as __rest, d as icons, t as takeFillRemainingWidthColumn, e as always, h as arrayUtils, I as InlineFlexCell, E as ExpansionCell, j as treeMode, k as treeMetaSymbol } from './chunks/srp-table-pipeline-eab4ee87.esm.js';
2
+ export { B as BaseTable, C as Classes, T as TablePipeline, c as collectNodes, g as getTreeDepth, l as groupBy2, a as internals, i as isLeafNode, m as mergeCellProps, u as useTablePipeline } from './chunks/srp-table-pipeline-eab4ee87.esm.js';
3
3
  import React, { useState, useEffect } from 'react';
4
4
  import styled__default from 'styled-components';
5
5
  import { fromEvent } from 'rxjs';
@@ -1227,6 +1227,286 @@ function fillRemainingWidth() {
1227
1227
  };
1228
1228
  }
1229
1229
 
1230
+ function normalizeFilterData(filterData) {
1231
+ const filterMap = new Map();
1232
+ for (const item of filterData !== null && filterData !== void 0 ? filterData : []) {
1233
+ if (item == null || typeof item.code !== 'string' || item.code.length === 0) {
1234
+ continue;
1235
+ }
1236
+ if (isBlankFilterValue$1(item.filterValue)) {
1237
+ filterMap.delete(item.code);
1238
+ continue;
1239
+ }
1240
+ filterMap.set(item.code, {
1241
+ code: item.code,
1242
+ filterValue: stringifyFilterValue$1(item.filterValue),
1243
+ });
1244
+ }
1245
+ return Array.from(filterMap.values());
1246
+ }
1247
+ function updateFilterData(filterData, code, nextFilterValue) {
1248
+ const nextFilterData = normalizeFilterData(filterData).filter((item) => item.code !== code);
1249
+ if (isBlankFilterValue$1(nextFilterValue)) {
1250
+ return nextFilterData;
1251
+ }
1252
+ nextFilterData.push({
1253
+ code,
1254
+ filterValue: stringifyFilterValue$1(nextFilterValue),
1255
+ });
1256
+ return nextFilterData;
1257
+ }
1258
+ function getFilterableColumns(columns) {
1259
+ return collectNodes(columns, 'leaf-only').filter((column) => column.code != null && column.filterProps != null);
1260
+ }
1261
+ function filterDataSourceByFilterData(dataSource, columns, filterData) {
1262
+ const activeFilterData = normalizeFilterData(filterData);
1263
+ if (activeFilterData.length === 0) {
1264
+ return dataSource;
1265
+ }
1266
+ const filterColumnsMap = new Map(columns.map((column) => [column.code, column]));
1267
+ return dataSource.filter((record) => {
1268
+ return activeFilterData.every(({ code, filterValue }) => {
1269
+ const column = filterColumnsMap.get(code);
1270
+ if (column == null) {
1271
+ return true;
1272
+ }
1273
+ return matchFilterValue(column, stringifyFilterValue$1(filterValue), record);
1274
+ });
1275
+ });
1276
+ }
1277
+ function matchFilterValue(column, filterValue, record) {
1278
+ var _a;
1279
+ if (isBlankFilterValue$1(filterValue)) {
1280
+ return true;
1281
+ }
1282
+ const customFilter = (_a = column.filterProps) === null || _a === void 0 ? void 0 : _a.filterOptions;
1283
+ if (typeof customFilter === 'function') {
1284
+ return customFilter(filterValue, record);
1285
+ }
1286
+ const rawValue = record === null || record === void 0 ? void 0 : record[column.code];
1287
+ if (rawValue == null) {
1288
+ return false;
1289
+ }
1290
+ return String(rawValue).toLowerCase().includes(filterValue.toLowerCase());
1291
+ }
1292
+ function isBlankFilterValue$1(value) {
1293
+ return stringifyFilterValue$1(value).trim() === '';
1294
+ }
1295
+ function stringifyFilterValue$1(value) {
1296
+ if (value == null) {
1297
+ return '';
1298
+ }
1299
+ return typeof value === 'string' ? value : String(value);
1300
+ }
1301
+
1302
+ const FilterHeaderShell = styled__default.div `
1303
+ display: flex;
1304
+ align-items: center;
1305
+ `;
1306
+ const FilterTrigger = styled__default.button `
1307
+ display: inline-flex;
1308
+ align-items: center;
1309
+ justify-content: center;
1310
+ width: 20px;
1311
+ height: 20px;
1312
+ margin-left: 4px;
1313
+ padding: 0;
1314
+ border: none;
1315
+ background: transparent;
1316
+ color: ${(props) => (props.active ? '#23A3FF' : '#8c8c8c')};
1317
+ cursor: pointer;
1318
+
1319
+ &:hover {
1320
+ color: ${(props) => (props.active ? '#23A3FF' : '#595959')};
1321
+ }
1322
+ `;
1323
+ const FilterPanel = styled__default.div `
1324
+ width: 160px;
1325
+ padding: 8px 8px 6px;
1326
+ box-sizing: border-box;
1327
+ color: #262626;
1328
+ font-size: 12px;
1329
+ line-height: 1.5;
1330
+ `;
1331
+ const FilterInput = styled__default.input `
1332
+ width: 100%;
1333
+ height: 28px;
1334
+ padding: 0 8px;
1335
+ box-sizing: border-box;
1336
+ border: 1px solid #d9d9d9;
1337
+ border-radius: 5px;
1338
+ outline: none;
1339
+ font-size: 12px;
1340
+ line-height: 1.5;
1341
+
1342
+ &:focus {
1343
+ border-color: #23a3ff;
1344
+ }
1345
+ `;
1346
+ const FilterOptionList = styled__default.div `
1347
+ max-height: 136px;
1348
+ margin-top: 6px;
1349
+ overflow: auto;
1350
+ `;
1351
+ const FilterOptionButton = styled__default.button `
1352
+ width: 100%;
1353
+ min-height: 28px;
1354
+ margin-top: 4px;
1355
+ padding: 3px 8px;
1356
+ box-sizing: border-box;
1357
+ border: 1px solid ${(props) => (props.active ? '#23A3FF' : '#e8e8e8')};
1358
+ border-radius: 5px;
1359
+ background: ${(props) => (props.active ? 'rgba(35, 163, 255, 0.12)' : '#fff')};
1360
+ color: #262626;
1361
+ font-size: 12px;
1362
+ line-height: 1.5;
1363
+ text-align: left;
1364
+ cursor: pointer;
1365
+ `;
1366
+ const FilterActionRow = styled__default.div `
1367
+ display: flex;
1368
+ justify-content: flex-end;
1369
+ gap: 4px;
1370
+ margin-top: 6px;
1371
+ `;
1372
+ const FilterActionButton = styled__default.button `
1373
+ min-width: 44px;
1374
+ height: 26px;
1375
+ padding: 0 8px;
1376
+ box-sizing: border-box;
1377
+ border: 1px solid ${(props) => (props.primary ? '#23A3FF' : '#d9d9d9')};
1378
+ border-radius: 999px;
1379
+ background: ${(props) => (props.primary ? '#23A3FF' : '#fff')};
1380
+ color: ${(props) => (props.primary ? '#fff' : '#262626')};
1381
+ font-size: 12px;
1382
+ line-height: 1;
1383
+ cursor: pointer;
1384
+ `;
1385
+ const FilterEmptyState = styled__default.div `
1386
+ margin-top: 8px;
1387
+ color: #8c8c8c;
1388
+ font-size: 12px;
1389
+ line-height: 1.5;
1390
+ `;
1391
+ const stateKey$1 = 'filterMode';
1392
+ function filterMode(opts = {}) {
1393
+ return function filterModeStep(pipeline) {
1394
+ var _a, _b, _c, _d;
1395
+ const inputFilterData = (_c = (_b = (_a = opts.filterData) !== null && _a !== void 0 ? _a : pipeline.getStateAtKey(stateKey$1)) !== null && _b !== void 0 ? _b : opts.defaultFilterData) !== null && _c !== void 0 ? _c : [];
1396
+ const filterData = normalizeFilterData(inputFilterData);
1397
+ const Popover = pipeline.ctx.components.Popover;
1398
+ const columns = pipeline.getColumns();
1399
+ const filterableColumns = getFilterableColumns(columns);
1400
+ if (filterableColumns.length === 0) {
1401
+ return pipeline;
1402
+ }
1403
+ if (Popover == null) {
1404
+ throw new Error('使用 filterMode 之前需要设置 pipeline.ctx.components.Popover');
1405
+ }
1406
+ const onChangeFilterData = (nextFilterData) => {
1407
+ var _a;
1408
+ const normalized = normalizeFilterData(nextFilterData);
1409
+ (_a = opts.onChangeFilterData) === null || _a === void 0 ? void 0 : _a.call(opts, normalized);
1410
+ pipeline.setStateAtKey(stateKey$1, normalized);
1411
+ };
1412
+ if (!opts.keepDataSource) {
1413
+ pipeline.dataSource(filterDataSourceByFilterData(pipeline.getDataSource(), filterableColumns, filterData));
1414
+ }
1415
+ pipeline.columns(processColumns(columns, {
1416
+ filterData,
1417
+ onChangeFilterData,
1418
+ Popover,
1419
+ popoverProps: opts.popoverProps,
1420
+ keepDataSource: Boolean(opts.keepDataSource),
1421
+ FilterHeaderCell: (_d = opts.FilterHeaderCell) !== null && _d !== void 0 ? _d : DefaultFilterHeaderCell,
1422
+ }));
1423
+ return pipeline;
1424
+ };
1425
+ }
1426
+ function stringifyFilterValue(value) {
1427
+ if (value == null) {
1428
+ return '';
1429
+ }
1430
+ return typeof value === 'string' ? value : String(value);
1431
+ }
1432
+ function isBlankFilterValue(value) {
1433
+ return stringifyFilterValue(value).trim() === '';
1434
+ }
1435
+ function processColumns(columns, { filterData, onChangeFilterData, Popover, popoverProps, keepDataSource, FilterHeaderCell, }) {
1436
+ const filterMap = new Map(filterData.map((item) => [item.code, stringifyFilterValue(item.filterValue)]));
1437
+ return columns.map(dfs);
1438
+ function dfs(col) {
1439
+ var _a;
1440
+ const result = Object.assign({}, col);
1441
+ if (isLeafNode(col) && col.code && col.filterProps) {
1442
+ const filterValue = (_a = filterMap.get(col.code)) !== null && _a !== void 0 ? _a : '';
1443
+ result.title = (React.createElement(ManagedFilterHeader, { Popover: Popover, FilterHeaderCell: FilterHeaderCell, column: col, filterModeOptions: { keepDataSource }, filterValue: filterValue, popoverProps: popoverProps, onChangeFilterValue: (nextFilterValue) => {
1444
+ onChangeFilterData(updateFilterData(filterData, col.code, nextFilterValue));
1445
+ } }, internals.safeRenderHeader(col)));
1446
+ }
1447
+ if (!isLeafNode(col)) {
1448
+ result.children = col.children.map(dfs);
1449
+ }
1450
+ return result;
1451
+ }
1452
+ }
1453
+ function ManagedFilterHeader({ Popover, FilterHeaderCell, column, filterModeOptions, filterValue, popoverProps, onChangeFilterValue, children, }) {
1454
+ var _a, _b, _c;
1455
+ const [open, setOpen] = useState(false);
1456
+ const [draftValue, setDraftValue] = useState(filterValue);
1457
+ useEffect(() => {
1458
+ setDraftValue(filterValue);
1459
+ }, [filterValue]);
1460
+ const showSearch = ((_a = column.filterProps) === null || _a === void 0 ? void 0 : _a.showSearch) !== false;
1461
+ const options = (_c = (_b = column.filterProps) === null || _b === void 0 ? void 0 : _b.dataSource) !== null && _c !== void 0 ? _c : [];
1462
+ const active = !isBlankFilterValue(filterValue);
1463
+ const visibleOptions = options.filter((item) => {
1464
+ if (!showSearch || isBlankFilterValue(draftValue)) {
1465
+ return true;
1466
+ }
1467
+ return item.toLowerCase().includes(draftValue.toLowerCase());
1468
+ });
1469
+ const filterPanel = (React.createElement(FilterPanel, { onClick: (event) => {
1470
+ event.stopPropagation();
1471
+ } },
1472
+ showSearch ? (React.createElement(FilterInput, { value: draftValue, placeholder: `筛选${column.name}`, onChange: (event) => {
1473
+ event.stopPropagation();
1474
+ setDraftValue(event.target.value);
1475
+ }, onClick: (event) => {
1476
+ event.stopPropagation();
1477
+ } })) : null,
1478
+ options.length > 0 ? (React.createElement(FilterOptionList, null, visibleOptions.map((item) => (React.createElement(FilterOptionButton, { key: item, type: "button", active: draftValue === item, onClick: (event) => {
1479
+ event.stopPropagation();
1480
+ setDraftValue(item);
1481
+ } }, item))))) : null,
1482
+ !showSearch && options.length === 0 ? React.createElement(FilterEmptyState, null, "\u5F53\u524D\u5217\u6CA1\u6709\u53EF\u9009\u7684\u9884\u7F6E\u7B5B\u9009\u9879\u3002") : null,
1483
+ showSearch && options.length > 0 && visibleOptions.length === 0 ? React.createElement(FilterEmptyState, null, "\u6CA1\u6709\u5339\u914D\u7684\u9884\u7F6E\u7B5B\u9009\u9879\u3002") : null,
1484
+ React.createElement(FilterActionRow, null,
1485
+ React.createElement(FilterActionButton, { type: "button", onClick: (event) => {
1486
+ event.stopPropagation();
1487
+ setDraftValue('');
1488
+ onChangeFilterValue('');
1489
+ setOpen(false);
1490
+ } }, "\u91CD\u7F6E"),
1491
+ React.createElement(FilterActionButton, { primary: true, type: "button", onClick: (event) => {
1492
+ event.stopPropagation();
1493
+ onChangeFilterValue(draftValue);
1494
+ setOpen(false);
1495
+ } }, "\u786E\u8BA4"))));
1496
+ return (React.createElement(FilterHeaderCell, { Popover: Popover, column: column, filterModeOptions: filterModeOptions, filterPanel: filterPanel, filterValue: filterValue, active: active, open: open, setOpen: setOpen, popoverProps: popoverProps }, children));
1497
+ }
1498
+ function DefaultFilterHeaderCell({ Popover, children, column, filterPanel, active, open, setOpen, popoverProps, }) {
1499
+ const justifyContent = column.align === 'right' ? 'flex-end' : column.align === 'center' ? 'center' : 'flex-start';
1500
+ return (React.createElement(FilterHeaderShell, { style: { justifyContent } },
1501
+ children,
1502
+ React.createElement(Popover, Object.assign({ visible: open, content: filterPanel }, popoverProps),
1503
+ React.createElement(FilterTrigger, { active: active, type: "button", "aria-label": `筛选${column.name}`, onClick: (event) => {
1504
+ event.stopPropagation();
1505
+ setOpen(!open);
1506
+ } },
1507
+ React.createElement(icons.Filter, null)))));
1508
+ }
1509
+
1230
1510
  function multiSelect(opts = {}) {
1231
1511
  return function multiSelectStep(pipeline) {
1232
1512
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
@@ -2345,6 +2625,7 @@ var index = /*#__PURE__*/Object.freeze({
2345
2625
  columnRangeHover: columnRangeHover,
2346
2626
  columnResize: columnResize,
2347
2627
  fillRemainingWidth: fillRemainingWidth,
2628
+ filterMode: filterMode,
2348
2629
  multiSelect: multiSelect,
2349
2630
  rowDetail: rowDetail,
2350
2631
  rowPopover: rowPopover,
package/dist/srp-table.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var pipeline = require('./chunks/srp-table-pipeline-f974e586.js');
5
+ var pipeline = require('./chunks/srp-table-pipeline-6f4336b2.js');
6
6
  var React = require('react');
7
7
  var styled = require('styled-components');
8
8
  var rxjs = require('rxjs');
@@ -1255,6 +1255,286 @@ function fillRemainingWidth() {
1255
1255
  };
1256
1256
  }
1257
1257
 
1258
+ function normalizeFilterData(filterData) {
1259
+ const filterMap = new Map();
1260
+ for (const item of filterData !== null && filterData !== void 0 ? filterData : []) {
1261
+ if (item == null || typeof item.code !== 'string' || item.code.length === 0) {
1262
+ continue;
1263
+ }
1264
+ if (isBlankFilterValue$1(item.filterValue)) {
1265
+ filterMap.delete(item.code);
1266
+ continue;
1267
+ }
1268
+ filterMap.set(item.code, {
1269
+ code: item.code,
1270
+ filterValue: stringifyFilterValue$1(item.filterValue),
1271
+ });
1272
+ }
1273
+ return Array.from(filterMap.values());
1274
+ }
1275
+ function updateFilterData(filterData, code, nextFilterValue) {
1276
+ const nextFilterData = normalizeFilterData(filterData).filter((item) => item.code !== code);
1277
+ if (isBlankFilterValue$1(nextFilterValue)) {
1278
+ return nextFilterData;
1279
+ }
1280
+ nextFilterData.push({
1281
+ code,
1282
+ filterValue: stringifyFilterValue$1(nextFilterValue),
1283
+ });
1284
+ return nextFilterData;
1285
+ }
1286
+ function getFilterableColumns(columns) {
1287
+ return pipeline.collectNodes(columns, 'leaf-only').filter((column) => column.code != null && column.filterProps != null);
1288
+ }
1289
+ function filterDataSourceByFilterData(dataSource, columns, filterData) {
1290
+ const activeFilterData = normalizeFilterData(filterData);
1291
+ if (activeFilterData.length === 0) {
1292
+ return dataSource;
1293
+ }
1294
+ const filterColumnsMap = new Map(columns.map((column) => [column.code, column]));
1295
+ return dataSource.filter((record) => {
1296
+ return activeFilterData.every(({ code, filterValue }) => {
1297
+ const column = filterColumnsMap.get(code);
1298
+ if (column == null) {
1299
+ return true;
1300
+ }
1301
+ return matchFilterValue(column, stringifyFilterValue$1(filterValue), record);
1302
+ });
1303
+ });
1304
+ }
1305
+ function matchFilterValue(column, filterValue, record) {
1306
+ var _a;
1307
+ if (isBlankFilterValue$1(filterValue)) {
1308
+ return true;
1309
+ }
1310
+ const customFilter = (_a = column.filterProps) === null || _a === void 0 ? void 0 : _a.filterOptions;
1311
+ if (typeof customFilter === 'function') {
1312
+ return customFilter(filterValue, record);
1313
+ }
1314
+ const rawValue = record === null || record === void 0 ? void 0 : record[column.code];
1315
+ if (rawValue == null) {
1316
+ return false;
1317
+ }
1318
+ return String(rawValue).toLowerCase().includes(filterValue.toLowerCase());
1319
+ }
1320
+ function isBlankFilterValue$1(value) {
1321
+ return stringifyFilterValue$1(value).trim() === '';
1322
+ }
1323
+ function stringifyFilterValue$1(value) {
1324
+ if (value == null) {
1325
+ return '';
1326
+ }
1327
+ return typeof value === 'string' ? value : String(value);
1328
+ }
1329
+
1330
+ const FilterHeaderShell = styled__default["default"].div `
1331
+ display: flex;
1332
+ align-items: center;
1333
+ `;
1334
+ const FilterTrigger = styled__default["default"].button `
1335
+ display: inline-flex;
1336
+ align-items: center;
1337
+ justify-content: center;
1338
+ width: 20px;
1339
+ height: 20px;
1340
+ margin-left: 4px;
1341
+ padding: 0;
1342
+ border: none;
1343
+ background: transparent;
1344
+ color: ${(props) => (props.active ? '#23A3FF' : '#8c8c8c')};
1345
+ cursor: pointer;
1346
+
1347
+ &:hover {
1348
+ color: ${(props) => (props.active ? '#23A3FF' : '#595959')};
1349
+ }
1350
+ `;
1351
+ const FilterPanel = styled__default["default"].div `
1352
+ width: 160px;
1353
+ padding: 8px 8px 6px;
1354
+ box-sizing: border-box;
1355
+ color: #262626;
1356
+ font-size: 12px;
1357
+ line-height: 1.5;
1358
+ `;
1359
+ const FilterInput = styled__default["default"].input `
1360
+ width: 100%;
1361
+ height: 28px;
1362
+ padding: 0 8px;
1363
+ box-sizing: border-box;
1364
+ border: 1px solid #d9d9d9;
1365
+ border-radius: 5px;
1366
+ outline: none;
1367
+ font-size: 12px;
1368
+ line-height: 1.5;
1369
+
1370
+ &:focus {
1371
+ border-color: #23a3ff;
1372
+ }
1373
+ `;
1374
+ const FilterOptionList = styled__default["default"].div `
1375
+ max-height: 136px;
1376
+ margin-top: 6px;
1377
+ overflow: auto;
1378
+ `;
1379
+ const FilterOptionButton = styled__default["default"].button `
1380
+ width: 100%;
1381
+ min-height: 28px;
1382
+ margin-top: 4px;
1383
+ padding: 3px 8px;
1384
+ box-sizing: border-box;
1385
+ border: 1px solid ${(props) => (props.active ? '#23A3FF' : '#e8e8e8')};
1386
+ border-radius: 5px;
1387
+ background: ${(props) => (props.active ? 'rgba(35, 163, 255, 0.12)' : '#fff')};
1388
+ color: #262626;
1389
+ font-size: 12px;
1390
+ line-height: 1.5;
1391
+ text-align: left;
1392
+ cursor: pointer;
1393
+ `;
1394
+ const FilterActionRow = styled__default["default"].div `
1395
+ display: flex;
1396
+ justify-content: flex-end;
1397
+ gap: 4px;
1398
+ margin-top: 6px;
1399
+ `;
1400
+ const FilterActionButton = styled__default["default"].button `
1401
+ min-width: 44px;
1402
+ height: 26px;
1403
+ padding: 0 8px;
1404
+ box-sizing: border-box;
1405
+ border: 1px solid ${(props) => (props.primary ? '#23A3FF' : '#d9d9d9')};
1406
+ border-radius: 999px;
1407
+ background: ${(props) => (props.primary ? '#23A3FF' : '#fff')};
1408
+ color: ${(props) => (props.primary ? '#fff' : '#262626')};
1409
+ font-size: 12px;
1410
+ line-height: 1;
1411
+ cursor: pointer;
1412
+ `;
1413
+ const FilterEmptyState = styled__default["default"].div `
1414
+ margin-top: 8px;
1415
+ color: #8c8c8c;
1416
+ font-size: 12px;
1417
+ line-height: 1.5;
1418
+ `;
1419
+ const stateKey$1 = 'filterMode';
1420
+ function filterMode(opts = {}) {
1421
+ return function filterModeStep(pipeline) {
1422
+ var _a, _b, _c, _d;
1423
+ const inputFilterData = (_c = (_b = (_a = opts.filterData) !== null && _a !== void 0 ? _a : pipeline.getStateAtKey(stateKey$1)) !== null && _b !== void 0 ? _b : opts.defaultFilterData) !== null && _c !== void 0 ? _c : [];
1424
+ const filterData = normalizeFilterData(inputFilterData);
1425
+ const Popover = pipeline.ctx.components.Popover;
1426
+ const columns = pipeline.getColumns();
1427
+ const filterableColumns = getFilterableColumns(columns);
1428
+ if (filterableColumns.length === 0) {
1429
+ return pipeline;
1430
+ }
1431
+ if (Popover == null) {
1432
+ throw new Error('使用 filterMode 之前需要设置 pipeline.ctx.components.Popover');
1433
+ }
1434
+ const onChangeFilterData = (nextFilterData) => {
1435
+ var _a;
1436
+ const normalized = normalizeFilterData(nextFilterData);
1437
+ (_a = opts.onChangeFilterData) === null || _a === void 0 ? void 0 : _a.call(opts, normalized);
1438
+ pipeline.setStateAtKey(stateKey$1, normalized);
1439
+ };
1440
+ if (!opts.keepDataSource) {
1441
+ pipeline.dataSource(filterDataSourceByFilterData(pipeline.getDataSource(), filterableColumns, filterData));
1442
+ }
1443
+ pipeline.columns(processColumns(columns, {
1444
+ filterData,
1445
+ onChangeFilterData,
1446
+ Popover,
1447
+ popoverProps: opts.popoverProps,
1448
+ keepDataSource: Boolean(opts.keepDataSource),
1449
+ FilterHeaderCell: (_d = opts.FilterHeaderCell) !== null && _d !== void 0 ? _d : DefaultFilterHeaderCell,
1450
+ }));
1451
+ return pipeline;
1452
+ };
1453
+ }
1454
+ function stringifyFilterValue(value) {
1455
+ if (value == null) {
1456
+ return '';
1457
+ }
1458
+ return typeof value === 'string' ? value : String(value);
1459
+ }
1460
+ function isBlankFilterValue(value) {
1461
+ return stringifyFilterValue(value).trim() === '';
1462
+ }
1463
+ function processColumns(columns, { filterData, onChangeFilterData, Popover, popoverProps, keepDataSource, FilterHeaderCell, }) {
1464
+ const filterMap = new Map(filterData.map((item) => [item.code, stringifyFilterValue(item.filterValue)]));
1465
+ return columns.map(dfs);
1466
+ function dfs(col) {
1467
+ var _a;
1468
+ const result = Object.assign({}, col);
1469
+ if (pipeline.isLeafNode(col) && col.code && col.filterProps) {
1470
+ const filterValue = (_a = filterMap.get(col.code)) !== null && _a !== void 0 ? _a : '';
1471
+ result.title = (React__default["default"].createElement(ManagedFilterHeader, { Popover: Popover, FilterHeaderCell: FilterHeaderCell, column: col, filterModeOptions: { keepDataSource }, filterValue: filterValue, popoverProps: popoverProps, onChangeFilterValue: (nextFilterValue) => {
1472
+ onChangeFilterData(updateFilterData(filterData, col.code, nextFilterValue));
1473
+ } }, pipeline.internals.safeRenderHeader(col)));
1474
+ }
1475
+ if (!pipeline.isLeafNode(col)) {
1476
+ result.children = col.children.map(dfs);
1477
+ }
1478
+ return result;
1479
+ }
1480
+ }
1481
+ function ManagedFilterHeader({ Popover, FilterHeaderCell, column, filterModeOptions, filterValue, popoverProps, onChangeFilterValue, children, }) {
1482
+ var _a, _b, _c;
1483
+ const [open, setOpen] = React.useState(false);
1484
+ const [draftValue, setDraftValue] = React.useState(filterValue);
1485
+ React.useEffect(() => {
1486
+ setDraftValue(filterValue);
1487
+ }, [filterValue]);
1488
+ const showSearch = ((_a = column.filterProps) === null || _a === void 0 ? void 0 : _a.showSearch) !== false;
1489
+ const options = (_c = (_b = column.filterProps) === null || _b === void 0 ? void 0 : _b.dataSource) !== null && _c !== void 0 ? _c : [];
1490
+ const active = !isBlankFilterValue(filterValue);
1491
+ const visibleOptions = options.filter((item) => {
1492
+ if (!showSearch || isBlankFilterValue(draftValue)) {
1493
+ return true;
1494
+ }
1495
+ return item.toLowerCase().includes(draftValue.toLowerCase());
1496
+ });
1497
+ const filterPanel = (React__default["default"].createElement(FilterPanel, { onClick: (event) => {
1498
+ event.stopPropagation();
1499
+ } },
1500
+ showSearch ? (React__default["default"].createElement(FilterInput, { value: draftValue, placeholder: `筛选${column.name}`, onChange: (event) => {
1501
+ event.stopPropagation();
1502
+ setDraftValue(event.target.value);
1503
+ }, onClick: (event) => {
1504
+ event.stopPropagation();
1505
+ } })) : null,
1506
+ options.length > 0 ? (React__default["default"].createElement(FilterOptionList, null, visibleOptions.map((item) => (React__default["default"].createElement(FilterOptionButton, { key: item, type: "button", active: draftValue === item, onClick: (event) => {
1507
+ event.stopPropagation();
1508
+ setDraftValue(item);
1509
+ } }, item))))) : null,
1510
+ !showSearch && options.length === 0 ? React__default["default"].createElement(FilterEmptyState, null, "\u5F53\u524D\u5217\u6CA1\u6709\u53EF\u9009\u7684\u9884\u7F6E\u7B5B\u9009\u9879\u3002") : null,
1511
+ showSearch && options.length > 0 && visibleOptions.length === 0 ? React__default["default"].createElement(FilterEmptyState, null, "\u6CA1\u6709\u5339\u914D\u7684\u9884\u7F6E\u7B5B\u9009\u9879\u3002") : null,
1512
+ React__default["default"].createElement(FilterActionRow, null,
1513
+ React__default["default"].createElement(FilterActionButton, { type: "button", onClick: (event) => {
1514
+ event.stopPropagation();
1515
+ setDraftValue('');
1516
+ onChangeFilterValue('');
1517
+ setOpen(false);
1518
+ } }, "\u91CD\u7F6E"),
1519
+ React__default["default"].createElement(FilterActionButton, { primary: true, type: "button", onClick: (event) => {
1520
+ event.stopPropagation();
1521
+ onChangeFilterValue(draftValue);
1522
+ setOpen(false);
1523
+ } }, "\u786E\u8BA4"))));
1524
+ return (React__default["default"].createElement(FilterHeaderCell, { Popover: Popover, column: column, filterModeOptions: filterModeOptions, filterPanel: filterPanel, filterValue: filterValue, active: active, open: open, setOpen: setOpen, popoverProps: popoverProps }, children));
1525
+ }
1526
+ function DefaultFilterHeaderCell({ Popover, children, column, filterPanel, active, open, setOpen, popoverProps, }) {
1527
+ const justifyContent = column.align === 'right' ? 'flex-end' : column.align === 'center' ? 'center' : 'flex-start';
1528
+ return (React__default["default"].createElement(FilterHeaderShell, { style: { justifyContent } },
1529
+ children,
1530
+ React__default["default"].createElement(Popover, Object.assign({ visible: open, content: filterPanel }, popoverProps),
1531
+ React__default["default"].createElement(FilterTrigger, { active: active, type: "button", "aria-label": `筛选${column.name}`, onClick: (event) => {
1532
+ event.stopPropagation();
1533
+ setOpen(!open);
1534
+ } },
1535
+ React__default["default"].createElement(pipeline.icons.Filter, null)))));
1536
+ }
1537
+
1258
1538
  function multiSelect(opts = {}) {
1259
1539
  return function multiSelectStep(pipeline$1) {
1260
1540
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
@@ -2373,6 +2653,7 @@ var index = /*#__PURE__*/Object.freeze({
2373
2653
  columnRangeHover: columnRangeHover,
2374
2654
  columnResize: columnResize,
2375
2655
  fillRemainingWidth: fillRemainingWidth,
2656
+ filterMode: filterMode,
2376
2657
  multiSelect: multiSelect,
2377
2658
  rowDetail: rowDetail,
2378
2659
  rowPopover: rowPopover,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guozhi5658/srp-table",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "High-performance React table component library",
5
5
  "sideEffects": false,
6
6
  "module": "dist/srp-table.esm.js",