UNPKG

@commercelayer/react-components

Version:
2 lines 3.06 kB
"use client"; "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CustomerAddressForm=CustomerAddressForm;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),AddressContext_1=tslib_1.__importDefault(require("../../context/AddressContext")),rapid_form_1=require("rapid-form"),react_1=require("react"),CustomerAddressFormContext_1=tslib_1.__importDefault(require("../../context/CustomerAddressFormContext")),OrderContext_1=tslib_1.__importDefault(require("../../context/OrderContext")),countryStateCity_1=require("../../utils/countryStateCity");function CustomerAddressForm(props){const{children,errorClassName,autoComplete="on",reset=!1,countriesWithPredefinedStateOptions}=props,p=tslib_1.__rest(props,["children","errorClassName","autoComplete","reset","countriesWithPredefinedStateOptions"]),{validation,values,errors,reset:resetForm}=(0,rapid_form_1.useRapidForm)(),{setAddressErrors,setAddress}=(0,react_1.useContext)(AddressContext_1.default),{order}=(0,react_1.useContext)(OrderContext_1.default),ref=(0,react_1.useRef)(null);(0,react_1.useEffect)(()=>{var _a,_b,_c,_d;if(Object.keys(errors).length>0){const formErrors=[];for(const fieldName in errors){const code=(_a=errors[fieldName])===null||_a===void 0?void 0:_a.code,message=((_b=errors[fieldName])===null||_b===void 0?void 0:_b.message)||"";fieldName==="billing_address_state_code"?values.state_code?delete errors[fieldName]:formErrors.push({code,message,resource:"billing_address",field:fieldName}):formErrors.push({code,message,resource:"billing_address",field:fieldName})}setAddressErrors(formErrors,"billing_address")}else if(Object.keys(values).length>0){setAddressErrors([],"billing_address");for(const name in values){const field=values[name];if(field?.value&&(values[name.replace("billing_address_","")]=field.value,delete values[name]),["billing_address_state_code"].includes(name)){const countryCode=((_c=values.billing_address_country_code)===null||_c===void 0?void 0:_c.value)||values.country_code;!(0,countryStateCity_1.isEmptyStates)({countryCode,countriesWithPredefinedStateOptions})&&!field.value&&delete values.billing_address_state_code}}setAddress({values,resource:"billing_address"})}reset&&(Object.keys(values).length>0||Object.keys(errors).length>0)&&ref&&((_d=ref.current)===null||_d===void 0||_d.reset(),resetForm({target:ref.current}),setAddressErrors([],"billing_address"),setAddress({values:{},resource:"billing_address"}))},[errors,values,reset]);const providerValues={values,validation,setValue:(name,value)=>{const field={[name.replace("billing_address_","")]:value};setAddress({values:Object.assign(Object.assign({},values),field),resource:"billing_address"})},errorClassName,requiresBillingInfo:order?.requires_billing_info||!1,errors,resetField:name=>{resetForm({currentTarget:ref.current},name)}};return(0,jsx_runtime_1.jsx)(CustomerAddressFormContext_1.default.Provider,{value:providerValues,children:(0,jsx_runtime_1.jsx)("form",Object.assign({ref,autoComplete},p,{children}))})}exports.default=CustomerAddressForm;