import clsx from 'clsx' import { useFormContext } from 'react-hook-form' import { useTranslation } from 'react-i18next' import { InputLabel, ProposalVoteButton, Switch } from '@dao-dao/stateless' import { ProposalVoteOption } from '@dao-dao/types' export type ProposalInstantVoterProps = { /** * Vote options. */ options: ProposalVoteOption[] /** * How to determine if a given vote option is selected. */ isSelected: (option: Vote, value: Vote) => boolean /** * Field name for the vote form field. */ fieldName: string /** * Optional container class name. */ className?: string } /** * A form for editing proposal execution metadata. It expects to be within a * form context whose data has a `metadata` field with type * `ProposalExecutionMetadata`. */ export const ProposalInstantVoter = ({ options, isSelected, fieldName, className, }: ProposalInstantVoterProps) => { const { t } = useTranslation() const { watch, setValue } = useFormContext<{ vote: any }>() const voteFieldName = fieldName as 'vote' const vote = watch('vote') const toggle = () => vote ? setValue(voteFieldName, undefined) : setValue(voteFieldName, options[0].value) return (
{options.map((option, index) => ( vote && isSelected(option.value, vote) ? setValue(voteFieldName, undefined) : setValue(voteFieldName, option.value) } option={option} pressed={!!vote && isSelected(option.value, vote)} /> ))}
) }