index.tsx
5.62 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import QxIcon from '@/packages/qx-icon';
import { Button, Tabs, Tooltip } from 'antd';
import React, { useEffect, useState } from 'react';
import './index.less';
interface QxRelationProps {
relationTag?: any[];
onChange?: any;
value: string;
filter: string;
}
// scroll状态
export enum SCROLL {
start,
middle,
end,
}
const RelationList: React.FC<QxRelationProps> = (props) => {
const { relationTag = [], onChange = () => {}, value, filter } = props;
const [tabsIcon, setTabsIcon] = useState<boolean>(false);
const [scroll, setScroll] = useState<SCROLL>(SCROLL.start);
const getQuickSearchScroll = () => {
const length =
document?.getElementById('listRelation')?.children?.[0].children?.length;
if (tabsIcon && length == 4) {
const listWidth =
document?.getElementById('listRelation')?.children?.[0].children?.[1]
.children?.[0];
const contentWidth =
document?.getElementById('listRelation')?.children?.[0].children?.[1];
if (contentWidth?.clientWidth && listWidth?.clientWidth) {
if (contentWidth?.clientWidth - listWidth?.clientWidth > 0) {
setTabsIcon(false);
}
}
const translate =
document?.getElementById('listRelation')?.children?.[0].children?.[1]
?.children?.[0]?.style?.transform;
if (translate && listWidth?.clientWidth && contentWidth?.clientWidth) {
const px = translate.split('(').pop().split('p').shift();
if (Number(px) == 0) {
setScroll(SCROLL.start);
}
if (
Math.abs(Number(px)) > 0 &&
Math.abs(Number(px)) < listWidth.clientWidth
) {
setScroll(SCROLL.middle);
}
if (
Math.abs(Number(px)) ==
listWidth.clientWidth - contentWidth?.clientWidth
) {
setScroll(SCROLL.end);
}
}
} else if (!tabsIcon && length === 2) {
const listWidth =
document?.getElementById('listRelation')?.children?.[0].children?.[0]
.children?.[0];
const contentWidth =
document?.getElementById('listRelation')?.children?.[0].children?.[0];
if (contentWidth?.clientWidth && listWidth?.clientWidth) {
if (contentWidth?.clientWidth - listWidth?.clientWidth < 0) {
setTabsIcon(true);
}
}
}
};
useEffect(() => {
if (!relationTag || !relationTag.length) {
return;
}
getQuickSearchScroll();
}, [relationTag]);
const OperationsSlot = {
left: tabsIcon && (
<Button
style={{ width: 24, opacity: scroll == SCROLL.start ? 0.5 : 1 }}
type="text"
icon={
<QxIcon
type={'icon-tag-left-translate'}
style={{ color: '#333', fontSize: 10 }}
/>
}
onClick={() => {
// 左移一位
// const tabList = document?.getElementById('listRelation')?.children?.[0].children?.[1].children?.[0];
// const tabsNav = document?.getElementById('listRelation')?.children?.[0];
// if(tabList.clientWidth > tabsNav.clientWidth){
// const translateX = Number(tabList.style.cssText.split('px')[0].split('(')[1]) + 100;
// if(Math.abs(translateX) < (tabList.clientWidth - tabsNav.clientWidth) && translateX < 0){
// tabList.style.cssText = 'transform: translate(' + translateX +'px, 0px);'
// }else{
// tabList.style.cssText = 'transform: translate(' + 0 +'px, 0px);'
// }
// }
const a =
document?.getElementById('listRelation')?.children?.[0]
.children?.[1].children?.[0];
a.style.cssText = 'transform: translate(0px, 0px);';
setScroll(SCROLL.start);
}}
/>
),
right: tabsIcon && (
<Button
style={{ width: 24, opacity: scroll == SCROLL.end ? 0.5 : 1 }}
type="text"
icon={
<QxIcon
type={'icon-tag-right-translate'}
style={{ color: '#333', fontSize: 10 }}
/>
}
onClick={() => {
const a =
document?.getElementById('listRelation')?.children?.[0]
.children?.[1].children?.[0];
const b =
document?.getElementById('listRelation')?.children?.[0]
.children?.[1];
const widget = b?.clientWidth - a?.clientWidth;
if (widget < 0) {
a.style.cssText = 'transform: translate(' + widget + 'px,0px);';
setScroll(SCROLL.end);
}
}}
/>
),
};
return (
<div className={'qx-list-top-relation'}>
<Tabs
id="listRelation"
tabBarGutter={8}
tabBarExtraContent={OperationsSlot}
activeKey={value}
onTabScroll={() => {
getQuickSearchScroll();
}}
onChange={(e) => onChange(e)}
>
{relationTag.map((pane: any) => (
<>
{!(
pane.fieldKey === 'sys_role' &&
(filter.includes('_default_admin') ||
filter.includes('_app_admin'))
) && (
<>
{!(
pane.fieldKey !== 'sys_role' &&
filter.includes('default_user')
) && (
<Tabs.TabPane
tab={
<span style={{ padding: '0 20px' }}>
<Tooltip title={pane.title}>{pane.title}</Tooltip>
</span>
}
key={pane.fieldKey}
/>
)}
</>
)}
</>
))}
</Tabs>
</div>
);
};
export default RelationList;