1
|
|
import { PropDescriptor, ComponentDoc } from './types'
|
2
|
1
|
import cleanName from './cleanName'
|
3
|
|
|
4
|
1
|
function getDefaultText(): string {
|
5
|
1
|
return 'Default Example Usage'
|
6
|
|
}
|
7
|
|
|
8
|
1
|
function getDefaultNumber(): string {
|
9
|
1
|
return '42'
|
10
|
|
}
|
11
|
|
|
12
|
1
|
function getDefaultBoolean(): string {
|
13
|
1
|
return 'true'
|
14
|
|
}
|
15
|
|
|
16
|
1
|
function getDefaultArray(): string {
|
17
|
1
|
return '[1, 2, 3]'
|
18
|
|
}
|
19
|
|
|
20
|
1
|
function getDefaultFunction(): string {
|
21
|
1
|
return '() => void'
|
22
|
|
}
|
23
|
|
|
24
|
1
|
function getDefaultDate(): string {
|
25
|
1
|
return 'new Date(\'2012-12-12\')'
|
26
|
|
}
|
27
|
|
|
28
|
1
|
function getDefaultObject(): string {
|
29
|
1
|
return '{}'
|
30
|
|
}
|
31
|
|
|
32
|
1
|
function getDefault(prop: PropDescriptor): string {
|
33
|
1
|
if (!prop || !prop.type) {
|
34
|
0
|
return getDefaultText()
|
35
|
1
|
} else if (prop.values && prop.values.length) {
|
36
|
0
|
return prop.values[0]
|
37
|
1
|
} else if (prop.type.name === 'string') {
|
38
|
1
|
return getDefaultText()
|
39
|
1
|
} else if (prop.type.name === 'number') {
|
40
|
1
|
return getDefaultNumber()
|
41
|
1
|
} else if (prop.type.name === 'boolean') {
|
42
|
1
|
return getDefaultBoolean()
|
43
|
1
|
} else if (prop.type.name === 'object') {
|
44
|
1
|
return getDefaultObject()
|
45
|
1
|
} else if (prop.type.name === 'array') {
|
46
|
1
|
return getDefaultArray()
|
47
|
1
|
} else if (prop.type.name === 'func') {
|
48
|
1
|
return getDefaultFunction()
|
49
|
1
|
} else if (prop.type.name === 'date') {
|
50
|
1
|
return getDefaultDate()
|
51
|
|
}
|
52
|
1
|
return getDefaultText()
|
53
|
|
}
|
54
|
|
|
55
|
1
|
export default (doc: ComponentDoc): string => {
|
56
|
1
|
const { displayName, props, slots } = doc
|
57
|
1
|
const cleanedName = cleanName(displayName)
|
58
|
1
|
const propsAttr: string[] = props
|
59
|
1
|
? props
|
60
|
1
|
.filter(p => p.required)
|
61
|
|
.map(
|
62
|
1
|
p =>
|
63
|
1
|
` ${!p || !p.type || p.type.name === 'string' ? '' : ':'}${p.name}="${getDefault(p)}"`
|
64
|
|
)
|
65
|
1
|
: []
|
66
|
1
|
return `<${cleanedName}${propsAttr.join(' ')}${
|
67
|
1
|
!slots || !slots.filter(s => s.name === 'default')
|
68
|
1
|
? ' />'
|
69
|
1
|
: `>${getDefaultText()}</${cleanedName}>`
|
70
|
|
}`
|
71
|
|
}
|