import { Component, ContextType } from "preact"; import { SettingsCtx } from "./SettingsContext.tsx"; import { ConfigType } from "../config.ts"; import Select from "preact-material-components/Select"; import { Ref, StateUpdater, useRef } from "preact/hooks"; interface obj { toString(): string; } type Props = { name: keyof ConfigType; list: { value: T; text?: string; disabled?: boolean }[]; description: string; /**@default {0}*/ selectedIndex?: number; /**@default {false}*/ disabled?: boolean; /**@default {false}*/ box?: boolean; /**@default {false}*/ outlined?: boolean; hintText?: string; set_value?: StateUpdater; }; type State = { selectedIndex: number; }; export default class SettingsSelect extends Component, State> { static contextType = SettingsCtx; declare context: ContextType; ref: Ref(); const id = `s-${this.props.name}`; return (
); } set_changed() { if (this.context) { this.context.set_changed((v) => { v.add(this.props.name); return v; }); } } set_value(index: number) { const value = this.props.list[index].value; if (this.props.set_value) { this.props.set_value(value); this.set_changed(); } else if (this.context) { this.context.set_settings((v) => { if (v) { const t: Record = v; t[this.props.name] = value; this.set_changed(); return t as ConfigType; } }); } } update(index: number) { const e = this.ref?.current; if (e) { const b = e.base; if (b) { const t = b as HTMLElement; const s = t.querySelector("select"); if (s) { s.selectedIndex = index; } } } } }