@muban/muban
Version:
Writing components for server-rendered HTML
29 lines (28 loc) • 958 B
JavaScript
import { mapValues } from '../utils/utils';
import { refElement } from './refDefinitions';
/**
* Checks if a passed ref is a shortcut or not
* TODO: also support Component shortcuts?
*
* @param refDefinition
*/
export function isRefItemShortcut(refDefinition) {
return ['string', 'function'].includes(typeof refDefinition);
}
/**
* Convert "string" and "queryFn" shortcut refs to refElements
* @param refs
*/
export function normalizeRefs(refs) {
return mapValues(refs, (value) => isRefItemShortcut(value) ? refElement(value) : value);
}
/**
* Turns `refDefinitions` passed to the component definitions into "objects" that
* are passed as `refs` to the setup function
* @param refs
* @param instance
*/
export function createComponentRefs(refs, instance) {
const normalizedRefs = normalizeRefs(Object.assign(Object.assign({}, refs), { self: '_self_' }));
return mapValues(normalizedRefs, (value) => value.createRef(instance));
}