|
1 | 1 | import { InputFieldMDRC } from '../renderChildren/InputFieldMDRC'; |
| 2 | +import { MBExtendedLiteral } from '../utils/Utils'; |
| 3 | +import { ComputedSignal } from '../utils/Signal'; |
2 | 4 |
|
3 | | -export abstract class AbstractInputField { |
4 | | - static allowBlock: boolean = true; |
5 | | - static allowInline: boolean = true; |
| 5 | +export type GetInputFieldType<T extends AbstractInputField<any>> = T extends AbstractInputField<infer R> ? R : unknown; |
| 6 | + |
| 7 | +export abstract class AbstractInputField<T extends MBExtendedLiteral> { |
6 | 8 | renderChild: InputFieldMDRC; |
7 | | - onValueChange: (value: any) => void | Promise<void>; |
| 9 | + filteredWriteSignal: ComputedSignal<MBExtendedLiteral | undefined, T>; |
| 10 | + onValueChange: (value: T | undefined) => void; |
8 | 11 |
|
9 | 12 | constructor(inputFieldMDRC: InputFieldMDRC) { |
10 | 13 | this.renderChild = inputFieldMDRC; |
11 | 14 |
|
12 | | - this.onValueChange = (value: any): void => { |
| 15 | + this.onValueChange = (value: T | undefined) => { |
13 | 16 | console.debug(`meta-bind | input field on value change`, value); |
14 | 17 | this.renderChild.readSignal.set(value); |
15 | 18 | }; |
16 | 19 |
|
17 | | - this.renderChild.writeSignal.registerListener({ |
18 | | - callback: (value: any) => { |
| 20 | + this.filteredWriteSignal = new ComputedSignal<MBExtendedLiteral | undefined, T>(this.renderChild.writeSignal, (value: MBExtendedLiteral | undefined) => { |
| 21 | + return this.filterValue(value); |
| 22 | + }); |
| 23 | + |
| 24 | + this.filteredWriteSignal.registerListener({ |
| 25 | + callback: (value: T): void => { |
19 | 26 | if (!this.isEqualValue(value)) { |
20 | | - this.setValue(value); |
| 27 | + this.updateDisplayValue(value); |
21 | 28 | } |
22 | 29 | }, |
23 | 30 | }); |
24 | 31 | } |
25 | 32 |
|
| 33 | + getInitialValue(): T { |
| 34 | + return this.filteredWriteSignal.get(); |
| 35 | + } |
| 36 | + |
| 37 | + isEqualValue(value: T | undefined): boolean { |
| 38 | + return this.getValue() === value; |
| 39 | + } |
| 40 | + |
26 | 41 | /** |
27 | 42 | * Returns the current content of the input field |
28 | 43 | */ |
29 | | - abstract getValue(): any; |
| 44 | + abstract getValue(): T | undefined; |
30 | 45 |
|
31 | 46 | /** |
32 | | - * Sets the value on this input field, overriding the current content |
| 47 | + * Maps an extended literal to the value for the input field |
33 | 48 | * |
34 | 49 | * @param value |
35 | 50 | */ |
36 | | - abstract setValue(value: any): void; |
| 51 | + abstract filterValue(value: MBExtendedLiteral | undefined): T; |
37 | 52 |
|
38 | | - /** |
39 | | - * Checks if the value is the same as the value of this input field |
40 | | - * |
41 | | - * @param value |
42 | | - */ |
43 | | - abstract isEqualValue(value: any): boolean; |
| 53 | + abstract updateDisplayValue(value: T): void; |
44 | 54 |
|
45 | 55 | /** |
46 | 56 | * Returns the default value of this input field |
47 | 57 | */ |
48 | | - abstract getDefaultValue(): any; |
| 58 | + abstract getDefaultValue(): T; |
49 | 59 |
|
50 | 60 | /** |
51 | 61 | * Returns the HTML element this input field is wrapped in |
|
0 commit comments