Page MenuHomeSealhub

simple-input.ts
No OneTemporary

Authored By
Unknown
Size
1 KB
Referenced Files
None
Subscribers
None

simple-input.ts

import { BaseContext } from "koa";
import { FormField } from "../field";
import { FormData } from "../form";
import { FormFieldsList } from "../form-fields-list";
import { FormFieldControl } from "./controls";
export class SimpleInput extends FormFieldControl {
constructor(
public fieldname: string,
public options: {
id?: string;
label?: string;
autocomplete?: boolean;
type?:
| "color"
| "date"
| "email"
| "file"
| "month"
| "number"
| "password"
| "search"
| "tel"
| "text"
| "time"
| "url"
| "week";
value?: string;
placeholder?: string;
readonly?: boolean;
step?: number;
} = {}
) {
super([fieldname]);
}
_render(_: BaseContext, fields: FormField[], data: FormData) {
const field = FormFieldsList.getField(fields, this.fieldname);
if (!field) {
throw new Error("wrong field name");
}
const id = this.options.id || field.name;
const label = this.options.label || field.name;
const type = this.options.type || "text";
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const value = data.values[field.name] as string;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const raw_value = data.raw_values[field.name] as string;
const placeholder = this.options.placeholder || type;
const readonly = this.options.readonly || false;
const required = field.required;
const error = data.errors[field.name];
return /* HTML */ `<div class="input">
<label for="${id}">${label}</label>
<input
id="${id}"
type="${type}"
name="${field.name}"
value="${value === undefined
? raw_value == undefined
? ""
: raw_value
: value}"
placeholder="${placeholder}"
${readonly ? "readonly" : ""}
${required ? "required" : ""}
${!this.options.autocomplete ? `autocomplete="off"` : ""}
${this.options.step ? `step="${this.options.step}"` : ""}
/>
${error ? `<div class="input__error">${error}</div>` : ""}
</div>`;
}
}

File Metadata

Mime Type
application/javascript
Expires
Fri, Nov 27, 19:26 (8 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1228529
Default Alt Text
simple-input.ts (1 KB)

Event Timeline