Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 2x 2x | import type Community from '../index'
import {BaseInput} from './index'
export class InputRadio extends BaseInput {
type: 'radio' = 'radio'
options: NodeListOf<HTMLInputElement>
values: (string | number)[] = []
source: string | number
constructor(e: HTMLElement, site: Community) {
super(e, site)
this.listen = this.listen.bind(this)
this.options = e.querySelectorAll('input')
this.options.forEach(o => {
this.values.push(o.value)
o.addEventListener('click', this.listen)
})
Iif ('number' === typeof this.default && this.options[this.default]) this.default = this.values[this.default]
}
get() {
for (let i = this.options.length; i--; ) {
Iif (this.options[i].checked) {
this.set(i)
break
}
}
}
set(v: string | number) {
this.current_index = 'string' === typeof v ? this.values.indexOf(v) : v
if (-1 !== this.current_index) {
this.source =
this.values[this.current_index] in this.site.inputs
? (this.site.valueOf(this.values[this.current_index]) as string)
: this.values[this.current_index]
this.options[this.current_index].checked = true
} else this.source = undefined
this.site.request_queue(this.id)
}
listen(e: MouseEvent) {
this.set((e.target as HTMLInputElement).value)
}
add(value: string, display?: string, noadd?: boolean) {
const e = document.createElement('div'),
s = 'TRUE' === this.e.dataset.switch,
input = document.createElement('input'),
label = document.createElement('label')
e.className = 'form-check' + (s ? ' form-switch' : '')
e.appendChild(input)
e.appendChild(label)
input.autocomplete = 'off'
input.className = 'form-check-input'
Iif (s) input.role = 'switch'
input.type = 'radio'
input.name = this.e.id + '_options'
input.id = this.e.id + '_option' + this.e.childElementCount
input.value = value
label.innerText = display || this.site.data.format_label(value)
label.className = 'form-check-label'
label.setAttribute('for', e.firstElementChild.id)
Iif (!noadd) this.e.appendChild(e)
return input
}
}
|