Skip to content

Commit

Permalink
feat(rdom-components): add staticRadio() component
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 4, 2021
1 parent 5f8a722 commit ff3d1c4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/rdom-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./dropdown";
export * from "./editor";
export * from "./icon-button";
export * from "./input";
export * from "./radio";
export * from "./tabs";
40 changes: 40 additions & 0 deletions packages/rdom-components/src/radio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Fn, Fn2 } from "@thi.ng/api";
import { div, InputRadioAttribs, label, radio } from "@thi.ng/hiccup-html";
import { $input, ComponentLike } from "@thi.ng/rdom";
import type { ISubscription } from "@thi.ng/rstream";

export interface RadioOpts<T> {
attribs: Partial<InputRadioAttribs>;
label: Fn2<T, ComponentLike, ComponentLike>;
value: Fn<T, string>;
}

export const staticRadio = <T = string>(
items: T[],
sel: ISubscription<string, string>,
opts?: Partial<RadioOpts<T>>
) => {
opts = {
label: (x, radio) => label({ for: String(x) }, String(x), radio),
value: String,
...opts,
};
return div(
{ ...opts.attribs },
...items.map($radio(sel, <Required<RadioOpts<T>>>opts))
);
};

const $radio =
<T>(sel: ISubscription<string, string>, opts: RadioOpts<T>) =>
(x: T) => {
let v = opts.value(x);
return opts.label(
x,
radio({
value: v,
checked: sel.map((x) => v === x),
onchange: $input(sel),
})
);
};

0 comments on commit ff3d1c4

Please sign in to comment.