billing-period-toggle.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { useTranslation } from 'react-i18next'
  2. import '../../../stylesheets/components/billing-period-toggle.scss'
  3. export type BillingPeriod = 'monthly' | 'annual'
  4. export type BillingPeriodToggleProps = {
  5. value: BillingPeriod
  6. onChange: (period: BillingPeriod) => void
  7. id?: string
  8. showDiscount?: boolean
  9. variant?: 'default' | 'premium'
  10. }
  11. export function BillingPeriodToggle({
  12. value,
  13. onChange,
  14. id = 'billing-period',
  15. showDiscount = true,
  16. variant = 'default',
  17. }: BillingPeriodToggleProps) {
  18. const { t } = useTranslation()
  19. return (
  20. <fieldset
  21. className={`billing-period-toggle ${variant === 'premium' ? 'billing-period-toggle-premium' : ''}`}
  22. >
  23. <legend className="visually-hidden">
  24. {t('billing_period_sentence_case')}
  25. </legend>
  26. <input
  27. type="radio"
  28. id={`${id}-annual`}
  29. name={id}
  30. checked={value === 'annual'}
  31. onChange={() => {
  32. if (value !== 'annual') {
  33. onChange('annual')
  34. }
  35. }}
  36. />
  37. <label htmlFor={`${id}-annual`}>
  38. {t('yearly')}
  39. {showDiscount && (
  40. <span className="billing-period-toggle-discount-badge">
  41. {t('save_20_percent')}
  42. </span>
  43. )}
  44. </label>
  45. <input
  46. type="radio"
  47. id={`${id}-monthly`}
  48. name={id}
  49. checked={value === 'monthly'}
  50. onChange={() => {
  51. if (value !== 'monthly') {
  52. onChange('monthly')
  53. }
  54. }}
  55. />
  56. <label htmlFor={`${id}-monthly`}>{t('monthly')}</label>
  57. </fieldset>
  58. )
  59. }
  60. export default BillingPeriodToggle