Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: use external ui components in address form #529

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"@commercelayer/eslint-config-ts-react": "^1.3.0",
"@commercelayer/js-auth": "^4.2.1",
"@commercelayer/sdk": "^5.32.0",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@mdx-js/react": "^3.0.1",
"@mui/material": "^5.15.17",
"@storybook/addon-actions": "^7.6.17",
"@storybook/addon-backgrounds": "^7.6.17",
"@storybook/addon-docs": "^7.6.17",
Expand All @@ -30,7 +33,7 @@
"@storybook/testing-library": "^0.2.2",
"@storybook/theming": "^7.6.17",
"@types/js-cookie": "^3.0.6",
"@types/react": "^18.2.57",
"@types/react": "^18.2.74",
"@vitejs/plugin-react": "^4.2.1",
"babel-loader": "^9.1.2",
"eslint": "^8.56.0",
Expand Down
9 changes: 9 additions & 0 deletions packages/docs/stories/examples/customer/002.address-form.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,12 @@ as shown in the following example:
To simplify the form example in this page, we omitted the `messages` prop for all `<Errors>`, but you can just copy the example above and implement it for every field.
</strong>
</span>



### Usage with 3rd party UI libraries (eg: Material UI)
The address components can be easily integrated with 3rd party UI libraries, such as Material UI via children by setting the `props.parentRef` as React ref to the final input element that will be rendered.

The following example shows how to use the `AddressInput` component with Material UI's `TextField` component.

<Canvas of={Stories.MaterialUiAddressForm} />
247 changes: 246 additions & 1 deletion packages/docs/stories/examples/customer/002.address-form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import CustomerContainer from '#components/customers/CustomerContainer'
import { BillingAddressForm } from '#components/addresses/BillingAddressForm'
import { useState } from 'react'
import AddressInput from '#components/addresses/AddressInput'
import AddressInputSelect from '#components/addresses/AddressInputSelect'
import Errors from '#components/errors/Errors'
import AddressCountrySelector from '#components/addresses/AddressCountrySelector'
import AddressStateSelector from '#components/addresses/AddressStateSelector'
Expand All @@ -13,6 +12,8 @@ import { type Address as TAddress } from '@commercelayer/sdk'
import AddressesContainer from '#components/addresses/AddressesContainer'
import { AddressField } from '#components/addresses/AddressField'
import { Address } from '#components/addresses/Address'
import TextField from '@mui/material/TextField'
import MenuItem from '@mui/material/MenuItem'

