Skip to content

Commit ca76ddb

Browse files
authored
Merge pull request #123 from UgnisSoftware/UGN-359
bug UGN-359 - fix trimmed rows if header.length < row.length.
2 parents 3f79b9e + 69a1e6c commit ca76ddb

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

src/steps/MatchColumnsStep/MatchColumnsStep.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export const MatchColumnsStep = <T extends string>({ data, headerValues, onConti
6868
const { fields, autoMapHeaders, autoMapDistance, translations } = useRsi<T>()
6969
const [isLoading, setIsLoading] = useState(false)
7070
const [columns, setColumns] = useState<Columns<T>>(
71-
(headerValues as string[]).map((value, index) => ({ type: ColumnType.empty, index, header: value })),
71+
// Do not remove spread, it indexes empty array elements, otherwise map() skips over them
72+
([...headerValues] as string[]).map((value, index) => ({ type: ColumnType.empty, index, header: value ?? "" })),
7273
)
7374
const [showUnmatchedFieldsAlert, setShowUnmatchedFieldsAlert] = useState(false)
7475

src/steps/MatchColumnsStep/components/ColumnGrid.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const ColumnGrid = <T extends string>({
4040
<Text sx={styles.title}>{translations.matchColumnsStep.userTableTitle}</Text>
4141
</Box>
4242
{columns.map((column, index) => (
43-
<Box gridRow="2/3" gridColumn={`${index + 2}/${index + 3}`} pt={3} key={column.header}>
43+
<Box gridRow="2/3" gridColumn={`${index + 2}/${index + 3}`} pt={3} key={column.header + index}>
4444
{userColumn(column)}
4545
</Box>
4646
))}
@@ -50,7 +50,14 @@ export const ColumnGrid = <T extends string>({
5050
</Box>
5151
<FadingWrapper gridColumn={`1/${columns.length + 3}`} gridRow="4/5" />
5252
{columns.map((column, index) => (
53-
<Box gridRow="4/5" gridColumn={`${index + 2}/${index + 3}`} key={column.index} py="1.125rem" pl={2} pr={3}>
53+
<Box
54+
gridRow="4/5"
55+
gridColumn={`${index + 2}/${index + 3}`}
56+
key={column.header + index}
57+
py="1.125rem"
58+
pl={2}
59+
pr={3}
60+
>
5461
{templateColumn(column)}
5562
</Box>
5663
))}

src/steps/MatchColumnsStep/utils/normalizeTableData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { normalizeCheckboxValue } from "./normalizeCheckboxValue"
55

66
export const normalizeTableData = <T extends string>(columns: Columns<T>, data: RawData[], fields: Fields<T>) =>
77
data.map((row) =>
8-
row.reduce((acc, curr, index) => {
9-
const column = columns[index]
8+
columns.reduce((acc, column, index) => {
9+
const curr = row[index]
1010
switch (column.type) {
1111
case ColumnType.matchedCheckbox: {
1212
const field = fields.find((field) => field.key === column.value)!

src/steps/SelectHeaderStep/components/columns.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ export const SelectColumn: Column<any, any> = {
3535
formatter: SelectFormatter,
3636
}
3737

38-
export const generateSelectionColumns = (data: RawData[]) => [
39-
SelectColumn,
40-
...data?.[0].map(
41-
(value, index): Column<any, any> => ({
38+
export const generateSelectionColumns = (data: RawData[]) => {
39+
const longestRowLength = data.reduce((acc, curr) => (acc > curr.length ? acc : curr.length), 0)
40+
return [
41+
SelectColumn,
42+
...Array.from(Array(longestRowLength), (_, index) => ({
4243
key: index.toString(),
4344
name: "",
44-
}),
45-
),
46-
]
45+
})),
46+
]
47+
}

src/steps/SelectHeaderStep/tests/SelectHeaderStep.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const CONTINUE_BUTTON = "Next"
1414
const ERROR_MESSAGE = "Something happened"
1515
const RAW_DATE = "2020-03-03"
1616
const FORMATTED_DATE = "2020/03/03"
17+
const TRAILING_CELL = "trailingcell"
1718

1819
test("Select header row and click next", async () => {
1920
const data = [
@@ -168,3 +169,33 @@ test("dateFormat property should be applied to dates read from xlsx files", asyn
168169
const el = await screen.findByText(FORMATTED_DATE, undefined, { timeout: 10000 })
169170
expect(el).toBeInTheDocument()
170171
})
172+
173+
test(
174+
"trailing (not under a header) cells should be rendered in SelectHeaderStep table, " +
175+
"but not in MatchColumnStep if a shorter row is selected as a header",
176+
async () => {
177+
const selectHeaderStepHook = jest.fn(async (headerValues, data) => {
178+
return { headerValues, data }
179+
})
180+
render(<ReactSpreadsheetImport {...mockRsiValues} selectHeaderStepHook={selectHeaderStepHook} />)
181+
const uploader = screen.getByTestId("rsi-dropzone")
182+
const data = readFileSync(__dirname + "/../../../../static/TrailingCellsWorkbook.xlsx")
183+
fireEvent.drop(uploader, {
184+
target: {
185+
files: [
186+
new File([data], "testFile.xlsx", {
187+
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
188+
}),
189+
],
190+
},
191+
})
192+
const trailingCell = await screen.findByText(TRAILING_CELL, undefined, { timeout: 10000 })
193+
expect(trailingCell).toBeInTheDocument()
194+
const nextButton = screen.getByRole("button", {
195+
name: "Next",
196+
})
197+
userEvent.click(nextButton)
198+
const trailingCellNextPage = await screen.findByText(TRAILING_CELL, undefined, { timeout: 10000 })
199+
expect(trailingCellNextPage).not.toBeInTheDocument()
200+
},
201+
)

static/TrailingCellsWorkbook.xlsx

23.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)