From 27fee5ecb004613f55ac769dd27a095207d6e367 Mon Sep 17 00:00:00 2001 From: Nancy <68706811+nancy-dassana@users.noreply.github.com> Date: Mon, 19 Jul 2021 16:11:30 -0700 Subject: [PATCH] fix #392 - Return array values properly for getJSONPathValue (#393) Closes #392 --- rollup.config.ts | 1 + src/components/Table/utils.tsx | 16 +- src/components/assets/images/404_error_2.svg | 212 +++++++++++++++---- src/components/utils.ts | 17 +- 4 files changed, 201 insertions(+), 45 deletions(-) diff --git a/rollup.config.ts b/rollup.config.ts index 9d33bced..1650036b 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -55,6 +55,7 @@ export default { 'react-helmet', 'react-hook-form', 'react-jss', + 'react-scroll', 'remark-gfm', 'react-markdown', 'typescript', diff --git a/src/components/Table/utils.tsx b/src/components/Table/utils.tsx index b9414b34..aaec1955 100644 --- a/src/components/Table/utils.tsx +++ b/src/components/Table/utils.tsx @@ -99,7 +99,10 @@ export function processData( columns.forEach(col => { const { dataIndex } = col - const value = getJSONPathValue(`$.${dataIndex}`, item) + const value = getJSONPathValue( + `$.${dataIndex}`, + item + ) as ProcessedData[keyof TableData] if (value) { partialData[dataIndex as keyof TableData] = value @@ -186,9 +189,9 @@ const compareIcons = ? `$.${dataIndex}.${iconKey}` : `$.${dataIndex}` - const compareValA = getJSONPathValue(jsonPath, a) || '' + const compareValA = getJSONPathValue(jsonPath, a)?.toString() || '' - const compareValB = getJSONPathValue(jsonPath, b) || '' + const compareValB = getJSONPathValue(jsonPath, b)?.toString() || '' return compareValA.localeCompare(compareValB) } @@ -299,7 +302,7 @@ const getIconOrIconKey = ( jsonPath: string ): string | IconName | undefined => { if (typeof record === 'object') { - const value = getJSONPathValue(jsonPath, record) + const value = getJSONPathValue(jsonPath, record)?.toString() if (value) return value } else return record @@ -505,7 +508,10 @@ function applyRender( const labelVal = typeof record === 'string' ? labelKey - : getJSONPathValue(jsonPath, record) + : (getJSONPathValue( + jsonPath, + record + ) as string) // eslint-disable-line no-mixed-spaces-and-tabs return ( - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/components/utils.ts b/src/components/utils.ts index 1ea3298c..0a80a807 100644 --- a/src/components/utils.ts +++ b/src/components/utils.ts @@ -126,16 +126,27 @@ export const getDataTestAttributeProp = ( [TAG]: dataTag ? `${cmpName}-${dataTag}` : cmpName }) -export const getJSONPathValue = ( +export const getJSONPathValue = ( path: string, obj: Record ) => { - const value = JSONPath({ + const value = JSONPath({ json: obj, path }) - if (value && Array.isArray(value)) return value[0] + if (Array.isArray(value)) { + switch (value.length) { + case 0: + return undefined + case 1: + return value[0] as T + default: + return value as T[] + } + } + + return value } /**