const setup: Meta = {
title: 'Examples/Customer Account/Address form'
Expand Down Expand Up @@ -232,3 +233,247 @@ export const SampleAddressForm: StoryFn<{ address: PartialAddress }> = (
}

SampleAddressForm.args = {}

export const MaterialUiAddressForm: StoryFn<{ address: PartialAddress }> = (
args
) => {
const errorClassNames = 'mt-1 text-sm text-red-600'
const [address] = useState<PartialAddress>(args.address ?? {})

return (
<CommerceLayer accessToken='customer-access-token'>
<CustomerContainer>
<AddressesContainer>
<BillingAddressForm
errorClassName='outline-none !border-red-600 focus:!border-red-600'
autoComplete='on'
className='p-2'
>
<div className='mb-4'>
<AddressInput
name='billing_address_first_name'
value={address?.first_name ?? ''}
>
{/* @ts-expect-error - element type not matched */}
{(props) => (
<TextField
name={props.name}
onChange={props.onChange}
inputRef={props.parentRef}
label='First name'
variant='filled'
style={{ width: '100%' }}
/>
)}
</AddressInput>
<Errors
resource='billing_address'
field='billing_address_first_name'
className={errorClassNames}
/>
</div>

<div className='mb-4'>
<AddressInput
name='billing_address_last_name'
value={address?.last_name ?? ''}
>
{/* @ts-expect-error - element type not matched */}
{(props) => (
<TextField
name={props.name}
onChange={props.onChange}
inputRef={props.parentRef}
label='Last name'
variant='filled'
style={{ width: '100%' }}
/>
)}
</AddressInput>
<Errors
resource='billing_address'
field='billing_address_last_name'
className={errorClassNames}
/>
</div>

<div className='mb-4'>
<AddressInput
name='billing_address_line_1'
type='text'
value={address?.line_1 ?? ''}
>
{/* @ts-expect-error - element type not matched */}
{(props) => (
<TextField
name={props.name}
onChange={props.onChange}
inputRef={props.parentRef}
label='Address'
variant='filled'
style={{ width: '100%' }}
/>
)}
</AddressInput>
<Errors
resource='billing_address'
field='billing_address_line_1'
className={errorClassNames}
/>
</div>

<div className='mb-4'>
<AddressInput
name='billing_address_city'
value={address?.city ?? ''}
>
{/* @ts-expect-error - element type not matched */}
{(props) => (
<TextField
name={props.name}
onChange={props.onChange}
inputRef={props.parentRef}
label='City'
variant='filled'
style={{ width: '100%' }}
/>
)}
</AddressInput>
<Errors
resource='billing_address'
field='billing_address_city'
className={errorClassNames}
/>
</div>

<div className='mb-4'>
<AddressCountrySelector
name='billing_address_country_code'
value={address?.country_code ?? ''}
>
{(props) => {
// console.log('props', props)
return (
<TextField
name={props.name}
select
value={props.value}
label='Country'
variant='filled'
style={{ width: '100%' }}
>
{props.options.map((option) => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</TextField>
)
}}
</AddressCountrySelector>
<Errors
resource='billing_address'
field='billing_address_country_code'
className={errorClassNames}
/>
</div>

<div className='mb-4'>
<AddressInput
name='billing_address_state_code'
value={address?.state_code ?? ''}
>
{/* @ts-expect-error - element type not matched */}
{(props) => (
<TextField
name={props.name}
onChange={props.onChange}
inputRef={props.parentRef}
label='State'
variant='filled'
style={{ width: '100%' }}
/>
)}
</AddressInput>
<Errors
resource='billing_address'
field='billing_address_state_code'
className={errorClassNames}
/>
</div>

<div className='mb-4'>
<AddressInput
name='billing_address_zip_code'
value={address?.zip_code ?? ''}
>
{/* @ts-expect-error - element type not matched */}
{(props) => (
<TextField
name={props.name}
onChange={props.onChange}
inputRef={props.parentRef}
label='Zip code'
variant='filled'
style={{ width: '100%' }}
/>
)}
</AddressInput>
<Errors
resource='billing_address'
field='billing_address_zip_code'
className={errorClassNames}
/>
</div>

<div className='mb-5'>
<AddressInput
name='billing_address_phone'
value={address?.phone ?? ''}
>
{/* @ts-expect-error - element type not matched */}
{(props) => (
<TextField
name={props.name}
onChange={props.onChange}
inputRef={props.parentRef}
type='tel'
label='Phone'
variant='filled'
style={{ width: '100%' }}
/>
)}
</AddressInput>
<Errors
resource='billing_address'
field='billing_address_phone'
className={errorClassNames}
/>
</div>
</BillingAddressForm>
<SaveAddressesButton
label='Save Address'
className='px-3 py-2 rounded bg-green-600 text-white disabled:opacity-50'
onClick={() => {
alert('Address saved')
}}
addressId={address?.id}
/>
</AddressesContainer>

<section className='grid grid-cols-3 gap-4 mt-4'>
<Address className='border p-4 grid gap-2 text-sm rounded-md'>
<AddressField name='full_name' className='font-bold' />
<AddressField name='full_address' />
<AddressField
className='cursor-pointer text-red-400 text-sm font-bold'
type='delete'
label='Delete'
onClick={() => {}}
/>
</Address>
</section>
</CustomerContainer>
</CommerceLayer>
)
}
Loading