index.tsx
3.53 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
import { BlockOutlined } from '@ant-design/icons';
import { useSetState } from 'ahooks';
import { Button } from 'antd';
import cls from 'classnames';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import { QxAppSelector } from '../qx-app-selector';
import type { InputSelectProps } from '../qx-input-select';
import { QxInputSelect } from '../qx-input-select';
import './styles.less';
import {getAppFromList} from "./service";
const prefix = 'qx-form-select';
const FormSelectMain: React.FC<FormSelectProps>= (props)=> {
const {
className,
options = [],
onChange,
value,
loading,
appId,
request,
disabled,
placeholder,
title
} = props;
const [state, setState] = useSetState<FormSelectState>({
visible: false,
modalVisible: false,
});
const inputSelectRef = useRef<any>();
const handleChange = (val: any) => {
if (!val?.code) return;
function onOk() {
if (val?.code) {
onChange?.(val);
setState({
visible: false,
});
}
}
onOk();
};
const onOpenOther = useCallback(() => {
inputSelectRef.current?.closeDropdown();
setState({
modalVisible: true,
});
}, []);
return (
<div className={cls(prefix, className)}>
<QxInputSelect
ref={inputSelectRef}
value={value?.name}
defaultValue={value?.name}
placeholder={placeholder || '请选择数据源'}
prefix={<BlockOutlined style={{ color: '#52c41a' }} />}
options={options}
onChange={handleChange}
dropdownProps={{
showSearch: true,
loading,
listHeight: 160,
renderBottom: (
<Button
className={`${prefix}__dropdown-bottom`}
type="link"
onClick={onOpenOther}
>
其他应用
</Button>
),
}}
disabled={disabled}
popupOnBody={props?.popupOnBody}
/>
{state.modalVisible ? (
<QxAppSelector
title={title || placeholder || '选择数据源'}
item={{
flag: 'SINGLE',
currentId: '',
appId: appId,
}}
onChange={handleChange}
flag="join"
showTableTypeTab={false}
modalProps={{
width: 550,
destroyOnClose: true,
open: state.modalVisible,
onOk: () => setState({ modalVisible: false }),
onCancel: () => setState({ modalVisible: false }),
}}
request={request}
/>
) : null}
</div>
);
}
/**
* 选择数据源
*/
export const QxFormSelect: React.FC<FormSelectProps> = (props) => {
const { options= [] as any , optionId } = props;
const [selectOptions, setSelectOptions] = useState([])
useEffect(() => {
console.log(optionId,'llllloptions')
if(optionId && !options?.length) {
getAppFromList(optionId).then((res: any)=> {
if(res?.length){
setSelectOptions(res)
} else {
setSelectOptions([])
}
}).catch(()=> {
setSelectOptions([])
})
} else {
setSelectOptions(options)
}
}, []);
return <FormSelectMain {...props} options={selectOptions}/>
};
interface FormSelectProps extends InputSelectProps {
value?: any;
loading?: boolean;
appId?: string;
request?: any;
disabled?: boolean;
popupOnBody?: boolean;
optionId?: string; // 获取options传入表单的appId
}
interface FormSelectState {
visible: boolean;
modalVisible: boolean;
}