-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathindex.tsx
More file actions
155 lines (141 loc) · 5.4 KB
/
Copy pathindex.tsx
File metadata and controls
155 lines (141 loc) · 5.4 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, { ReactNode, useState } from 'react';
import { QuestionOutlined, UpOutlined } from '@dtinsight/react-icons';
import { Tooltip } from 'antd';
import classNames from 'classnames';
import useLocale from '../locale/useLocale';
import { LabelTooltipType, toTooltipProps } from '../utils';
import './style.scss';
export declare type SizeType = 'small' | 'middle' | 'large';
function isControlled(props: IBlockHeaderProps) {
return props.expand !== undefined;
}
export interface IBlockHeaderProps {
/** 标题 */
title: ReactNode;
/** 标题前的图标,默认是一个色块 */
addonBefore?: ReactNode;
/** 标题后的提示说明文字 */
description?: ReactNode;
/** 默认展示为问号的tooltip */
tooltip?: LabelTooltipType;
/** 后缀自定义内容块 */
addonAfter?: ReactNode;
/**
* 小标题 font-size: 12px; line-height: 32px
* 中标题 font-size: 14px; line-height: 40px
* 大标题 font-size: 16px; line-height: 40px
* 默认 中标题
*/
size?: SizeType;
/** 自定义 Bottom 值 */
spaceBottom?: number;
/** 标题一行的样式类名 */
className?: string;
/** 标题的样式类名 */
style?: React.CSSProperties;
/** 展示内容(children)的样式类名 */
contentClassName?: string;
/** 展示内容(children)的样式 */
contentStyle?: React.CSSProperties;
/** 是否显示背景, 默认 true */
background?: boolean;
/** 当前展开状态 */
expand?: boolean;
/** 是否默认展开内容, 默认为 undefined */
defaultExpand?: boolean;
/** 展开/收起的内容 */
children?: ReactNode;
/** 展开/收起时的回调 */
onExpand?: (expand: boolean) => void;
}
const prefixCls = 'dtc-block-header';
const preTitleRowCls = `${prefixCls}__title`;
const BlockHeader: React.FC<IBlockHeaderProps> = function (props) {
const {
title,
description = '',
tooltip,
size = 'middle',
spaceBottom = 16,
className = '',
contentClassName = '',
style = {},
contentStyle = {},
background = true,
defaultExpand,
addonAfter,
expand,
children = '',
addonBefore = <div className="addon-before--default" />,
onExpand,
} = props;
const [internalExpand, setInternalExpand] = useState(defaultExpand);
const locale = useLocale('BlockHeader');
const currentExpand = isControlled(props) ? expand : internalExpand;
// 只有在有了 children 并且设置了 expand/defaultExpand 的时候才能够展开收起
const showCollapse =
(typeof expand === 'boolean' || typeof defaultExpand === 'boolean') && children;
const tooltipProps = toTooltipProps(tooltip);
let bottomStyle;
if (spaceBottom)
bottomStyle =
showCollapse && currentExpand ? { marginBottom: 0 } : { marginBottom: spaceBottom };
const handleExpand = (expand: boolean) => {
if (!children) return;
!isControlled(props) && setInternalExpand(expand);
onExpand?.(expand);
};
return (
<div className={classNames(`${prefixCls}`, className)} style={{ ...bottomStyle, ...style }}>
<div
className={classNames(preTitleRowCls, `${preTitleRowCls}--${size}`, {
[`${preTitleRowCls}--background`]: background,
[`${preTitleRowCls}--pointer`]: showCollapse,
})}
onClick={() => showCollapse && handleExpand(!currentExpand)}
>
<div className="title__box">
{addonBefore ? (
<div className={`title__addon-before title__addon-before--${size}`}>
{addonBefore}
</div>
) : null}
<div className="title__text">{title}</div>
{tooltipProps?.title ? (
<div className={`title__tooltip`}>
<Tooltip {...tooltipProps}>
<QuestionOutlined />
</Tooltip>
</div>
) : null}
{description ? <div className={`title__description`}>{description}</div> : null}
</div>
{addonAfter && <div className={`title__addon-after`}>{addonAfter}</div>}
{showCollapse && (
<div className={`title__collapse`}>
<div className="collapse__text">
{currentExpand ? locale.collapse : locale.expand}
</div>
<UpOutlined
className={classNames('collapse__icon', {
'collapse__icon--up': currentExpand,
'collapse__icon--down': !currentExpand,
})}
/>
</div>
)}
</div>
{children && (
<div
className={classNames(`${prefixCls}__content`, contentClassName, {
[`${prefixCls}__content--active`]: currentExpand || !showCollapse,
})}
style={contentStyle}
>
{children}
</div>
)}
</div>
);
};
export default BlockHeader;