Skip to content

Commit

Permalink
fix(table): Stringify non-primitive datatypes before display in react…
Browse files Browse the repository at this point in the history
…-table
  • Loading branch information
hydrosquall committed Jun 29, 2021
1 parent 64f9565 commit 4b3f411
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/charts/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type OnChangeProps = (input: number | string) => void;
// Only some field types are filterable
// Iterate over a union type
// https://stackoverflow.com/a/59420158/5129731
// Members come from values of Field.type
const filterableFields= ['integer' , 'number' , 'string'] as const;
type FilterIndexSignature = typeof filterableFields[number];

Expand Down Expand Up @@ -156,6 +157,13 @@ function isFilterableFieldType (fieldType: any): fieldType is FilterIndexSignatu
return filterableFieldSet.has(fieldType);
}

// Non-primitive field types cannot be outputted directly as React children
// Members come from possible values of Field.type
const shouldStringifyFieldSet = new Set([
'boolean',
'object'
])

class DataResourceTransformGrid extends React.PureComponent<Props, State> {
static defaultProps = {
metadata: {},
Expand Down Expand Up @@ -206,8 +214,8 @@ class DataResourceTransformGrid extends React.PureComponent<Props, State> {
Header: field.name,
id: field.name,
accessor: (rowValue: { [key: string]: any }) => {
return field.type === "boolean"
? rowValue[field.name].toString()
return shouldStringifyFieldSet.has(field.type)
? JSON.stringify(rowValue[field.name])
: rowValue[field.name];
},
fixed: primaryKey.indexOf(field.name) !== -1 && "left",
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface Field {
name: 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
type: 'string' | 'integer' | 'number' | 'boolean' | 'datetime' | 'object' | string; // Other types are permitted, but not explicitly handled
}


Expand Down

0 comments on commit 4b3f411

Please sign in to comment.