vuestic-ui
Version:
Vue 3 UI Framework
46 lines (45 loc) • 1.42 kB
JavaScript
import { computed, provide, getCurrentInstance, inject } from "vue";
const CHILD_COMPONENT_PROP_PREFIX = "child:";
const CHILD_COMPONENTS_INJECT_KEY = "$va:childComponents";
const defineChildProps = (obj) => {
return Object.keys(obj).reduce((acc, key) => {
const childName = `${CHILD_COMPONENT_PROP_PREFIX}${key}`;
acc[childName] = {
type: Object,
required: false,
default: void 0
};
return acc;
}, {});
};
const useChildComponents = (props) => {
const childProps = computed(() => {
const propNames = Object.keys(props);
return propNames.reduce((acc, propName) => {
if (propName.startsWith(CHILD_COMPONENT_PROP_PREFIX)) {
const childName = propName.slice(CHILD_COMPONENT_PROP_PREFIX.length);
acc[childName] = props[propName];
}
return acc;
}, {});
});
provide(CHILD_COMPONENTS_INJECT_KEY, childProps);
};
const injectChildPropsFromParent = () => {
var _a;
const childName = (_a = getCurrentInstance()) == null ? void 0 : _a.attrs["va-child"];
if (!childName) {
return null;
}
const childProps = inject(CHILD_COMPONENTS_INJECT_KEY);
if (!(childProps == null ? void 0 : childProps.value)) {
return null;
}
return computed(() => childProps.value[childName]);
};
export {
defineChildProps as d,
injectChildPropsFromParent as i,
useChildComponents as u
};
//# sourceMappingURL=useChildComponents.mjs.map