mig-schema-table 3.0.60 → 3.0.61

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.
@@ -15,7 +15,6 @@ interface IColumnFilterMap {
15
15
  export interface ISchemaTableProps<T> {
16
16
  Heading?: any;
17
17
  checkedIndexes?: number[];
18
- disabledCheckedIndexes?: number[];
19
18
  config?: {
20
19
  [propName: string]: IColumnConfig<T>;
21
20
  };
@@ -24,13 +23,14 @@ export interface ISchemaTableProps<T> {
24
23
  defaultColumnFilters?: IColumnFilterMap;
25
24
  defaultSortAsc?: boolean;
26
25
  defaultSortColumn?: keyof T;
26
+ disabledCheckedIndexes?: number[];
27
+ enableAutoFocus?: boolean;
27
28
  getRowClassName?: (rowData: T, dataIndex: number, filteredSortedData: IRenderData[]) => string;
28
29
  getRowSelected?: (rowData: T, dataIndex: number) => boolean;
29
- maxHeight?: number;
30
- isSearchable?: boolean;
31
- enableAutoFocus?: boolean;
32
30
  isColumnFilterable?: boolean;
31
+ isSearchable?: boolean;
33
32
  isSortable?: boolean;
33
+ maxHeight?: number;
34
34
  onCheckedIndexesChange?: (dataIndex: number[]) => void;
35
35
  onRowClick?: (rowData: T, dataIndex: number, event: React.MouseEvent) => void;
36
36
  onRowDoubleClick?: (rowData: T, dataIndex: number, event: React.MouseEvent) => void;
@@ -38,6 +38,7 @@ export interface ISchemaTableProps<T> {
38
38
  schema: oas31.SchemaObject;
39
39
  searchPlaceholder?: string;
40
40
  style?: React.CSSProperties;
41
+ useFilterStateHash?: boolean;
41
42
  width: number;
42
43
  }
43
44
  export interface IDateColumnFilterValue {
@@ -46,6 +47,6 @@ export interface IDateColumnFilterValue {
46
47
  filterEmpty?: boolean;
47
48
  }
48
49
  export type TColumnFilterValue = string | number | boolean | IDateColumnFilterValue;
49
- declare function SchemaTable<T>({ Heading, checkedIndexes, disabledCheckedIndexes, config, customElement, data, defaultColumnFilters, defaultSortAsc, defaultSortColumn, getRowClassName, getRowSelected, maxHeight, isSearchable, isColumnFilterable, isSortable, onCheckedIndexesChange, onRowClick, rowHeight, schema, searchPlaceholder, style, width, onRowDoubleClick, enableAutoFocus, }: ISchemaTableProps<T>): import("react/jsx-runtime").JSX.Element;
50
+ declare function SchemaTable<T>({ Heading, checkedIndexes, config, customElement, data, defaultColumnFilters, defaultSortAsc, defaultSortColumn, disabledCheckedIndexes, enableAutoFocus, getRowClassName, getRowSelected, isColumnFilterable, isSearchable, isSortable, maxHeight, onCheckedIndexesChange, onRowClick, onRowDoubleClick, rowHeight, schema, searchPlaceholder, style, useFilterStateHash, width, }: ISchemaTableProps<T>): import("react/jsx-runtime").JSX.Element;
50
51
  declare const _default: typeof SchemaTable;
51
52
  export default _default;
@@ -8,6 +8,7 @@ import { SELECT_ALL_COLUMN_NAME, SELECT_ALL_COLUMN_WIDTH } from "./constants";
8
8
  import Td from "./Td";
9
9
  import { DEFAULT_DATE_FORMAT, DEFAULT_DATE_TIME_FORMAT } from "../inc/constant";
10
10
  import SchemaColumnFilterPopover from "./SchemaColumnFilterPopover";
11
+ import { parseLocationHash, serializeLocationHash } from "../inc/data";
11
12
  const startOfTheWorldDate = new Date("1000-01-01 00:00:00Z");
12
13
  function getSortByValue(propSchema, propConfig) {
13
14
  var _a;
@@ -23,14 +24,15 @@ function getSortByValue(propSchema, propConfig) {
23
24
  propSchema.type === "integer" ||
24
25
  !!(propConfig === null || propConfig === void 0 ? void 0 : propConfig.renderCell));
25
26
  }
26
- function SchemaTable({ Heading = VariableSizeList, checkedIndexes, disabledCheckedIndexes, config, customElement, data, defaultColumnFilters = {}, defaultSortAsc = false, defaultSortColumn, getRowClassName, getRowSelected, maxHeight, isSearchable, isColumnFilterable, isSortable, onCheckedIndexesChange, onRowClick, rowHeight = 36, schema, searchPlaceholder, style, width, onRowDoubleClick, enableAutoFocus, }) {
27
+ function SchemaTable({ Heading = VariableSizeList, checkedIndexes, config, customElement, data, defaultColumnFilters = {}, defaultSortAsc = false, defaultSortColumn, disabledCheckedIndexes, enableAutoFocus, getRowClassName, getRowSelected, isColumnFilterable, isSearchable, isSortable, maxHeight, onCheckedIndexesChange, onRowClick, onRowDoubleClick, rowHeight = 36, schema, searchPlaceholder, style, useFilterStateHash, width, }) {
27
28
  const [sortColumn, setSortColumn] = React.useState(defaultSortColumn);
28
29
  const [sortAsc, setSortAsc] = React.useState(defaultSortAsc);
29
- const [searchQuery, setSearchQuery] = React.useState("");
30
- const [columnFilterMap, setColumnFilterMap] = React.useState(defaultColumnFilters);
31
30
  const [popoverConfig, setPopoverConfig] = React.useState();
32
31
  const isDataFunction = data instanceof Function;
33
32
  const [sourceData, setSourceData] = React.useState(isDataFunction ? undefined : data);
33
+ const [locationHash, setLocationHash] = React.useState(parseLocationHash(window.location.hash));
34
+ const [searchQuery, setSearchQuery] = React.useState(locationHash.searchQuery || "");
35
+ const [columnFilterMap, setColumnFilterMap] = React.useState(locationHash.columnFilterMap || defaultColumnFilters);
34
36
  const [isDirty, setIsDirty] = React.useState(false);
35
37
  React.useEffect(() => {
36
38
  if (isDataFunction) {
@@ -414,8 +416,12 @@ function SchemaTable({ Heading = VariableSizeList, checkedIndexes, disabledCheck
414
416
  if (isDataFunction) {
415
417
  setIsDirty(true);
416
418
  }
419
+ if (useFilterStateHash) {
420
+ window.location.hash = serializeLocationHash(Object.assign(Object.assign({}, locationHash), { searchQuery: e.currentTarget.value }));
421
+ return;
422
+ }
417
423
  setSearchQuery(e.currentTarget.value);
418
- }, [isDataFunction]);
424
+ }, [isDataFunction, locationHash, useFilterStateHash]);
419
425
  const refreshData = React.useCallback(() => {
420
426
  setIsDirty(false);
421
427
  setSourceData(undefined);
@@ -441,6 +447,29 @@ function SchemaTable({ Heading = VariableSizeList, checkedIndexes, disabledCheck
441
447
  const onPopoverClose = React.useCallback(() => {
442
448
  setPopoverConfig(undefined);
443
449
  }, []);
450
+ React.useEffect(() => {
451
+ if (!useFilterStateHash) {
452
+ return;
453
+ }
454
+ const onHashChange = () => {
455
+ setLocationHash(parseLocationHash(window.location.hash));
456
+ };
457
+ window.addEventListener("hashchange", onHashChange);
458
+ return () => {
459
+ window.removeEventListener("hashchange", onHashChange);
460
+ };
461
+ }, [useFilterStateHash]);
462
+ React.useEffect(() => {
463
+ if (!useFilterStateHash) {
464
+ return;
465
+ }
466
+ if (locationHash.columnFilterMap) {
467
+ setColumnFilterMap(locationHash.columnFilterMap);
468
+ }
469
+ if (locationHash.searchQuery !== undefined) {
470
+ setSearchQuery(locationHash.searchQuery);
471
+ }
472
+ }, [locationHash, useFilterStateHash]);
444
473
  const onSchemaColumnFilterChange = React.useCallback((newColumnFilterValue) => {
445
474
  if (!popoverConfig) {
446
475
  return;
@@ -452,8 +481,19 @@ function SchemaTable({ Heading = VariableSizeList, checkedIndexes, disabledCheck
452
481
  disableColumnFilter(popoverConfig.propName);
453
482
  return;
454
483
  }
484
+ if (useFilterStateHash) {
485
+ window.location.hash = serializeLocationHash(Object.assign(Object.assign({}, locationHash), { columnFilterMap: Object.assign(Object.assign({}, columnFilterMap), { [popoverConfig.propName]: newColumnFilterValue }) }));
486
+ return;
487
+ }
455
488
  setColumnFilterMap((columnFilterMap) => (Object.assign(Object.assign({}, columnFilterMap), { [popoverConfig.propName]: newColumnFilterValue })));
456
- }, [disableColumnFilter, isDataFunction, popoverConfig]);
489
+ }, [
490
+ columnFilterMap,
491
+ disableColumnFilter,
492
+ isDataFunction,
493
+ locationHash,
494
+ popoverConfig,
495
+ useFilterStateHash,
496
+ ]);
457
497
  const searchInputAutoFocus = React.useMemo(() => {
458
498
  if (enableAutoFocus === undefined) {
459
499
  return true;
@@ -0,0 +1,2 @@
1
+ export declare const parseLocationHash: <T extends {}>(search: string) => T;
2
+ export declare const serializeLocationHash: (params: {}) => string;
@@ -0,0 +1,24 @@
1
+ export const parseLocationHash = (search) => {
2
+ if (!search.length) {
3
+ return {};
4
+ }
5
+ const result = {};
6
+ search
7
+ .substring(1)
8
+ .split("&")
9
+ .forEach((pair) => {
10
+ if (!pair) {
11
+ return;
12
+ }
13
+ const splitPair = pair.split("=");
14
+ if (splitPair.length !== 2) {
15
+ return;
16
+ }
17
+ // @ts-ignore
18
+ result[decodeURIComponent(splitPair[0])] = JSON.parse(decodeURIComponent(splitPair[1]));
19
+ });
20
+ return result;
21
+ };
22
+ export const serializeLocationHash = (params) => `${Object.entries(params)
23
+ .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`)
24
+ .join("&")}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mig-schema-table",
3
- "version": "3.0.60",
3
+ "version": "3.0.61",
4
4
  "main": "dist/index.js",
5
5
  "files": [
6
6
  "dist/"