-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi-reference.tsx
More file actions
134 lines (122 loc) · 3.68 KB
/
Copy pathapi-reference.tsx
File metadata and controls
134 lines (122 loc) · 3.68 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import type { FC } from 'react'
import styles from './controller-docs.module.css'
export type Locale = 'en' | 'zh'
export type LocalizedText = Readonly<Record<Locale, string>>
export type ApiRow = Readonly<{
name: string
type: string
defaultValue: string
description: LocalizedText
required?: boolean
}>
export type ApiSection = Readonly<{
name: string
inherited: LocalizedText
rows: readonly ApiRow[]
}>
export type DataAttribute = Readonly<{
description: LocalizedText
name: string
values?: string
}>
export const localizedText = (en: string, zh: string): LocalizedText => ({ en, zh })
const labels = {
en: {
attribute: 'Data Attributes',
defaultValue: 'Default',
description: 'Description',
name: 'Prop',
required: 'required',
type: 'Type',
},
zh: {
attribute: 'Data 属性',
defaultValue: '默认值',
description: '说明',
name: '属性',
required: '必填',
type: '类型',
},
} as const
type ApiTableProps = Readonly<{
lang: Locale
section: ApiSection
}>
type DataAttributesProps = Readonly<{
attributes: readonly DataAttribute[]
component: string
lang: Locale
}>
export const DataAttributes: FC<DataAttributesProps> = ({ attributes, component, lang }) => {
if (attributes.length === 0) return null
return (
<section data-data-attributes={component}>
<h3>{labels[lang].attribute}</h3>
<dl>
{attributes.map((attribute) => (
<div key={attribute.name}>
<dt>
<code>{attribute.name}</code>
{attribute.values && (
<>
{' '}
— <code>{attribute.values}</code>
</>
)}
</dt>
<dd>{attribute.description[lang]}</dd>
</div>
))}
</dl>
</section>
)
}
export const ApiTable: FC<ApiTableProps> = ({ lang, section }) => {
const localeLabels = labels[lang]
return (
<section className={styles.apiSection} aria-labelledby={`${section.name}-api`}>
<h3 className={styles.apiHeading} id={`${section.name}-api`}>
{section.name}
</h3>
<p className={styles.apiInherited}>{section.inherited[lang]}</p>
{section.rows.length > 0 && (
<div className={styles.tableWrap}>
<table className={styles.apiTable}>
<caption className={styles.visuallyHidden}>
{section.name} {lang === 'zh' ? '公开属性' : 'public props'}
</caption>
<thead>
<tr>
<th scope="col">{localeLabels.name}</th>
<th scope="col">{localeLabels.description}</th>
<th scope="col">{localeLabels.type}</th>
<th scope="col">{localeLabels.defaultValue}</th>
</tr>
</thead>
<tbody>
{section.rows.map((row) => (
<tr key={row.name}>
<td className={styles.propName} data-label={localeLabels.name}>
<code>{row.name}</code>
{row.required && (
<span className={styles.required} title={localeLabels.required}>
*
</span>
)}
</td>
<td data-label={localeLabels.description}>{row.description[lang]}</td>
<td data-label={localeLabels.type}>
<code>{row.type}</code>
</td>
<td data-label={localeLabels.defaultValue}>
<code>{row.defaultValue}</code>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</section>
)
}