58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
// Types
|
|
// eslint-disable-line vue/prefer-import-from-vue
|
|
|
|
/**
|
|
* Creates a factory function for props definitions.
|
|
* This is used to define props in a composable then override
|
|
* default values in an implementing component.
|
|
*
|
|
* @example Simplified signature
|
|
* (props: Props) => (defaults?: Record<keyof props, any>) => Props
|
|
*
|
|
* @example Usage
|
|
* const makeProps = propsFactory({
|
|
* foo: String,
|
|
* })
|
|
*
|
|
* defineComponent({
|
|
* props: {
|
|
* ...makeProps({
|
|
* foo: 'a',
|
|
* }),
|
|
* },
|
|
* setup (props) {
|
|
* // would be "string | undefined", now "string" because a default has been provided
|
|
* props.foo
|
|
* },
|
|
* }
|
|
*/
|
|
|
|
export function propsFactory(props, source) {
|
|
return defaults => {
|
|
return Object.keys(props).reduce((obj, prop) => {
|
|
const isObjectDefinition = typeof props[prop] === 'object' && props[prop] != null && !Array.isArray(props[prop]);
|
|
const definition = isObjectDefinition ? props[prop] : {
|
|
type: props[prop]
|
|
};
|
|
if (defaults && prop in defaults) {
|
|
obj[prop] = {
|
|
...definition,
|
|
default: defaults[prop]
|
|
};
|
|
} else {
|
|
obj[prop] = definition;
|
|
}
|
|
if (source && !obj[prop].source) {
|
|
obj[prop].source = source;
|
|
}
|
|
return obj;
|
|
}, {});
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Like `Partial<T>` but doesn't care what the value is
|
|
*/
|
|
|
|
// Copied from Vue
|
|
//# sourceMappingURL=propsFactory.mjs.map
|