12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- // 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;
- }, {});
- };
- }
- //# sourceMappingURL=propsFactory.mjs.map
|