UNPKG

@commercelayer/react-components

Version:
2 lines 820 B
"use client"; import{jsx as _jsx,Fragment as _Fragment}from"react/jsx-runtime";import{useRef,useContext}from"react";import validateFormFields from"../../utils/validateFormFields";import isEmpty from"lodash/isEmpty";import GiftCardContext from"../../context/GiftCardContext";export function GiftCard(props){const{children,onSubmit}=props,name="giftCardForm",ref=useRef(null),{addGiftCard,addGiftCardError}=useContext(GiftCardContext);return _jsx(_Fragment,{children:_jsx("form",{name,onSubmit:e=>{e.preventDefault();const currentForm=ref.current,elements=currentForm?.elements,{errors,values}=validateFormFields(elements,["currencyCode","balanceCents"],"gift_cards");isEmpty(errors)?(addGiftCard(values),currentForm?.reset(),onSubmit&&onSubmit(values)):addGiftCardError(errors)},children},name)})}export default GiftCard;