Skip to content

Commit cdb4f3c

Browse files
authored
Merge pull request #108 from UgnisSoftware/UGN-354
chore UGN-354 - update readme to include initialStepState
2 parents adf8540 + 4a556ce commit cdb4f3c

5 files changed

Lines changed: 82 additions & 27 deletions

File tree

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,60 @@ Example:
124124
/>
125125
```
126126

127+
### Initial state
128+
129+
In rare case when you need to skip the beginning of he flow, you can start the flow from any of the steps.
130+
131+
- **initialStepState** - initial state of component that will be rendered on load.
132+
133+
```tsx
134+
initialStepState?: StepState
135+
136+
type StepState =
137+
| {
138+
type: StepType.upload
139+
}
140+
| {
141+
type: StepType.selectSheet
142+
workbook: XLSX.WorkBook
143+
}
144+
| {
145+
type: StepType.selectHeader
146+
data: RawData[]
147+
}
148+
| {
149+
type: StepType.matchColumns
150+
data: RawData[]
151+
headerValues: RawData
152+
}
153+
| {
154+
type: StepType.validateData
155+
data: any[]
156+
}
157+
158+
type RawData = Array<string | undefined>
159+
160+
// XLSX.workbook type is native to SheetJS and can be viewed here: https://github.com/SheetJS/sheetjs/blob/83ddb4c1203f6bac052d8c1608b32fead02ea32f/types/index.d.ts#L269
161+
```
162+
163+
Example:
164+
165+
```tsx
166+
import { ReactSpreadsheetImport, StepType } from "react-spreadsheet-import";
167+
168+
<ReactSpreadsheetImport
169+
initialStepState={{
170+
type: StepType.matchColumns,
171+
data: [
172+
["Josh", "2"],
173+
["Charlie", "3"],
174+
["Lena", "50"],
175+
],
176+
headerValues: ["name", "age"],
177+
}}
178+
/>
179+
```
180+
127181
### Other optional props
128182

129183
```tsx

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export { StepType } from "./steps/UploadFlow"
12
export { ReactSpreadsheetImport } from "./ReactSpreadsheetImport"

src/steps/MatchColumnsStep/tests/MatchColumnsStep.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { Fields } from "../../../types"
1010
import selectEvent from "react-select-event"
1111
import { translations } from "../../../translationsRSIProps"
1212
import { SELECT_DROPDOWN_ID } from "../../../components/Selects/MenuPortal"
13-
import { Type } from "../../UploadFlow"
13+
import { StepType } from "../../UploadFlow"
1414

