diff --git a/components/BCheckbox.tsx b/components/BCheckbox.tsx new file mode 100644 index 0000000..6eb3c28 --- /dev/null +++ b/components/BCheckbox.tsx @@ -0,0 +1,48 @@ +import { Component, ContextType } from "preact"; +import Checkbox from "preact-material-components/Checkbox"; +import { StateUpdater } from "preact/hooks"; +import { BCtx } from "./BContext.tsx"; + +type Props = { + id?: string; + checked: boolean; + name?: string; + description?: string; + set_value?: StateUpdater; +}; + +export default class BCheckbox extends Component { + static contextType = BCtx; + declare context: ContextType; + set_value(value: boolean) { + if (this.props.set_value) { + this.props.set_value(value); + } else if (this.context) { + this.context.set_value((v) => { + v[this.props.name || ""] = value; + return v; + }); + } + } + render() { + let label = null; + if (this.props.description) { + label = ; + } + return ( +
+ { + if (ev.target) { + const e = ev.target as HTMLInputElement; + this.set_value(e.checked); + } + }} + /> + {label} +
+ ); + } +} diff --git a/components/BContext.tsx b/components/BContext.tsx new file mode 100644 index 0000000..e8a656f --- /dev/null +++ b/components/BContext.tsx @@ -0,0 +1,29 @@ +import { Component, ComponentChild, createContext } from "preact"; +import { StateUpdater } from "preact/hooks"; + +type State = { + set_value: StateUpdater>; +}; + +type Props = { + children: ComponentChild; + set_value: StateUpdater>; +}; + +export const BCtx = createContext(null); + +export default class BContext extends Component { + constructor(props: Props) { + super(props); + this.state = { + set_value: props.set_value, + }; + } + render() { + return ( + + {this.props.children} + + ); + } +} diff --git a/components/BSelect.tsx b/components/BSelect.tsx index a6c7b69..b6df991 100644 --- a/components/BSelect.tsx +++ b/components/BSelect.tsx @@ -1,6 +1,7 @@ -import { Component } from "preact"; +import { Component, ContextType } from "preact"; import Select from "preact-material-components/Select"; import { Ref, StateUpdater, useRef } from "preact/hooks"; +import { BCtx } from "./BContext.tsx"; interface obj { toString(): string; @@ -19,7 +20,8 @@ type Props = { /**@default {false}*/ outlined?: boolean; hintText?: string; - set_value: StateUpdater; + set_value?: StateUpdater; + name?: string; }; type State = { @@ -28,6 +30,8 @@ type State = { export default class SettingsSelect extends Component, State> { + static contextType = BCtx; + declare context: ContextType; ref: Ref