( value: ModelSignal<TValue>, options: TransformedValueOptions<TValue, TRaw>, )
| 112 | * ``` |
| 113 | */ |
| 114 | export function transformedValue<TValue, TRaw>( |
| 115 | value: ModelSignal<TValue>, |
| 116 | options: TransformedValueOptions<TValue, TRaw>, |
| 117 | ): TransformedValueSignal<TRaw> { |
| 118 | const {parse, format} = options; |
| 119 | const parser = createParser(value, value.set, parse); |
| 120 | |
| 121 | // Create the result signal with overridden set/update and a `parseErrors` property. |
| 122 | const rawValue = linkedSignal(() => format(value())); |
| 123 | const result = rawValue as WritableSignal<TRaw> & { |
| 124 | parseErrors: Signal<readonly ValidationError.WithoutFieldTree[]>; |
| 125 | }; |
| 126 | result.parseErrors = parser.errors; |
| 127 | const originalSet = result.set.bind(result); |
| 128 | |
| 129 | // Wire up the integration with the form control (parse errors and reset handling). |
| 130 | const integration = inject(FORM_CONTROL_INTEGRATION, {self: true, optional: true}); |
| 131 | if (integration) { |
| 132 | integration.setParseErrors(parser.errors); |
| 133 | integration.onReset = (resetValue) => { |
| 134 | parser.reset(); |
| 135 | const modelValue = resetValue !== undefined ? resetValue : value(); |
| 136 | originalSet(format(modelValue)); |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | // Notify the parser when `set` or `update` is called on the raw value |
| 141 | result.set = (newRawValue: TRaw) => { |
| 142 | parser.setRawValue(newRawValue); |
| 143 | originalSet(newRawValue); |
| 144 | }; |
| 145 | result.update = (updateFn: (value: TRaw) => TRaw) => { |
| 146 | result.set(updateFn(rawValue())); |
| 147 | }; |
| 148 | |
| 149 | return result; |
| 150 | } |
no test coverage detected
searching dependent graphs…