'use strict'; import classNames from 'classnames'; import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import AutosuggestInput from 'soapbox/components/autosuggest_input'; import Icon from 'soapbox/components/icon'; import IconButton from 'soapbox/components/icon_button'; import { HStack } from 'soapbox/components/ui'; import { useAppSelector } from 'soapbox/hooks'; import type { AutoSuggestion } from 'soapbox/components/autosuggest_input'; const messages = defineMessages({ option_placeholder: { id: 'compose_form.poll.option_placeholder', defaultMessage: 'Choice {number}' }, add_option: { id: 'compose_form.poll.add_option', defaultMessage: 'Add a choice' }, remove_option: { id: 'compose_form.poll.remove_option', defaultMessage: 'Remove this choice' }, poll_duration: { id: 'compose_form.poll.duration', defaultMessage: 'Poll duration' }, switchToMultiple: { id: 'compose_form.poll.switch_to_multiple', defaultMessage: 'Change poll to allow multiple choices' }, switchToSingle: { id: 'compose_form.poll.switch_to_single', defaultMessage: 'Change poll to allow for a single choice' }, minutes: { id: 'intervals.full.minutes', defaultMessage: '{number, plural, one {# minute} other {# minutes}}' }, hours: { id: 'intervals.full.hours', defaultMessage: '{number, plural, one {# hour} other {# hours}}' }, days: { id: 'intervals.full.days', defaultMessage: '{number, plural, one {# day} other {# days}}' }, }); interface IOption { index: number isPollMultiple?: boolean maxChars: number numOptions: number onChange(index: number, value: string): void onClearSuggestions(): void onFetchSuggestions(token: string): void onRemove(index: number): void onRemovePoll(): void onSuggestionSelected(tokenStart: number, token: string, value: string, key: (string | number)[]): void onToggleMultiple(): void suggestions?: any // list title: string } const Option = (props: IOption) => { const { index, isPollMultiple, maxChars, numOptions, onChange, onClearSuggestions, onFetchSuggestions, onRemove, onRemovePoll, onToggleMultiple, suggestions, title, } = props; const intl = useIntl(); const handleOptionTitleChange = (event: React.ChangeEvent) => onChange(index, event.target.value); const handleOptionRemove = () => { if (numOptions > 2) { onRemove(index); } else { onRemovePoll(); } }; const handleToggleMultiple = (event: React.MouseEvent | React.KeyboardEvent) => { event.preventDefault(); event.stopPropagation(); onToggleMultiple(); }; const onSuggestionsClearRequested = () => onClearSuggestions(); const handleCheckboxKeypress = (event: React.KeyboardEvent) => { if (event.key === 'Enter' || event.key === ' ') { handleToggleMultiple(event); } }; const onSuggestionsFetchRequested = (token: string) => onFetchSuggestions(token); const onSuggestionSelected = (tokenStart: number, token: string | null, value: AutoSuggestion) => { if (token && typeof value === 'string') { props.onSuggestionSelected(tokenStart, token, value, ['poll', 'options', index]); } }; return (
  • ); }; interface IPollForm { expiresIn?: number isMultiple?: boolean onAddOption(value: string): void onChangeOption(): void onChangeSettings(value: string | number | undefined, isMultiple?: boolean): void onClearSuggestions(): void onFetchSuggestions(token: string): void onRemoveOption(): void onRemovePoll(): void onSuggestionSelected(tokenStart: number, token: string, value: string, key: (string | number)[]): void options?: any suggestions?: any // list } const PollForm = (props: IPollForm) => { const { expiresIn, isMultiple, onAddOption, onChangeOption, onChangeSettings, onRemoveOption, options, ...filteredProps } = props; const intl = useIntl(); const pollLimits = useAppSelector((state) => state.instance.getIn(['configuration', 'polls']) as any); const maxOptions = pollLimits.get('max_options'); const maxOptionChars = pollLimits.get('max_characters_per_option'); const handleAddOption = () => onAddOption(''); const handleSelectDuration = (event: React.ChangeEvent) => onChangeSettings(event.target.value, isMultiple); const handleToggleMultiple = () => onChangeSettings(expiresIn, !isMultiple); if (!options) { return null; } return (
      {options.map((title: string, i: number) => (
    {options.size < maxOptions && ( )}
    ); }; export default PollForm;