|
| 1 | +import * as Common from '@clerk/elements/common'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import { useAppearance } from '~/contexts'; |
| 5 | +import { useEnvironment } from '~/hooks/use-environment'; |
| 6 | +import { useLocalizations } from '~/hooks/use-localizations'; |
| 7 | +import * as Field from '~/primitives/field'; |
| 8 | + |
| 9 | +import { LinkRenderer } from './link-renderer'; |
| 10 | + |
| 11 | +export function LegalAcceptedField({ |
| 12 | + className, |
| 13 | + checked = false, |
| 14 | + ...restProps |
| 15 | +}: Omit<React.ComponentProps<typeof Common.Input>, 'type'>) { |
| 16 | + const { t } = useLocalizations(); |
| 17 | + const { displayConfig } = useEnvironment(); |
| 18 | + const { parsedAppearance } = useAppearance(); |
| 19 | + const termsUrl = parsedAppearance.options.termsPageUrl || displayConfig.termsUrl; |
| 20 | + const privacyPolicyUrl = parsedAppearance.options.privacyPageUrl || displayConfig.privacyPolicyUrl; |
| 21 | + |
| 22 | + let localizedText: string | undefined; |
| 23 | + |
| 24 | + if (termsUrl && privacyPolicyUrl) { |
| 25 | + localizedText = t('signUp.__experimental_legalConsent.checkbox.label__termsOfServiceAndPrivacyPolicy', { |
| 26 | + termsOfServiceLink: termsUrl, |
| 27 | + privacyPolicyLink: privacyPolicyUrl, |
| 28 | + }); |
| 29 | + } else if (termsUrl) { |
| 30 | + localizedText = t('signUp.__experimental_legalConsent.checkbox.label__onlyTermsOfService', { |
| 31 | + termsOfServiceLink: termsUrl, |
| 32 | + }); |
| 33 | + } else if (privacyPolicyUrl) { |
| 34 | + localizedText = t('signUp.__experimental_legalConsent.checkbox.label__onlyPrivacyPolicy', { |
| 35 | + privacyPolicyLink: privacyPolicyUrl, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <Common.Field |
| 41 | + name='__experimental_legalAccepted' |
| 42 | + asChild |
| 43 | + > |
| 44 | + <Field.Root> |
| 45 | + <div className='flex justify-center gap-2'> |
| 46 | + <Common.Input |
| 47 | + type='checkbox' |
| 48 | + asChild |
| 49 | + checked={checked} |
| 50 | + {...restProps} |
| 51 | + > |
| 52 | + <Field.Checkbox /> |
| 53 | + </Common.Input> |
| 54 | + |
| 55 | + <Common.Label asChild> |
| 56 | + <Field.Label> |
| 57 | + <span> |
| 58 | + <LinkRenderer |
| 59 | + text={localizedText || ''} |
| 60 | + className='underline underline-offset-2' |
| 61 | + /> |
| 62 | + </span> |
| 63 | + </Field.Label> |
| 64 | + </Common.Label> |
| 65 | + </div> |
| 66 | + |
| 67 | + <Common.FieldError asChild> |
| 68 | + {({ message }) => { |
| 69 | + return <Field.Message intent='error'>{message}</Field.Message>; |
| 70 | + }} |
| 71 | + </Common.FieldError> |
| 72 | + </Field.Root> |
| 73 | + </Common.Field> |
| 74 | + ); |
| 75 | +} |
0 commit comments