Skip to content

Commit

Permalink
refactor(table): explicitly mark accepted field table for grid view
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrosquall committed Jun 29, 2021
1 parent 4e65d36 commit 64f9565
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
33 changes: 18 additions & 15 deletions src/charts/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const switchMode = (currentMode: string) => {

type OnChangeProps = (input: number | string) => void;

type FilterIndexSignature = "integer" | "number" | "string";
// Only some field types are filterable
// Iterate over a union type
// https://stackoverflow.com/a/59420158/5129731
const filterableFields= ['integer' , 'number' , 'string'] as const;
type FilterIndexSignature = typeof filterableFields[number];

interface NumberFilterProps {
onChange: OnChangeProps;
Expand Down Expand Up @@ -147,6 +151,11 @@ interface Props {
theme?: string;
}

const filterableFieldSet = new Set(filterableFields);
function isFilterableFieldType (fieldType: any): fieldType is FilterIndexSignature {
return filterableFieldSet.has(fieldType);
}

class DataResourceTransformGrid extends React.PureComponent<Props, State> {
static defaultProps = {
metadata: {},
Expand All @@ -173,21 +182,13 @@ class DataResourceTransformGrid extends React.PureComponent<Props, State> {
const { primaryKey = [] } = schema;

const tableColumns = schema.fields.map((field: Dx.Field) => {
if (
field.type === "string" ||
field.type === "number" ||
field.type === "integer"
) {
if (isFilterableFieldType(field.type)) {
return {
Header: field.name,
accessor: field.name,
fixed: primaryKey.indexOf(field.name) !== -1 && "left",
filterMethod: (filter: Dx.JSONObject, row: Dx.JSONObject) => {
if (
field.type === "string" ||
field.type === "number" ||
field.type === "integer"
) {
if (isFilterableFieldType(field.type)) {
return filterMethod[field.type](filters[field.name])(filter, row);
}
},
Expand All @@ -197,17 +198,19 @@ class DataResourceTransformGrid extends React.PureComponent<Props, State> {
field.name,
(newFilter: { [key: string]: Function }) => {
this.setState({ filters: { ...filters, ...newFilter } });
}
)
},
),
};
} else {
return {
Header: field.name,
id: field.name,
accessor: (rowValue: { [key: string]: any }) => {
return field.type === "boolean" ? rowValue[field.name].toString() : rowValue[field.name]
return field.type === "boolean"
? rowValue[field.name].toString()
: rowValue[field.name];
},
fixed: primaryKey.indexOf(field.name) !== -1 && "left"
fixed: primaryKey.indexOf(field.name) !== -1 && "left",
};
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/utilities/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export interface Schema {
export const defaultPrimaryKey = "dx-default-pk";
export interface Field {
name: string;
type: string;
// Types are based on the json-schema standard, with some variations
// https://specs.frictionlessdata.io/table-schema/#types-and-formats
type: 'string' | 'integer' | 'number' | 'boolean' | 'datetime' | string; // Other types are permitted, but not explicitly handled
}


Expand Down

0 comments on commit 64f9565

Please sign in to comment.