2024-04-20 20:46:11 +08:00
|
|
|
import { $Container, $ContainerOptions } from "./$Container";
|
|
|
|
import { $OptGroup } from "./$OptGroup";
|
|
|
|
import { $Option } from "./$Option";
|
2024-10-03 23:21:55 +08:00
|
|
|
import { $State, $StateArgument } from "../$State";
|
2024-05-15 18:38:45 +08:00
|
|
|
import { $HTMLElementAPIFilter, $HTMLElementAPIs } from "../$ElementTemplate";
|
|
|
|
import { $Util } from "../$Util";
|
2024-04-20 20:46:11 +08:00
|
|
|
|
|
|
|
export interface $SelectOptions extends $ContainerOptions {}
|
|
|
|
export class $Select extends $Container<HTMLSelectElement> {
|
2024-04-23 18:18:43 +08:00
|
|
|
constructor(options?: $SelectOptions) {
|
2024-04-20 20:46:11 +08:00
|
|
|
super('select')
|
|
|
|
}
|
|
|
|
|
|
|
|
add(option: $SelectContentType | OrMatrix<$SelectContentType>) {
|
|
|
|
this.insert(option);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
item(index: number) { return $(this.dom.item(index)) }
|
|
|
|
namedItem(name: string) { return $(this.dom.namedItem(name)) }
|
|
|
|
|
|
|
|
multiple(): boolean;
|
2024-04-23 18:18:43 +08:00
|
|
|
multiple(multiple: $StateArgument<boolean> | undefined): this;
|
|
|
|
multiple(multiple?: $StateArgument<boolean> | undefined) { return $.fluent(this, arguments, () => this.dom.multiple, () => $.set(this.dom, 'multiple', multiple))}
|
2024-04-20 20:46:11 +08:00
|
|
|
|
|
|
|
get length() { return this.dom.length }
|
|
|
|
get size() { return this.dom.size }
|
|
|
|
get options() { return Array.from(this.dom.options).map($option => $($option)) }
|
|
|
|
get selectedIndex() { return this.dom.selectedIndex }
|
|
|
|
get selectedOptions() { return Array.from(this.dom.selectedOptions).map($option => $($option)) }
|
2024-04-20 22:28:58 +08:00
|
|
|
|
|
|
|
value(): string;
|
2024-04-23 18:18:43 +08:00
|
|
|
value(value?: $StateArgument<string> | undefined): this;
|
2024-10-03 23:21:55 +08:00
|
|
|
value(value?: $StateArgument<string> | undefined) { return $.fluent(this, arguments, () => this.dom.value, () => $.set(this.dom, 'value', value as $State<string> | string, (value$) => {
|
|
|
|
this.on('input', () => {
|
|
|
|
if (value$.attributes.has(this.dom) === false) return;
|
|
|
|
if (typeof value$.value === 'string') (value$ as $State<string>).set(`${this.value()}`)
|
|
|
|
if (typeof value$.value === 'number') (value$ as unknown as $State<number>).set(Number(this.value()))
|
|
|
|
})
|
|
|
|
}))}
|
2024-04-20 22:28:58 +08:00
|
|
|
|
|
|
|
get labels() { return Array.from(this.dom.labels ?? []).map(label => $(label)) }
|
2024-04-20 20:46:11 +08:00
|
|
|
}
|
|
|
|
|
2024-05-15 18:38:45 +08:00
|
|
|
export interface $Select extends $HTMLElementAPIFilter<$Select, 'checkValidity' | 'reportValidity' | 'autocomplete' | 'name' | 'form' | 'required' | 'disabled' | 'validationMessage' | 'validity' | 'willValidate'> {}
|
|
|
|
$Util.mixin($Select, $HTMLElementAPIs.create('checkValidity', 'reportValidity', 'autocomplete', 'name', 'form', 'required', 'disabled', 'validationMessage', 'validity', 'willValidate'))
|
|
|
|
|
2024-04-20 20:46:11 +08:00
|
|
|
export type $SelectContentType = $Option | $OptGroup | undefined;
|