-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redmine 1301354): create StepperPage (#196)
* feat(redmine 1301354): create StepperPage * feat(redmine 1301354): response to github comments after rebase
- Loading branch information
1 parent
09e3e63
commit 2a9ae1c
Showing
8 changed files
with
191 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'storybook-pages': minor | ||
'@smile/haring-react': minor | ||
--- | ||
|
||
Create Stepper page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Meta, Source } from '@storybook/blocks'; | ||
|
||
import * as StepperPageStory from './StepperPage.stories'; | ||
|
||
import StepperPage from './StepperPage?raw'; | ||
|
||
<Meta of={StepperPageStory} /> | ||
|
||
# Code | ||
|
||
Code of StepperPage page | ||
|
||
<Source code={`${StepperPage}`} /> |
7 changes: 7 additions & 0 deletions
7
packages/storybook-pages/src/Pages/StepperPage/StepperPage.mock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const texts = { | ||
steps: { | ||
step1: 'Step 1: ', | ||
step2: 'Step 2: ', | ||
step3: 'Completed, click back button to get to previous step', | ||
}, | ||
}; |
18 changes: 18 additions & 0 deletions
18
packages/storybook-pages/src/Pages/StepperPage/StepperPage.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { StepperPage as Cmp } from './StepperPage'; | ||
|
||
const meta = { | ||
component: Cmp, | ||
parameters: { | ||
layout: 'fullscreen', | ||
}, | ||
title: '3-custom/Pages/StepperPage', | ||
} satisfies Meta<typeof Cmp>; | ||
|
||
export default meta; | ||
type IStory = StoryObj<typeof meta>; | ||
|
||
export const StepperPage: IStory = { | ||
args: {}, | ||
}; |
134 changes: 134 additions & 0 deletions
134
packages/storybook-pages/src/Pages/StepperPage/StepperPage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
'use client'; | ||
import type { IAddressFieldsValues } from '@smile/haring-react/src/Form/AddressFields/AddressFields'; | ||
import type { IValue } from '@smile/haring-react/src/Form/FetchAutocompleteField/FetchAutocompleteField'; | ||
import type { ReactElement } from 'react'; | ||
|
||
import { Button, Flex, Group, Stepper } from '@mantine/core'; | ||
import { useForm } from '@mantine/form'; | ||
import { | ||
AddressAutocompleteFields, | ||
FoldableColumnLayout, | ||
FullNameFields, | ||
SidebarMenu, | ||
} from '@smile/haring-react'; | ||
import { useState } from 'react'; | ||
|
||
import { menuMock } from '../BrowsingPage/BrowsingPage.mock'; | ||
|
||
import { texts } from './StepperPage.mock'; | ||
|
||
export interface IAddressGouvData { | ||
properties: { | ||
city?: string; | ||
housenumber?: string; | ||
label: string; | ||
postcode?: string; | ||
street?: string; | ||
}; | ||
} | ||
|
||
async function getDataAddressGouvRequest( | ||
value: string, | ||
): Promise<IValue<IAddressGouvData>[]> { | ||
const response = await fetch( | ||
`https://api-adresse.data.gouv.fr/search/?q=${encodeURIComponent( | ||
value, | ||
)}&autocomplete=1`, | ||
); | ||
const data: { features: IAddressGouvData[] } = await response.json(); | ||
const result = data.features.map((element) => { | ||
return { label: element.properties.label, value: element }; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
export function StepperPage(): ReactElement { | ||
const [active, setActive] = useState(0); | ||
|
||
const form = useForm({ | ||
initialValues: { | ||
address: { | ||
city: '', | ||
country: '', | ||
number: '', | ||
postCode: '', | ||
street: '', | ||
}, | ||
fullName: { | ||
firstName: '', | ||
lastName: '', | ||
}, | ||
}, | ||
}); | ||
|
||
const nextStep = (): void => { | ||
if (!form.validate().hasErrors) { | ||
setActive((current) => (current < 3 ? current + 1 : current)); | ||
} | ||
}; | ||
|
||
const prevStep = (): void => { | ||
setActive((current) => (current > 0 ? current - 1 : current)); | ||
}; | ||
|
||
function onOptionSubmitMock( | ||
value: IValue<IAddressGouvData>, | ||
): IAddressFieldsValues { | ||
const address = value.value.properties; | ||
const newAddress = { | ||
city: address.city ?? '', | ||
country: 'France', | ||
number: address.housenumber ?? '', | ||
postCode: address.postcode ?? '', | ||
street: address.street ?? '', | ||
}; | ||
form.setFieldValue('address', newAddress); | ||
return newAddress; | ||
} | ||
|
||
return ( | ||
<FoldableColumnLayout | ||
sidebarContent={ | ||
<Flex direction="column"> | ||
<SidebarMenu menu={menuMock} /> | ||
</Flex> | ||
} | ||
> | ||
<Stepper active={active}> | ||
<Stepper.Step description="Create an account" label="First step"> | ||
{texts.steps.step1} | ||
<FullNameFields mt="md" {...form.getInputProps('fullName')} /> | ||
</Stepper.Step> | ||
<Stepper.Step description="Verify email" label="Second step"> | ||
{texts.steps.step2} | ||
<AddressAutocompleteFields | ||
addressFieldsProps={form.values.address} | ||
mt="md" | ||
onFetchData={getDataAddressGouvRequest} | ||
onFieldsValuesChange={(values) => { | ||
form.setFieldValue('address', { | ||
city: values.city ?? '', | ||
country: values.country ?? form.values.address.country, | ||
number: values.number ?? '', | ||
postCode: values.postCode ?? '', | ||
street: values.street ?? '', | ||
}); | ||
}} | ||
onOptionSubmit={onOptionSubmitMock} | ||
/> | ||
</Stepper.Step> | ||
<Stepper.Completed>{texts.steps.step3}</Stepper.Completed> | ||
</Stepper> | ||
|
||
<Group mt="xl"> | ||
{active !== 0 && ( | ||
<Button onClick={prevStep} variant="default"> | ||
Back | ||
</Button> | ||
)} | ||
{active !== 2 && <Button onClick={nextStep}>Next step</Button>} | ||
</Group> | ||
</FoldableColumnLayout> | ||
); | ||
} |