1- import { component$ , useSignal , useStore , useTask$ } from '@builder.io/qwik' ;
2- import { Form } from '@builder.io/qwik-city' ;
3- import {
4- SfButton ,
5- SfCheckbox ,
6- SfInput ,
7- SfLoaderCircular ,
8- SfSelect ,
9- } from 'qwik-storefront-ui' ;
10- import { useAddressForm } from '~/routes/layout' ;
1+ import { component$ } from '@builder.io/qwik' ;
2+ import { SfInput , SfSelect } from 'qwik-storefront-ui' ;
113import { FormHelperText } from '../Form/FormHelperText' ;
124import { FormLabel } from '../Form/FormLabel' ;
135
@@ -26,50 +18,24 @@ export type AddressFormFields = Record<AddressFields, string>;
2618
2719export type AddressFormProps = {
2820 type : 'billingAddress' | 'shippingAddress' ;
29- savedAddress ?: AddressFormFields | undefined ;
21+ savedAddress : AddressFormFields ;
22+ class ?: string ;
3023} ;
3124
3225export const AddressForm = component$ < AddressFormProps > (
33- ( { type, savedAddress } ) => {
34- const addressFormAction = useAddressForm ( ) ;
35- const formRefSig = useSignal < HTMLFormElement > ( ) ;
36- useStore < { value : AddressFormFields } > ( {
37- value : {
38- firstName : savedAddress ?. firstName || '' ,
39- lastName : savedAddress ?. lastName || '' ,
40- phone : savedAddress ?. phone || '' ,
41- country : savedAddress ?. country || '' ,
42- streetName : savedAddress ?. streetName || '' ,
43- streetNumber : savedAddress ?. streetNumber || '' ,
44- city : savedAddress ?. city || '' ,
45- state : savedAddress ?. state || '' ,
46- postalCode : savedAddress ?. postalCode || '' ,
47- } ,
48- } ) ;
49- const countries = [ 'US' ] ;
50- const states = [ 'California' ] ;
51-
52- useTask$ ( ( { track } ) => {
53- const success = track ( ( ) => addressFormAction . value ?. success ) ;
54- if ( success ) {
55- if ( formRefSig . value ) {
56- formRefSig . value . reset ( ) ;
57- }
58- }
59- } ) ;
60-
26+ ( { savedAddress, class : _class } ) => {
6127 return (
62- < Form
63- class = 'grid grid-cols-1 md:grid-cols-[50%_1fr_120px] gap-4'
28+ < div
29+ class = { [ 'grid grid-cols-1 md:grid-cols-[50%_1fr_120px] gap-4' , _class ] }
6430 data-testid = 'address-form'
65- action = { addressFormAction }
66- ref = { formRefSig }
6731 >
6832 < label >
6933 < FormLabel > { $localize `form.firstNameLabel` } </ FormLabel >
7034 < SfInput
7135 name = 'firstName'
7236 autoComplete = 'given-name'
37+ value = { savedAddress . firstName }
38+ readOnly = { true }
7339 // defaultValue={addressStore.firstName} <-- fix lib <-- fix lib
7440 required
7541 />
@@ -79,7 +45,8 @@ export const AddressForm = component$<AddressFormProps>(
7945 < SfInput
8046 name = 'lastName'
8147 autoComplete = 'family-name'
82- // defaultValue={addressStore.lastName} <-- fix lib
48+ value = { savedAddress . lastName }
49+ readOnly = { true }
8350 required
8451 />
8552 </ label >
@@ -89,7 +56,8 @@ export const AddressForm = component$<AddressFormProps>(
8956 name = 'phone'
9057 type = 'tel'
9158 autoComplete = 'tel'
92- // defaultValue={addressStore.phone} <-- fix lib
59+ value = { savedAddress . phone }
60+ readOnly = { true }
9361 required
9462 />
9563 </ label >
@@ -99,10 +67,10 @@ export const AddressForm = component$<AddressFormProps>(
9967 name = 'country'
10068 placeholder = { $localize `form.selectPlaceholder` }
10169 autoComplete = 'country-name'
102- // defaultValue={addressStore.country} <-- fix lib
70+ disabled = { true }
10371 required
10472 >
105- { countries . map ( ( country ) => (
73+ { [ 'US' ] . map ( ( country ) => (
10674 < option key = { country } > { country } </ option >
10775 ) ) }
10876 </ SfSelect >
@@ -112,7 +80,8 @@ export const AddressForm = component$<AddressFormProps>(
11280 < SfInput
11381 name = 'streetName'
11482 autoComplete = 'address-line1'
115- // defaultValue={// addressStore.streetName} <-- fix lib
83+ value = { savedAddress . streetName }
84+ readOnly = { true }
11685 required
11786 />
11887 < FormHelperText > { $localize `form.streetNameHelp` } </ FormHelperText >
@@ -121,7 +90,8 @@ export const AddressForm = component$<AddressFormProps>(
12190 < FormLabel > { $localize `form.streetNumberLabel` } </ FormLabel >
12291 < SfInput
12392 name = 'streetNumber'
124- // defaultValue={// addressStore.streetNumber} <-- fix lib
93+ value = { savedAddress . streetNumber }
94+ readOnly = { true }
12595 />
12696 < FormHelperText > { $localize `form.streetNumberHelp` } </ FormHelperText >
12797 </ label >
@@ -130,7 +100,8 @@ export const AddressForm = component$<AddressFormProps>(
130100 < SfInput
131101 name = 'city'
132102 autoComplete = 'address-level2'
133- // defaultValue={// addressStore.city} <-- fix lib
103+ value = { savedAddress . city }
104+ readOnly = { true }
134105 required
135106 />
136107 </ label >
@@ -139,11 +110,12 @@ export const AddressForm = component$<AddressFormProps>(
139110 < SfSelect
140111 name = 'state'
141112 autoComplete = 'address-level1'
142- // defaultValue={// addressStore.state} <-- fix lib
113+ value = { savedAddress . state }
114+ disabled = { true }
143115 placeholder = { $localize `form.selectPlaceholder` }
144116 required
145117 >
146- { states . map ( ( state ) => (
118+ { [ 'California' ] . map ( ( state ) => (
147119 < option key = { state } > { state } </ option >
148120 ) ) }
149121 </ SfSelect >
@@ -153,47 +125,12 @@ export const AddressForm = component$<AddressFormProps>(
153125 < SfInput
154126 name = 'postalCode'
155127 autoComplete = 'postal-code'
156- // defaultValue={// addressStore.postalCode} <-- fix lib
128+ value = { savedAddress . postalCode }
129+ readOnly = { true }
157130 required
158131 />
159132 </ label >
160-
161- { type === 'billingAddress' && (
162- < label class = 'md:col-span-3 flex items-center gap-2' >
163- < SfCheckbox name = 'useAsShipping' />
164- { $localize `form.useAsShippingLabel` }
165- </ label >
166- ) }
167-
168- < div class = 'md:col-span-3 flex justify-end gap-4' >
169- < SfButton
170- type = 'reset'
171- onClick$ = { ( ) => {
172- if ( formRefSig . value ) {
173- formRefSig . value . reset ( ) ;
174- }
175- } }
176- class = 'max-md:w-1/2'
177- variant = 'secondary'
178- >
179- { $localize `checkout:contactInfo.clearAll` }
180- </ SfButton >
181- < SfButton
182- type = 'submit'
183- class = 'w-1/2 md:w-1/6'
184- disabled = { addressFormAction . isRunning }
185- >
186- { addressFormAction . isRunning ? (
187- < SfLoaderCircular
188- class = 'flex justify-center items-center'
189- size = 'sm'
190- />
191- ) : (
192- $localize `checkout:contactInfo.save`
193- ) }
194- </ SfButton >
195- </ div >
196- </ Form >
133+ </ div >
197134 ) ;
198135 }
199136) ;
0 commit comments