38 lines
1016 B
JavaScript
38 lines
1016 B
JavaScript
import { Field } from "./Field.js";
|
|
import { ValidationError } from "../ValidationError.js";
|
|
|
|
export class EnumField extends Field {
|
|
constructor(key, values) {
|
|
super(key);
|
|
this.values = values;
|
|
this.optionLabels = values;
|
|
}
|
|
|
|
validate(value) {
|
|
if (this.values.includes(value))
|
|
return true;
|
|
|
|
throw new ValidationError(`Invalid value for field "${this.key}" - must be one of: ${this.values.join(", ")}`);
|
|
}
|
|
|
|
getInputHtml() {
|
|
return `<select id="${this.id}" name="${this.key}" ${this.isRequired ? "required" : ""}>` +
|
|
(this.isRequired || this.defaultValue ? "" : `<option value="" selected><not set></option>`) +
|
|
this.values.map((value, i) =>
|
|
`<option value="${value}" ${this.defaultValue === value ? "selected" : ""}>
|
|
${this.optionLabels[i] + (this.defaultValue === value ? " (Default)" : "")}
|
|
</option>`
|
|
) +
|
|
`</select>`;
|
|
}
|
|
|
|
getInputValue() {
|
|
return document.getElementById(this.id).value;
|
|
}
|
|
|
|
options(options) {
|
|
this.optionLabels = options;
|
|
return this;
|
|
}
|
|
}
|