37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
import { camelize, computed, getCurrentInstance, toRef } from "vue";
|
|
|
|
//#region src/shared/useForwardProps.ts
|
|
/**
|
|
* The `useForwardProps` function in TypeScript takes in a set of props and returns a computed value
|
|
* that combines default props with assigned props from the current instance.
|
|
* @param {T} props - The `props` parameter is an object that represents the props passed to a
|
|
* component.
|
|
* @returns computed value that combines the default props, preserved props, and assigned props.
|
|
*/
|
|
function useForwardProps(props) {
|
|
const vm = getCurrentInstance();
|
|
const defaultProps = Object.keys(vm?.type.props ?? {}).reduce((prev, curr) => {
|
|
const defaultValue = (vm?.type.props[curr]).default;
|
|
if (defaultValue !== void 0) prev[curr] = defaultValue;
|
|
return prev;
|
|
}, {});
|
|
const refProps = toRef(props);
|
|
return computed(() => {
|
|
const preservedProps = {};
|
|
const assignedProps = vm?.vnode.props ?? {};
|
|
Object.keys(assignedProps).forEach((key) => {
|
|
preservedProps[camelize(key)] = assignedProps[key];
|
|
});
|
|
return Object.keys({
|
|
...defaultProps,
|
|
...preservedProps
|
|
}).reduce((prev, curr) => {
|
|
if (refProps.value[curr] !== void 0) prev[curr] = refProps.value[curr];
|
|
return prev;
|
|
}, {});
|
|
});
|
|
}
|
|
|
|
//#endregion
|
|
export { useForwardProps };
|
|
//# sourceMappingURL=useForwardProps.js.map
|