-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ApiReferencesTableSet.tsx
186 lines (157 loc) · 5.46 KB
/
ApiReferencesTableSet.tsx
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as React from 'react';
import { ActionButton, IButtonStyles } from '@fluentui/react/lib/Button';
import { Stack } from '@fluentui/react/lib/Stack';
import { Text } from '@fluentui/react/lib/Text';
import { ApiReferencesTable, gapTokens } from './ApiReferencesTable';
import {
IApiReferencesTableProps,
IApiInterfaceProperty,
IMethod,
IApiReferencesTableSetProps,
} from './ApiReferencesTableSet.types';
import { ITableRowJson, IPageJson } from '@fluentui/react/lib/common/DocPage.types';
import { extractAnchorLink } from '../../utilities/extractAnchorLink';
import { jumpToAnchor } from '../../utilities/index2';
import { getTokenResolver } from './tokenResolver';
export interface IApiReferencesTableSetState {
showSeeMore: boolean;
}
const TITLE_LINE_HEIGHT = 31.5;
const seeMoreButtonStyles: Partial<IButtonStyles> = {
root: { paddingLeft: '0px' },
textContainer: { paddingLeft: '4px' },
};
export class ApiReferencesTableSet extends React.Component<IApiReferencesTableSetProps, IApiReferencesTableSetState> {
public static defaultProps: Partial<IApiReferencesTableSetProps> = {
jumpToAnchors: true,
};
private _tableProps: IApiReferencesTableProps[];
constructor(props: IApiReferencesTableSetProps) {
super(props);
this.state = { showSeeMore: false };
this._tableProps = _generateTableProps(props.jsonDocs);
}
public render(): JSX.Element {
const { className } = this.props;
return (
<Stack tokens={gapTokens.large} className={className}>
{this._renderFirst()}
{this._renderEach()}
</Stack>
);
}
public componentDidMount(): void {
window.addEventListener('hashchange', this._onHashChange);
const anchor = extractAnchorLink(window.location.hash);
if (anchor && !this._allVisible) {
const section = this._tableProps.filter(x => x.name === anchor)[0];
if (section) {
this.setState({ showSeeMore: true });
}
}
}
public componentWillUnmount(): void {
window.removeEventListener('hashchange', this._onHashChange);
}
public componentDidUpdate(prevProps: IApiReferencesTableSetProps, prevState: IApiReferencesTableSetState): void {
if (prevState.showSeeMore === false && this.state.showSeeMore === true && this.props.jumpToAnchors) {
jumpToAnchor(undefined, TITLE_LINE_HEIGHT);
}
}
private get _allVisible(): boolean {
return this.props.showAll || this.state.showSeeMore;
}
private _renderFirst(): JSX.Element | undefined {
if (this._tableProps.length >= 1) {
const item = this._tableProps[0];
return <ApiReferencesTable {...item} />;
}
return undefined;
}
private _renderEach(): JSX.Element | undefined {
if (this._tableProps.length > 1) {
return (
<Stack tokens={gapTokens.medium}>
{!this.props.showAll && (
<ActionButton
iconProps={{ iconName: this.state.showSeeMore ? 'SkypeCircleMinus' : 'CirclePlus' }}
onClick={this._onClickSeeMore}
onRenderText={this._onRenderText}
styles={seeMoreButtonStyles}
>
See more
</ActionButton>
)}
{this._allVisible && (
<Stack tokens={gapTokens.large}>
{this._tableProps.map((item: IApiReferencesTableProps, index: number) =>
index !== 0 ? <ApiReferencesTable key={item.name} {...item} /> : undefined,
)}
</Stack>
)}
</Stack>
);
}
return undefined;
}
private _onHashChange = (): void => {
const anchor = extractAnchorLink(window.location.hash);
if (anchor) {
this.props.jumpToAnchors && jumpToAnchor(anchor, TITLE_LINE_HEIGHT);
if (!this._allVisible) {
const section = this._tableProps.filter(x => x.name === anchor)[0];
if (section) {
this.setState({ showSeeMore: true });
}
}
}
};
private _onRenderText(): JSX.Element {
return <Text variant="xLarge">See more</Text>;
}
private _onClickSeeMore = (): void => {
this.setState({
showSeeMore: !this.state.showSeeMore,
});
};
}
function _generateTableProps(jsonDocs: IPageJson | undefined): IApiReferencesTableProps[] {
if (!jsonDocs) {
return [];
}
const tokenResolver = getTokenResolver();
const propsName: string = `I${jsonDocs.name}Props`;
const results: IApiReferencesTableProps[] = [];
for (const table of jsonDocs.tables) {
const { kind, members, name, ...rest } = table;
const tableProps: IApiReferencesTableProps = {
...rest,
name,
title: kind !== 'typeAlias' ? name + ' ' + kind : name,
renderAs: kind,
properties: members || [],
tokenResolver: tokenResolver,
};
if (kind === 'class') {
// class members are a mix of IApiInterfaceProperty and IMethod
const classMembers: IApiInterfaceProperty[] = (tableProps.properties = []);
const classMethods: IMethod[] = (tableProps.methods = []);
(members as ITableRowJson[]).forEach(member => {
if (member.kind === 'method') {
classMethods.push(member);
} else {
classMembers.push(member);
}
});
}
if (jsonDocs.group === 'references' && jsonDocs.name === name) {
results.unshift(tableProps);
} else if (kind === 'interface' && propsName === name) {
// to ensure that I{componentName}Props comes first
results.unshift(tableProps);
} else {
results.push(tableProps);
}
}
return results;
}