element-vir
Version:
Heroic. Reactive. Declarative. Type safe. Web components without compromise.
26 lines (25 loc) • 1.04 kB
JavaScript
import { getObjectTypedKeys } from '@augment-vir/common';
export function assignInputs(element, inputs) {
const instanceState = element.instanceState;
getObjectTypedKeys(inputs).forEach((newInputKey) => {
if (instanceState && newInputKey in instanceState) {
throw new Error(`Cannot set input '${String(newInputKey)}' on '${element.tagName}'. '${element.tagName}' already has a state property with the same name.`);
}
if ('instanceInputs' in element) {
element.instanceInputs[newInputKey] =
inputs[newInputKey];
}
else {
element[newInputKey] = inputs[newInputKey];
}
});
/** Wipe out all inputs that weren't set to undefined (as expected) */
if ('instanceInputs' in element) {
getObjectTypedKeys(element.instanceInputs).forEach((existingKey) => {
if (!(existingKey in inputs)) {
element.instanceInputs[existingKey] =
undefined;
}
});
}
}