tweb/src/components/radioField.ts
morethanwords 70c741882a Rollback scrollbar to native Safari
General settings layout
2021-01-03 14:59:12 +04:00

48 lines
1.3 KiB
TypeScript

import appStateManager from "../lib/appManagers/appStateManager";
import { getDeepProperty } from "../helpers/object";
const RadioField = (text: string, name: string, value?: string, stateKey?: string) => {
const label = document.createElement('label');
label.classList.add('radio-field');
const input = document.createElement('input');
input.type = 'radio';
input.id = input.name = 'input-radio-' + name;
if(value) {
input.value = value;
if(stateKey) {
appStateManager.getState().then(state => {
input.checked = getDeepProperty(state, stateKey) === value;
});
input.addEventListener('change', () => {
appStateManager.setByKey(stateKey, value);
});
}
}
const main = document.createElement('div');
main.classList.add('radio-field-main');
if(text) {
main.innerHTML = text;
/* const caption = document.createElement('div');
caption.classList.add('radio-field-main-caption');
caption.innerHTML = text;
if(subtitle) {
label.classList.add('radio-field-with-subtitle');
caption.insertAdjacentHTML('beforeend', `<div class="radio-field-main-subtitle">${subtitle}</div>`);
}
main.append(caption); */
}
label.append(input, main);
return {label, input, main};
};
export default RadioField;