ConsentModal.tsx
2 years ago
ShowTerms.tsx
2 years ago
createIframePortal.tsx
1 year ago
index.tsx
2 years ago
styles.scss
2 years ago
index.tsx
74 lines
| 1 | import {useState} from '@wordpress/element'; |
| 2 | import type {ConsentProps} from '@givewp/forms/propTypes'; |
| 3 | import Checkbox from '../Checkbox'; |
| 4 | import ShowTerms from './ShowTerms'; |
| 5 | import ConsentModal from './ConsentModal'; |
| 6 | import {Markup} from 'interweave'; |
| 7 | |
| 8 | export default function ConsentField({ |
| 9 | name, |
| 10 | ErrorMessage, |
| 11 | fieldError, |
| 12 | inputProps, |
| 13 | checkboxLabel, |
| 14 | displayType, |
| 15 | linkText, |
| 16 | linkUrl, |
| 17 | modalHeading, |
| 18 | modalAcceptanceText, |
| 19 | agreementText, |
| 20 | Label: LabelWithRequired, |
| 21 | label, |
| 22 | validationRules, |
| 23 | }: ConsentProps) { |
| 24 | const [showModal, setShowModal] = useState<boolean>(false); |
| 25 | const {useFormContext} = window.givewp.form.hooks; |
| 26 | const {setValue} = useFormContext(); |
| 27 | |
| 28 | const FieldLabel = window.givewp.form.templates.layouts.fieldLabel; |
| 29 | |
| 30 | const {required} = validationRules; |
| 31 | |
| 32 | const isModalDisplay = displayType === 'showModalTerms'; |
| 33 | const isFormDisplay = displayType === 'showFormTerms'; |
| 34 | |
| 35 | const openTerms = (event) => { |
| 36 | event.preventDefault(); |
| 37 | setShowModal(true); |
| 38 | }; |
| 39 | |
| 40 | const acceptTerms = (event) => { |
| 41 | event.preventDefault(); |
| 42 | setValue(name, 'accepted'); |
| 43 | setShowModal(false); |
| 44 | }; |
| 45 | |
| 46 | const Label = () => ( |
| 47 | <span className={'givewp-fields-consent__checkbox-label'}> |
| 48 | <FieldLabel label={checkboxLabel} required={!label && required} /> |
| 49 | |
| 50 | {!isFormDisplay && ( |
| 51 | <ShowTerms openTerms={openTerms} displayType={displayType} linkText={linkText} linkUrl={linkUrl} /> |
| 52 | )} |
| 53 | </span> |
| 54 | ); |
| 55 | |
| 56 | return ( |
| 57 | <div className={'givewp-fields-consent'}> |
| 58 | {label && <LabelWithRequired />} |
| 59 | {/*// @ts-ignore*/} |
| 60 | <Checkbox {...{Label, ErrorMessage, fieldError, inputProps}} value={'accepted'} /> |
| 61 | |
| 62 | {isModalDisplay && showModal && ( |
| 63 | <ConsentModal {...{setShowModal, modalHeading, modalAcceptanceText, agreementText, acceptTerms}} /> |
| 64 | )} |
| 65 | |
| 66 | {isFormDisplay && ( |
| 67 | <div className="givewp-fields-consent__container"> |
| 68 | <Markup content={agreementText} noWrap /> |
| 69 | </div> |
| 70 | )} |
| 71 | </div> |
| 72 | ); |
| 73 | } |
| 74 |