fix poll checkboxes

This commit is contained in:
Henry Jameson 2025-03-13 02:10:20 +02:00
commit 9f1f37ec7e
4 changed files with 101 additions and 112 deletions

View file

@ -1,6 +1,7 @@
import Timeago from 'components/timeago/timeago.vue'
import genRandomSeed from '../../services/random_seed/random_seed.service.js'
import RichContent from 'components/rich_content/rich_content.jsx'
import Checkbox from 'components/checkbox/checkbox.vue'
import { forEach, map } from 'lodash'
import { usePollsStore } from 'src/stores/polls'
@ -9,7 +10,8 @@ export default {
props: ['basePoll', 'emoji'],
components: {
Timeago,
RichContent
RichContent,
Checkbox
},
data () {
return {
@ -78,26 +80,15 @@ export default {
resultTitle (option) {
return `${option.votes_count}/${this.totalVotesCount} ${this.$t('polls.votes')}`
},
activateOption (index) {
// forgive me father: doing checking the radio/checkboxes
// in code because of customized input elements need either
// a) an extra element for the actual graphic, or b) use a
// pseudo element for the label. We use b) which mandates
// using "for" and "id" matching which isn't nice when the
// same poll appears multiple times on the site (notifs and
// timeline for example). With code we can make sure it just
// works without altering the pseudo element implementation.
const allElements = this.$el.querySelectorAll('input')
const clickedElement = this.$el.querySelector(`input[value="${index}"]`)
activateOption (index, value) {
let result
if (this.poll.multiple) {
// Checkboxes, toggle only the clicked one
clickedElement.checked = !clickedElement.checked
result = this.choices || this.options.map(() => false)
} else {
// Radio button, uncheck everything and check the clicked one
forEach(allElements, element => { element.checked = false })
clickedElement.checked = true
result = this.options.map(() => false)
}
this.choices = map(allElements, e => e.checked)
result[index] = value
this.choices = result
},
optionId (index) {
return `poll${this.poll.id}-${index}`