{"version":3,"file":"MultipleAnswersField.mjs","names":[],"sources":["../../../../../src/components/Poll/PollCreationDialog/MultipleAnswersField.tsx"],"sourcesContent":["import { POLL_COMPOSER_VALIDATION_CODE, pollComposerValidationError } from 'stream-chat';\nimport type { PollComposerState, PollComposerValidationCode } from 'stream-chat';\nimport React, { useMemo, useRef, useState } from 'react';\nimport { NumericInput } from '../../Form/NumericInput';\nimport { SwitchField, SwitchFieldLabel } from '../../Form/SwitchField';\nimport { useTranslationContext } from '../../../context';\nimport { useMessageComposerController } from '../../MessageComposer/hooks/useMessageComposerController';\nimport { useStateStore } from '../../../store';\n\nconst pollComposerStateSelector = (state: PollComposerState) => ({\n  enforce_unique_vote: state.data.enforce_unique_vote,\n  error: state.errors.max_votes_allowed,\n  max_votes_allowed: state.data.max_votes_allowed,\n});\n\nexport const MultipleAnswersField = () => {\n  const { t } = useTranslationContext();\n  const { pollComposer } = useMessageComposerController();\n  const { enforce_unique_vote, error, max_votes_allowed } = useStateStore(\n    pollComposer.state,\n    pollComposerStateSelector,\n  );\n  const [voteLimitEnabled, setVoteLimitEnabled] = useState(false);\n  const maxVotesInputRef = useRef<HTMLInputElement | null>(null);\n\n  const knownValidationErrors = useMemo<\n    Partial<Record<PollComposerValidationCode, string>>\n  >(\n    () => ({\n      [POLL_COMPOSER_VALIDATION_CODE.maxVotesUniqueVoteEnforced]: t(\n        'poll.multipleAnswersField.enforceUniqueVoteEnabled.label',\n        'Enforce unique vote is enabled',\n      ),\n      [POLL_COMPOSER_VALIDATION_CODE.maxVotesNotNumeric]: t(\n        'poll.multipleAnswersField.onlyNumbersAllowed.label',\n        'Only numbers are allowed',\n      ),\n      [POLL_COMPOSER_VALIDATION_CODE.maxVotesOutOfRange]: t(\n        'poll.multipleAnswersField.typeNumber210.label',\n        'Type a number from 2 to 10',\n      ),\n    }),\n    [t],\n  );\n\n  const multipleVotesEnabled = !enforce_unique_vote;\n  const errorText = error && (knownValidationErrors[error.code] ?? error.message);\n  const voteLimitSwitchId = 'max_votes_allowed_enabled';\n  const voteLimitSwitchLabelId = `${voteLimitSwitchId}-label`;\n\n  return (\n    <div className='str-chat__form__switch-fieldset'>\n      <SwitchField\n        checked={multipleVotesEnabled}\n        description={t(\n          'poll.multipleAnswersField.selectMoreThanOne.description',\n          'Select More Than One Option',\n        )}\n        id='enforce_unique_vote'\n        onChange={(e) => {\n          setVoteLimitEnabled(false);\n          pollComposer.updateFields({ data: { enforce_unique_vote: !e.target.checked } });\n        }}\n        title={t('poll.multipleAnswersField.multipleVotes.title', 'Multiple Votes')}\n      />\n      {multipleVotesEnabled && (\n        <SwitchField\n          aria-labelledby={voteLimitSwitchLabelId}\n          checked={voteLimitEnabled}\n          fieldClassName='str-chat__multiple-answers-field__votes-limit-field'\n          id={voteLimitSwitchId}\n          onChange={(event) => {\n            const nextVoteLimitEnabled = event.target.checked;\n            setVoteLimitEnabled(nextVoteLimitEnabled);\n            pollComposer.updateFields({ data: { max_votes_allowed: '2' } });\n            if (!nextVoteLimitEnabled) return;\n            requestAnimationFrame(() => {\n              maxVotesInputRef.current?.focus();\n            });\n          }}\n        >\n          <div className='str-chat__multiple-answers-field__votes-limit-field__numeric-field'>\n            <SwitchFieldLabel\n              asError={!!errorText}\n              description={t(\n                'poll.multipleAnswersField.chooseBetween210.description',\n                'Choose Between 2 to 10 Options',\n              )}\n              htmlFor={voteLimitSwitchId}\n              id={voteLimitSwitchLabelId}\n              title={t(\n                'poll.multipleAnswersField.limitVotesPerPerson.title',\n                'Limit Votes per Person',\n              )}\n            />\n            {voteLimitEnabled && (\n              <NumericInput\n                aria-label={t(\n                  'poll.multipleAnswersField.maximumVotesPerPerson.ariaLabel',\n                  'Maximum votes per person',\n                )}\n                id='max_votes_allowed'\n                max={10}\n                min={2}\n                onBlur={() => {\n                  pollComposer.handleFieldBlur('max_votes_allowed');\n                }}\n                onChange={(e) => {\n                  const raw = e.target.value;\n                  const nativeFieldValidation =\n                    raw !== '' && !/^\\d+$/.test(raw)\n                      ? {\n                          // Same shape as a composer error, so it renders identically.\n                          max_votes_allowed: pollComposerValidationError(\n                            POLL_COMPOSER_VALIDATION_CODE.maxVotesNotNumeric,\n                          ),\n                        }\n                      : undefined;\n                  pollComposer.updateFields({\n                    data: {\n                      max_votes_allowed: nativeFieldValidation\n                        ? pollComposer.max_votes_allowed\n                        : raw,\n                    },\n                    injectedFieldErrors: nativeFieldValidation,\n                  });\n                }}\n                ref={maxVotesInputRef}\n                value={max_votes_allowed ?? ''}\n              />\n            )}\n          </div>\n        </SwitchField>\n      )}\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;AASA,IAAM,6BAA6B,WAA8B;CAC/D,qBAAqB,MAAM,KAAK;CAChC,OAAO,MAAM,OAAO;CACpB,mBAAmB,MAAM,KAAK;AAChC;AAEA,IAAa,6BAA6B;CACxC,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,EAAE,iBAAiB,6BAA6B;CACtD,MAAM,EAAE,qBAAqB,OAAO,sBAAsB,cACxD,aAAa,OACb,yBACF;CACA,MAAM,CAAC,kBAAkB,uBAAuB,SAAS,KAAK;CAC9D,MAAM,mBAAmB,OAAgC,IAAI;CAE7D,MAAM,wBAAwB,eAGrB;GACJ,8BAA8B,6BAA6B,EAC1D,4DACA,gCACF;GACC,8BAA8B,qBAAqB,EAClD,sDACA,0BACF;GACC,8BAA8B,qBAAqB,EAClD,iDACA,4BACF;CACF,IACA,CAAC,CAAC,CACJ;CAEA,MAAM,uBAAuB,CAAC;CAC9B,MAAM,YAAY,UAAU,sBAAsB,MAAM,SAAS,MAAM;CACvE,MAAM,oBAAoB;CAC1B,MAAM,yBAAyB,GAAG,kBAAkB;CAEpD,OACE,qBAAC,OAAD;EAAK,WAAU;YAAf,CACE,oBAAC,aAAD;GACE,SAAS;GACT,aAAa,EACX,2DACA,6BACF;GACA,IAAG;GACH,WAAW,MAAM;IACf,oBAAoB,KAAK;IACzB,aAAa,aAAa,EAAE,MAAM,EAAE,qBAAqB,CAAC,EAAE,OAAO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