1515
const fields: Fields<any> = [
1616
{
@@ -595,7 +595,7 @@ describe("Match Columns general tests", () => {
595595
{...mockValues}
596596
matchColumnsStepHook={matchColumnsStepHook}
597597
initialStepState={{
598-
type: Type.matchColumns,
598+
type: StepType.matchColumns,
599599
data: [
600600
["Josh", "2"],
601601
["Charlie", "3"],
@@ -627,7 +627,7 @@ describe("Match Columns general tests", () => {
627627
{...mockValues}
628628
matchColumnsStepHook={matchColumnsStepHook}
629629
initialStepState={{
630-
type: Type.matchColumns,
630+
type: StepType.matchColumns,
631631
data: [
632632
["Josh", "2"],
633633
["Charlie", "3"],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Providers } from "../../../components/Providers"
77
import { ModalWrapper } from "../../../components/ModalWrapper"
88
import userEvent from "@testing-library/user-event"
99
import { readFileSync } from "fs"
10-
import { Type } from "../../UploadFlow"
10+
import { StepType } from "../../UploadFlow"
1111

1212
const MUTATED_HEADER = "mutated header"
1313
const CONTINUE_BUTTON = "Next"
@@ -86,7 +86,7 @@ test("selectHeaderStepHook should be able to modify raw data", async () => {
8686
{...mockRsiValues}
8787
selectHeaderStepHook={selectHeaderStepHook}
8888
initialStepState={{
89-
type: Type.selectHeader,
89+
type: StepType.selectHeader,
9090
data: [
9191
["name", "age"],
9292
["Josh", "2"],

src/steps/UploadFlow.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ import { exceedsMaxRecords } from "../utils/exceedsMaxRecords"
1111
import { useRsi } from "../hooks/useRsi"
1212
import type { RawData } from "../types"
1313

14-
export enum Type {
15-
upload,
16-
selectSheet,
17-
selectHeader,
18-
matchColumns,
19-
validateData,
14+
export enum StepType {
15+
upload = "upload",
16+
selectSheet = "selectSheet",
17+
selectHeader = "selectHeader",
18+
matchColumns = "matchColumns",
19+
validateData = "validateData",
2020
}
2121
export type StepState =
2222
| {
23-
type: Type.upload
23+
type: StepType.upload
2424
}
2525
| {
26-
type: Type.selectSheet
26+
type: StepType.selectSheet
2727
workbook: XLSX.WorkBook
2828
}
2929
| {
30-
type: Type.selectHeader
30+
type: StepType.selectHeader
3131
data: RawData[]
3232
}
3333
| {
34-
type: Type.matchColumns
34+
type: StepType.matchColumns
3535
data: RawData[]
3636
headerValues: RawData
3737
}
3838
| {
39-
type: Type.validateData
39+
type: StepType.validateData
4040
data: any[]
4141
}
4242

@@ -46,12 +46,12 @@ interface Props {
4646

4747
export const UploadFlow = ({ nextStep }: Props) => {
4848
const { initialStepState } = useRsi()
49-
const [state, setState] = useState<StepState>(initialStepState || { type: Type.upload })
49+
const [state, setState] = useState<StepState>(initialStepState || { type: StepType.upload })
5050
const { maxRecords, translations, uploadStepHook, selectHeaderStepHook, matchColumnsStepHook } = useRsi()
5151
const toast = useToast()
5252

5353
switch (state.type) {
54-
case Type.upload:
54+
case StepType.upload:
5555
return (
5656
<UploadStep
5757
onContinue={async (workbook) => {
@@ -69,17 +69,17 @@ export const UploadFlow = ({ nextStep }: Props) => {
6969
}
7070
const mappedWorkbook = await uploadStepHook(mapWorkbook(workbook))
7171
setState({
72-
type: Type.selectHeader,
72+
type: StepType.selectHeader,
7373
data: mappedWorkbook,
7474
})
7575
nextStep()
7676
} else {
77-
setState({ type: Type.selectSheet, workbook })
77+
setState({ type: StepType.selectSheet, workbook })
7878
}
7979
}}
8080
/>
8181
)
82-
case Type.selectSheet:
82+
case StepType.selectSheet:
8383
return (
8484
<SelectSheetStep
8585
sheetNames={state.workbook.SheetNames}
@@ -96,44 +96,44 @@ export const UploadFlow = ({ nextStep }: Props) => {
9696
}
9797
const mappedWorkbook = await uploadStepHook(mapWorkbook(state.workbook, sheetName))
9898
setState({
99-
type: Type.selectHeader,
99+
type: StepType.selectHeader,
100100
data: mappedWorkbook,
101101
})
102102
nextStep()
103103
}}
104104
/>
105105
)
106-
case Type.selectHeader:
106+
case StepType.selectHeader:
107107
return (
108108
<SelectHeaderStep
109109
data={state.data}
110110
onContinue={async (...args) => {
111111
const { data, headerValues } = await selectHeaderStepHook(...args)
112112
setState({
113-
type: Type.matchColumns,
113+
type: StepType.matchColumns,
114114
data,
115115
headerValues,
116116
})
117117
nextStep()
118118
}}
119119
/>
120120
)
121-
case Type.matchColumns:
121+
case StepType.matchColumns:
122122
return (
123123
<MatchColumnsStep
124124
data={state.data}
125125
headerValues={state.headerValues}
126126
onContinue={async (values, rawData, columns) => {
127127
const data = await matchColumnsStepHook(values, rawData, columns)
128128
setState({
129-
type: Type.validateData,
129+
type: StepType.validateData,
130130
data,
131131
})
132132
nextStep()
133133
}}
134134
/>
135135
)
136-
case Type.validateData:
136+
case StepType.validateData:
137137
return <ValidationStep initialData={state.data} />
138138
default:
139139
return <Progress isIndeterminate />

0 commit comments

Comments
 (0)