|
| 1 | +import { InputFieldMDRC } from '../../renderChildren/InputFieldMDRC'; |
| 2 | +import { InputFieldComponent } from './InputFieldComponent'; |
| 3 | +import { SvelteComponent } from 'svelte'; |
| 4 | +import { ComputedSignal, Listener, Notifier } from '../../utils/Signal'; |
| 5 | +import { InputFieldArgumentType } from '../InputFieldConfigs'; |
| 6 | +import { DefaultValueInputFieldArgument } from '../../inputFieldArguments/arguments/DefaultValueInputFieldArgument'; |
| 7 | + |
| 8 | +export abstract class NewAbstractInputField<MetadataValueType, ComponentValueType> extends Notifier<MetadataValueType, Listener<MetadataValueType>> { |
| 9 | + readonly renderChild: InputFieldMDRC; |
| 10 | + readonly inputFieldComponent: InputFieldComponent<ComponentValueType>; |
| 11 | + readonly signal: ComputedSignal<any, MetadataValueType>; |
| 12 | + |
| 13 | + protected constructor(renderChild: InputFieldMDRC) { |
| 14 | + super(); |
| 15 | + |
| 16 | + this.renderChild = renderChild; |
| 17 | + this.inputFieldComponent = new InputFieldComponent<ComponentValueType>(this.getSvelteComponent()); |
| 18 | + |
| 19 | + this.signal = new ComputedSignal<any, MetadataValueType>(this.renderChild.writeSignal, (value: any): MetadataValueType => { |
| 20 | + const filteredValue = this.filterValue(value); |
| 21 | + return filteredValue !== undefined ? filteredValue : this.getDefaultValue(); |
| 22 | + }); |
| 23 | + |
| 24 | + this.signal.registerListener({ |
| 25 | + callback: value => this.inputFieldComponent.setValue(this.reverseMapValue(value)), |
| 26 | + }); |
| 27 | + |
| 28 | + this.inputFieldComponent.registerListener({ |
| 29 | + callback: value => { |
| 30 | + console.log('input field component change', value); |
| 31 | + this.notifyListeners(this.mapValue(value)); |
| 32 | + }, |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + protected abstract getSvelteComponent(): typeof SvelteComponent; |
| 37 | + |
| 38 | + /** |
| 39 | + * Takes in any value and returns the value if it is type of `T`, `undefined` otherwise. |
| 40 | + * |
| 41 | + * @param value |
| 42 | + */ |
| 43 | + protected abstract filterValue(value: any): MetadataValueType | undefined; |
| 44 | + |
| 45 | + protected abstract getFallbackDefaultValue(): ComponentValueType; |
| 46 | + |
| 47 | + /** |
| 48 | + * Maps a metadata value to a component value. If the value can't be mapped, the function should return `undefined`. |
| 49 | + * |
| 50 | + * @param value |
| 51 | + */ |
| 52 | + protected abstract rawReverseMapValue(value: MetadataValueType): ComponentValueType | undefined; |
| 53 | + protected abstract rawMapValue(value: ComponentValueType): MetadataValueType; |
| 54 | + |
| 55 | + private reverseMapValue(value: MetadataValueType): ComponentValueType { |
| 56 | + const mappedValue = this.rawReverseMapValue(value); |
| 57 | + if (mappedValue !== undefined) { |
| 58 | + return mappedValue; |
| 59 | + } |
| 60 | + const mappedDefaultValue = this.rawReverseMapValue(this.getDefaultValue()); |
| 61 | + if (mappedDefaultValue !== undefined) { |
| 62 | + return mappedDefaultValue; |
| 63 | + } |
| 64 | + return this.getFallbackDefaultValue(); |
| 65 | + } |
| 66 | + |
| 67 | + private mapValue(value: ComponentValueType): MetadataValueType { |
| 68 | + return this.rawMapValue(value); |
| 69 | + } |
| 70 | + |
| 71 | + private getValue(): MetadataValueType { |
| 72 | + return this.signal.get(); |
| 73 | + } |
| 74 | + |
| 75 | + private getDefaultValue(): MetadataValueType { |
| 76 | + const defaultValueArgument = this.renderChild.getArgument(InputFieldArgumentType.DEFAULT_VALUE) as DefaultValueInputFieldArgument | undefined; |
| 77 | + if (!defaultValueArgument) { |
| 78 | + return this.mapValue(this.getFallbackDefaultValue()); |
| 79 | + } |
| 80 | + const filteredValue = this.filterValue(defaultValueArgument.value); |
| 81 | + return filteredValue !== undefined ? filteredValue : this.mapValue(this.getFallbackDefaultValue()); |
| 82 | + } |
| 83 | + |
| 84 | + protected getMountArgs(): Record<string, any> { |
| 85 | + return {}; |
| 86 | + } |
| 87 | + |
| 88 | + public mount(container: HTMLElement): void { |
| 89 | + this.inputFieldComponent.mount(container, this.reverseMapValue(this.getValue()), this.getMountArgs()); |
| 90 | + } |
| 91 | + |
| 92 | + public unmount(): void { |
| 93 | + this.inputFieldComponent.unmount(); |
| 94 | + } |
| 95 | +} |
0 commit comments