Page MenuHomeSealhub

checkbox-group.ts
No OneTemporary

checkbox-group.ts

import { Context } from "koa";
import { FlatTemplatable } from "tempstream";
import { EnumMultipleField, FormField } from "../fields/field";
import { ProxyFormField } from "../fields/proxy-field";
import { FormDataValue, FormMessage } from "../form";
import { CheckboxWithValue } from "./checkbox";
import { FieldGroup, FormFieldControl } from "./controls";
export type DefaultCheckboxOptions = {
id?: string;
label?: string;
hide_errors?: boolean;
readonly?: boolean;
name?: string;
checkboxNameToLabel?: (name: string) => string;
classes?: string[];
};
export class CheckboxGroup<
Options extends DefaultCheckboxOptions = DefaultCheckboxOptions
> extends FormFieldControl {
constructor(
public field: EnumMultipleField<readonly string[], boolean>,
public options: Options = {} as Options
) {
super([field]);
}
wrapGroup(
ctx: Context,
data: Record<string, FormDataValue>,
messages: FormMessage[],
field_prefix: string,
form_id: string,
checkboxes: CheckboxWithValue[],
show_field_errors: boolean
): Promise<FlatTemplatable> {
return new FieldGroup(checkboxes, {
label: this.options.label,
classes: [
...(this.options.classes || []),
"field-group--checkboxes",
],
}).render(
ctx,
data,
messages,
field_prefix,
form_id,
show_field_errors
);
}
async render<Fields extends Record<string, FormField>>(
ctx: Context,
data: Record<string, FormDataValue>,
messages: FormMessage[],
field_prefix: string,
form_id: string,
show_field_errors: boolean
): Promise<FlatTemplatable> {
const { parsed: values } = await this.field.getValue(ctx, data);
const checkboxes = this.field.options.map(
(option) =>
new CheckboxWithValue(
new ProxyFormField(
false,
this.field.name,
false,
values.includes(option)
),
{
id: this.field.name + "__" + option,
label: (
this.options.checkboxNameToLabel ||
((s: string) => s)
)(option),
},
option
)
);
return this.wrapGroup(
ctx,
data,
messages,
field_prefix,
form_id,
checkboxes,
show_field_errors
);
}
}

File Metadata

Mime Type
text/x-java
Expires
Sat, Sep 20, 14:34 (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
930061
Default Alt Text
checkbox-group.ts (2 KB)

Event Timeline