2019-03-06 14:53:37 +11:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-24 01:15:17 +10:00
|
|
|
import { PureComponent } from 'react';
|
|
|
|
|
|
|
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
|
|
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2019-03-06 14:53:37 +11:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2023-05-24 01:15:17 +10:00
|
|
|
|
2024-01-12 21:31:24 +11:00
|
|
|
import AddIcon from '@material-symbols/svg-600/outlined/add.svg?react';
|
|
|
|
import CloseIcon from '@material-symbols/svg-600/outlined/close.svg?react';
|
2023-10-25 04:45:08 +11:00
|
|
|
|
2019-05-10 06:10:27 +10:00
|
|
|
import AutosuggestInput from 'mastodon/components/autosuggest_input';
|
2023-05-24 01:15:17 +10:00
|
|
|
import { Icon } from 'mastodon/components/icon';
|
|
|
|
import { IconButton } from 'mastodon/components/icon_button';
|
2019-03-06 14:53:37 +11:00
|
|
|
|
|
|
|
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' },
|
2019-12-04 05:53:16 +11:00
|
|
|
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' },
|
2019-03-06 14:53:37 +11:00
|
|
|
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}}' },
|
|
|
|
});
|
|
|
|
|
2023-05-23 18:52:27 +10:00
|
|
|
class OptionIntl extends PureComponent {
|
2019-03-06 14:53:37 +11:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
title: PropTypes.string.isRequired,
|
2023-01-30 05:00:19 +11:00
|
|
|
lang: PropTypes.string,
|
2019-03-06 14:53:37 +11:00
|
|
|
index: PropTypes.number.isRequired,
|
|
|
|
isPollMultiple: PropTypes.bool,
|
2020-05-12 23:29:44 +10:00
|
|
|
autoFocus: PropTypes.bool,
|
2019-03-06 14:53:37 +11:00
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
onRemove: PropTypes.func.isRequired,
|
2019-04-22 22:56:14 +10:00
|
|
|
onToggleMultiple: PropTypes.func.isRequired,
|
2019-05-10 06:10:27 +10:00
|
|
|
suggestions: ImmutablePropTypes.list,
|
|
|
|
onClearSuggestions: PropTypes.func.isRequired,
|
|
|
|
onFetchSuggestions: PropTypes.func.isRequired,
|
|
|
|
onSuggestionSelected: PropTypes.func.isRequired,
|
2019-03-06 14:53:37 +11:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleOptionTitleChange = e => {
|
|
|
|
this.props.onChange(this.props.index, e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
handleOptionRemove = () => {
|
|
|
|
this.props.onRemove(this.props.index);
|
|
|
|
};
|
|
|
|
|
2019-05-10 06:10:27 +10:00
|
|
|
|
2019-04-22 22:56:14 +10:00
|
|
|
handleToggleMultiple = e => {
|
|
|
|
this.props.onToggleMultiple();
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
2019-12-04 05:53:16 +11:00
|
|
|
handleCheckboxKeypress = e => {
|
|
|
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
|
|
this.handleToggleMultiple(e);
|
|
|
|
}
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2019-12-04 05:53:16 +11:00
|
|
|
|
2019-05-10 06:10:27 +10:00
|
|
|
onSuggestionsClearRequested = () => {
|
|
|
|
this.props.onClearSuggestions();
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2019-05-10 06:10:27 +10:00
|
|
|
|
|
|
|
onSuggestionsFetchRequested = (token) => {
|
|
|
|
this.props.onFetchSuggestions(token);
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2019-05-10 06:10:27 +10:00
|
|
|
|
|
|
|
onSuggestionSelected = (tokenStart, token, value) => {
|
|
|
|
this.props.onSuggestionSelected(tokenStart, token, value, ['poll', 'options', this.props.index]);
|
2023-01-30 11:45:35 +11:00
|
|
|
};
|
2019-05-10 06:10:27 +10:00
|
|
|
|
2019-03-06 14:53:37 +11:00
|
|
|
render () {
|
2023-01-30 05:00:19 +11:00
|
|
|
const { isPollMultiple, title, lang, index, autoFocus, intl } = this.props;
|
2019-03-06 14:53:37 +11:00
|
|
|
|
|
|
|
return (
|
|
|
|
<li>
|
2020-04-03 02:10:55 +11:00
|
|
|
<label className='poll__option editable'>
|
2019-04-22 22:56:14 +10:00
|
|
|
<span
|
|
|
|
className={classNames('poll__input', { checkbox: isPollMultiple })}
|
|
|
|
onClick={this.handleToggleMultiple}
|
2019-12-04 05:53:16 +11:00
|
|
|
onKeyPress={this.handleCheckboxKeypress}
|
2019-04-22 22:56:14 +10:00
|
|
|
role='button'
|
2023-04-05 00:33:44 +10:00
|
|
|
tabIndex={0}
|
2019-12-17 09:55:02 +11:00
|
|
|
title={intl.formatMessage(isPollMultiple ? messages.switchToSingle : messages.switchToMultiple)}
|
|
|
|
aria-label={intl.formatMessage(isPollMultiple ? messages.switchToSingle : messages.switchToMultiple)}
|
2019-04-22 22:56:14 +10:00
|
|
|
/>
|
2019-03-06 14:53:37 +11:00
|
|
|
|
2019-05-10 06:10:27 +10:00
|
|
|
<AutosuggestInput
|
2019-03-06 14:53:37 +11:00
|
|
|
placeholder={intl.formatMessage(messages.option_placeholder, { number: index + 1 })}
|
2020-04-03 02:10:55 +11:00
|
|
|
maxLength={50}
|
2019-03-06 14:53:37 +11:00
|
|
|
value={title}
|
2023-01-30 05:00:19 +11:00
|
|
|
lang={lang}
|
2023-02-05 02:34:21 +11:00
|
|
|
spellCheck
|
2019-03-06 14:53:37 +11:00
|
|
|
onChange={this.handleOptionTitleChange}
|
2019-05-10 06:10:27 +10:00
|
|
|
suggestions={this.props.suggestions}
|
|
|
|
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
|
|
|
|
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
|
|
|
|
onSuggestionSelected={this.onSuggestionSelected}
|
|
|
|
searchTokens={[':']}
|
2020-05-12 23:29:44 +10:00
|
|
|
autoFocus={autoFocus}
|
2019-03-06 14:53:37 +11:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<div className='poll__cancel'>
|
2023-10-25 04:45:08 +11:00
|
|
|
<IconButton disabled={index <= 1} title={intl.formatMessage(messages.remove_option)} icon='times' iconComponent={CloseIcon} onClick={this.handleOptionRemove} />
|
2019-03-06 14:53:37 +11:00
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-24 13:17:53 +11:00
|
|
|
const Option = injectIntl(OptionIntl);
|
|
|
|
|
2019-03-06 14:53:37 +11:00
|
|
|
class PollForm extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
options: ImmutablePropTypes.list,
|
2023-01-30 05:00:19 +11:00
|
|
|
lang: PropTypes.string,
|
2019-03-06 14:53:37 +11:00
|
|
|
expiresIn: PropTypes.number,
|
|
|
|
isMultiple: PropTypes.bool,
|
|
|
|
onChangeOption: PropTypes.func.isRequired,
|
|
|
|
onAddOption: PropTypes.func.isRequired,
|
|
|
|
onRemoveOption: PropTypes.func.isRequired,
|
|
|
|
onChangeSettings: PropTypes.func.isRequired,
|
2019-05-10 06:10:27 +10:00
|
|
|
suggestions: ImmutablePropTypes.list,
|
|
|
|
onClearSuggestions: PropTypes.func.isRequired,
|
|
|
|
onFetchSuggestions: PropTypes.func.isRequired,
|
|
|
|
onSuggestionSelected: PropTypes.func.isRequired,
|
2019-03-06 14:53:37 +11:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleAddOption = () => {
|
|
|
|
this.props.onAddOption('');
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSelectDuration = e => {
|
|
|
|
this.props.onChangeSettings(e.target.value, this.props.isMultiple);
|
|
|
|
};
|
|
|
|
|
2019-04-22 22:56:14 +10:00
|
|
|
handleToggleMultiple = () => {
|
|
|
|
this.props.onChangeSettings(this.props.expiresIn, !this.props.isMultiple);
|
|
|
|
};
|
|
|
|
|
2019-03-06 14:53:37 +11:00
|
|
|
render () {
|
2023-01-30 05:00:19 +11:00
|
|
|
const { options, lang, expiresIn, isMultiple, onChangeOption, onRemoveOption, intl, ...other } = this.props;
|
2019-03-06 14:53:37 +11:00
|
|
|
|
|
|
|
if (!options) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-12 23:29:44 +10:00
|
|
|
const autoFocusIndex = options.indexOf('');
|
|
|
|
|
2019-03-06 14:53:37 +11:00
|
|
|
return (
|
|
|
|
<div className='compose-form__poll-wrapper'>
|
|
|
|
<ul>
|
2023-01-30 05:00:19 +11:00
|
|
|
{options.map((title, i) => <Option title={title} lang={lang} key={i} index={i} onChange={onChangeOption} onRemove={onRemoveOption} isPollMultiple={isMultiple} onToggleMultiple={this.handleToggleMultiple} autoFocus={i === autoFocusIndex} {...other} />)}
|
2019-03-06 14:53:37 +11:00
|
|
|
</ul>
|
|
|
|
|
|
|
|
<div className='poll__footer'>
|
2023-10-25 04:45:08 +11:00
|
|
|
<button type='button' disabled={options.size >= 4} className='button button-secondary' onClick={this.handleAddOption}><Icon id='plus' icon={AddIcon} /> <FormattedMessage {...messages.add_option} /></button>
|
2019-03-06 14:53:37 +11:00
|
|
|
|
2020-03-09 02:02:36 +11:00
|
|
|
{/* eslint-disable-next-line jsx-a11y/no-onchange */}
|
2019-12-11 05:39:02 +11:00
|
|
|
<select value={expiresIn} onChange={this.handleSelectDuration}>
|
2019-03-06 14:53:37 +11:00
|
|
|
<option value={300}>{intl.formatMessage(messages.minutes, { number: 5 })}</option>
|
|
|
|
<option value={1800}>{intl.formatMessage(messages.minutes, { number: 30 })}</option>
|
|
|
|
<option value={3600}>{intl.formatMessage(messages.hours, { number: 1 })}</option>
|
|
|
|
<option value={21600}>{intl.formatMessage(messages.hours, { number: 6 })}</option>
|
2022-12-16 02:20:34 +11:00
|
|
|
<option value={43200}>{intl.formatMessage(messages.hours, { number: 12 })}</option>
|
2019-03-06 14:53:37 +11:00
|
|
|
<option value={86400}>{intl.formatMessage(messages.days, { number: 1 })}</option>
|
|
|
|
<option value={259200}>{intl.formatMessage(messages.days, { number: 3 })}</option>
|
|
|
|
<option value={604800}>{intl.formatMessage(messages.days, { number: 7 })}</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-24 13:17:53 +11:00
|
|
|
|
|
|
|
export default injectIntl(PollForm);
|