Skip to content

Commit c9d1b2f

Browse files
committed
Refactor wizard component
1 parent 13448b4 commit c9d1b2f

File tree

2 files changed

+97
-74
lines changed
  • client/src

2 files changed

+97
-74
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as React from 'react'
2+
3+
interface WizardChildrenProps {
4+
nextStep: () => void
5+
prevStep: () => void
6+
step: number
7+
}
8+
9+
interface Props {
10+
children: (values: WizardChildrenProps) => React.ReactNode
11+
}
12+
13+
interface State {
14+
step: number
15+
}
16+
17+
export class Wizard extends React.Component<Props, State> {
18+
public readonly state = {
19+
step: 0
20+
}
21+
22+
public nextStep = () => this.setState(({ step }) => ({ step: step + 1 }))
23+
24+
public prevStep = () => {
25+
if (this.state.step === 0) {
26+
return
27+
}
28+
29+
this.setState(({ step }) => ({ step: step - 1 }))
30+
}
31+
public render() {
32+
const { step } = this.state
33+
34+
return this.props.children({
35+
nextStep: this.nextStep,
36+
prevStep: this.prevStep,
37+
step
38+
})
39+
}
40+
}
Lines changed: 57 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react'
22
import { Formik, Form } from 'formik'
33

4+
import { Wizard } from 'src/Components/Wizard'
45
import { Stepper } from 'src/Components/Stepper'
56
import { Button } from 'src/Components/Button'
67
import { BasicInfoForm } from '../BasicInfoForm'
@@ -22,81 +23,63 @@ interface FieldValues {
2223
lng: number
2324
}
2425

25-
interface State {
26-
step: number
27-
}
28-
2926
// tslint:disable-next-line:jsx-key
3027
const pages = [<BasicInfoForm />, <RoomInfoForm />, <LocationForm />]
3128

32-
export class CreateListingForm extends React.Component<{}, State> {
33-
public readonly state = {
34-
step: 0
35-
}
36-
37-
private nextStep = () => this.setState(({ step }) => ({ step: step + 1 }))
38-
39-
private prevStep = () => {
40-
if (this.state.step === 0) {
41-
return
42-
}
43-
44-
this.setState(({ step }) => ({ step: step - 1 }))
45-
}
46-
47-
public render() {
48-
const { step } = this.state
49-
50-
return (
51-
<Formik<FieldValues>
52-
initialValues={{
53-
name: '',
54-
category: '',
55-
picture: '',
56-
description: '',
57-
price: 0,
58-
guests: 0,
59-
beds: 0,
60-
amenities: '',
61-
lat: 0,
62-
lng: 0
63-
}}
64-
onSubmit={(values: FieldValues) => {
65-
console.log(values)
66-
}}
67-
>
68-
{({ submitForm }) => (
69-
<Form>
70-
<Stepper
71-
currentStep={step + 1}
72-
maxSteps={pages.length}
73-
/>
74-
<Container>
75-
<Wrapper>
76-
{pages[step]}
77-
<Buttons>
78-
<Button
79-
disabled={step === 0}
80-
onClick={this.prevStep}
81-
>
82-
Wstecz
83-
</Button>
84-
85-
{step === pages.length - 1 ? (
86-
<Button onClick={submitForm}>
87-
Submit
88-
</Button>
89-
) : (
90-
<Button onClick={() => this.nextStep()}>
91-
Dalej
29+
export const CreateListingForm: React.SFC = () => (
30+
<Formik<FieldValues>
31+
initialValues={{
32+
name: '',
33+
category: '',
34+
picture: '',
35+
description: '',
36+
price: 0,
37+
guests: 0,
38+
beds: 0,
39+
amenities: '',
40+
lat: 0,
41+
lng: 0
42+
}}
43+
onSubmit={(values: FieldValues) => {
44+
console.log(values)
45+
}}
46+
>
47+
{({ submitForm }) => (
48+
<Form>
49+
<Wizard>
50+
{({ nextStep, prevStep, step }) => (
51+
<>
52+
<Stepper
53+
currentStep={step + 1}
54+
maxSteps={pages.length}
55+
/>
56+
<Container>
57+
<Wrapper>
58+
{pages[step]}
59+
<Buttons>
60+
<Button
61+
disabled={step === 0}
62+
onClick={prevStep}
63+
>
64+
Wstecz
9265
</Button>
93-
)}
94-
</Buttons>
95-
</Wrapper>
96-
</Container>
97-
</Form>
98-
)}
99-
</Formik>
100-
)
101-
}
102-
}
66+
67+
{step === pages.length - 1 ? (
68+
<Button onClick={submitForm}>
69+
Submit
70+
</Button>
71+
) : (
72+
<Button onClick={nextStep}>
73+
Dalej
74+
</Button>
75+
)}
76+
</Buttons>
77+
</Wrapper>
78+
</Container>
79+
</>
80+
)}
81+
</Wizard>
82+
</Form>
83+
)}
84+
</Formik>
85+
)

0 commit comments

Comments
 (0)