All files / src/utils/data-value-out data-value-out.ts

100% Statements 18/18
100% Branches 11/11
100% Functions 3/3
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 4095x         95x   4x 4x 1x 1x           4x       11x 11x 4x   4x 4x 4x 4x   3x   3x 2x              
import { Metadata, Utils } from '@zeedhi/core';
import { Form } from '../../components/zd-form/form';
import { Input } from '../../components/zd-input/input';
import { ISelectDataValueOut, ISelectDataValueOutItem } from './interfaces';
 
export class DataValueOutHelper {
	private static getFormParent(select: ISelectDataValueOut) {
		let { formParent } = select as Input;
		if (select.dataValueOutFormName) {
			try {
				formParent = Metadata.getInstance<Form>(select.dataValueOutFormName);
			} catch (_error) {
				// instance not found, use input.formParent
			}
		}
 
		return formParent;
	}
 
	public static setDataValueOut(select: ISelectDataValueOut, value: any) {
		const { dataValueOut } = select;
		if (dataValueOut?.length && typeof value === 'object') {
			const formParent = DataValueOutHelper.getFormParent(select);
 
			if (formParent) {
				dataValueOut.forEach((columns: ISelectDataValueOutItem) => {
					const { column, columnOnForm } = columns;
					if (!column || !columnOnForm) return;
 
					const currentValue = value ? value[column] : null;
 
					if (!Utils.isEqual(formParent.value[columnOnForm], currentValue)) {
						formParent.value[columnOnForm] = currentValue;
					}
				});
			}
		}
	}
}