forked from baidu/amis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnchorNav.tsx
201 lines (174 loc) · 4.09 KB
/
AnchorNav.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React from 'react';
import {Renderer, RendererProps} from '../factory';
import {
AnchorNav as CAnchorNav,
AnchorNavSection
} from '../components/AnchorNav';
import {isVisible, autobind} from '../utils/helper';
import {filter} from '../utils/tpl';
import {find} from 'lodash';
import {BaseSchema, SchemaClassName, SchemaCollection} from '../Schema';
/**
* AnchorNavSection 锚点区域渲染器
* 文档:https://baidu.gitee.io/amis/docs/components/anchor-nav
*/
export type AnchorNavSectionSchema = {
/**
* 导航文字说明
*/
title: string;
/**
* 锚点链接
*/
href?: string;
/**
* 内容
*/
body?: SchemaCollection;
} & Omit<BaseSchema, 'type'>;
/**
* AnchorNav 锚点导航渲染器
* 文档:https://baidu.gitee.io/amis/docs/components/anchor-nav
*/
export interface AnchorNavSchema extends BaseSchema {
/**
* 指定为 AnchorNav 锚点导航渲染器
*/
type: 'anchor-nav';
/**
* 楼层集合
*/
links: Array<AnchorNavSectionSchema>;
/**
* 被激活(定位)的楼层
*/
active?: string | number;
/**
* 样式名
*/
className?: SchemaClassName;
/**
* 导航样式名
*/
linkClassName?: SchemaClassName;
/**
* 楼层样式名
*/
sectionClassName?: SchemaClassName;
}
export interface AnchorNavProps
extends RendererProps,
Omit<AnchorNavSchema, 'className' | 'linkClassName' | 'sectionClassName'> {
active?: string | number;
sectionRender?: (
section: AnchorNavSectionSchema,
props: AnchorNavProps,
index: number
) => JSX.Element;
}
export interface AnchorNavState {
active: any;
}
export default class AnchorNav extends React.Component<
AnchorNavProps,
AnchorNavState
> {
static defaultProps: Partial<AnchorNavProps> = {
className: '',
linkClassName: '',
sectionClassName: ''
};
renderSection?: (
section: AnchorNavSectionSchema,
props: AnchorNavProps,
index: number
) => JSX.Element;
constructor(props: AnchorNavProps) {
super(props);
// 设置默认激活项
const links = props.links;
let active: any = 0;
if (typeof props.active !== 'undefined') {
active = props.active;
} else {
const section: AnchorNavSectionSchema = find(
links,
section => section.href === props.active
) as AnchorNavSectionSchema;
active =
section && section.href
? section.href
: (links[0] && links[0].href) || 0;
}
this.state = {
active
};
}
@autobind
handleSelect(key: any) {
this.setState({
active: key
});
}
@autobind
locateTo(index: number) {
const {links} = this.props;
Array.isArray(links) &&
links[index] &&
this.setState({
active: links[index].href || index
});
}
render() {
const {
classnames: cx,
classPrefix: ns,
className,
linkClassName,
sectionClassName,
sectionRender,
render,
data
} = this.props;
let links = this.props.links;
if (!links) {
return null;
}
links = Array.isArray(links) ? links : [links];
let children: Array<JSX.Element | null> = [];
children = links.map((section, index) =>
isVisible(section, data) ? (
<AnchorNavSection
{...(section as any)}
title={filter(section.title, data)}
key={index}
name={section.href || index}
>
{this.renderSection
? this.renderSection(section, this.props, index)
: sectionRender
? sectionRender(section, this.props, index)
: render(`section/${index}`, section.body || '')}
</AnchorNavSection>
) : null
);
return (
<CAnchorNav
classPrefix={ns}
classnames={cx}
className={className}
linkClassName={linkClassName}
sectionClassName={sectionClassName}
onSelect={this.handleSelect}
active={this.state.active}
>
{children}
</CAnchorNav>
);
}
}
@Renderer({
test: /(^|\/)anchor-nav$/,
name: 'anchor-nav'
})
export class AnchorNavRenderer extends AnchorNav {}