-
Notifications
You must be signed in to change notification settings - Fork 341
/
extract-component-options.ts
44 lines (37 loc) · 1.43 KB
/
extract-component-options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { ExtractComponentEmits, ExtractComponentProps, DefineComponentOptions } from './types'
import { getComponentProps } from './resolve-component-props'
/**
* Returns component props options.
*
*
* @param ignoreProps - deprecated - prefer using lodash omit instead
*
* @returns object that looks like this:
* ```ts
* {
* modelValue: { type: String, required: true },
* options: { type: Array, default: [] },
* size: { type: String as PropType<'small' | 'medium' | 'large'>, default: 'medium' }
* }
* ```
*/
export function extractComponentProps<T, IgnoreProps extends string = ''> (component: T, ignoreProps?: IgnoreProps[]): Omit<ExtractComponentProps<T>, IgnoreProps> {
const props: any = getComponentProps(component as any)
// TODO: Not sure if it is a good idea to handle ignore props here
// Looks like it is not type safe. Need a separated filter object function
if (ignoreProps) {
return Object
.keys(props)
.reduce<any>((acc, propName) => {
if (ignoreProps.includes(propName as unknown as IgnoreProps)) { return acc }
if (props[propName] === undefined) { return acc }
acc[propName] = typeof props[propName] === 'string' ? {} : props[propName]
return acc
}, {})
}
return props
}
/** Returns component emits option */
export function extractComponentEmits<T> (component: T): ExtractComponentEmits<T> {
return [...new Set((component as any).emits)] as any
}