index.tsx
3.77 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
import { DownOutlined, UpOutlined } from '@ant-design/icons';
import { useSetState } from 'ahooks';
import {Button, Dropdown, Input } from 'antd';
import type { InputProps } from 'antd/lib/input';
import cls from 'classnames';
import React, { useImperativeHandle } from 'react';
import type {
DropdownContentOptions,
DropdownContentProps,
} from './dropdown-content';
import DropdownContent from './dropdown-content';
import './styles.less';
import {QxBaseIcon} from "@qx/common";
const prefix = 'qx-input-select';
/**
* 下拉选择
*/
export const QxInputSelect = React.forwardRef<any, InputSelectProps>(
(props, ref) => {
const {
className,
options = [],
dropdownProps,
onChange,
disabled,
from,
...rest
} = props;
const [state, setState] = useSetState<InputSelectState>({
visible: false,
});
const inputSuffix = (
<div className={`${prefix}__input-suffix`}>
{state.visible ? <UpOutlined /> : <DownOutlined />}
</div>
);
const handleChange = (val: DropdownContentOptions) => {
onChange?.(val);
setState({ visible: false });
};
/**
* 菜单显示状态改变时调用
*/
const onVisibleChange = (visible: boolean) => {
setState({ visible });
};
useImperativeHandle(ref, () => ({
closeDropdown: () => {
setState({ visible: false });
},
openDropdown: () => {
setState({ visible: true });
},
}));
return (
<div className={cls(prefix, className)}>
<Dropdown
open={state.visible}
destroyPopupOnHide
trigger={['click']}
className={`${prefix}__dropdown`}
dropdownRender={() => (
<DropdownContent
options={options}
onChange={handleChange}
{...dropdownProps}
/>
)}
getPopupContainer={(triggerNode) => {
if (props?.popupOnBody) {
return document.body;
} else {
return triggerNode;
}
}}
onOpenChange={onVisibleChange}
disabled={true}
>
<>
{
from === 'FORM'?
<Button
disabled={disabled}
className={`qx-input-select__button`}
tabIndex={1}
onClick={() => setState({ visible: !state.visible })}
>
{props?.prefix}
{ !props?.value ?
<span className={'qx-input-select__button-main qx-input-select__button-placeholder'}>{props?.placeholder}</span>:
<span className={'qx-input-select__button-main'}>
<span>{props?.appName}</span>
<QxBaseIcon
type="qx-icon-left"
className={'qx-input-select__button-svg'}
/>
<span>{props?.value}</span>
</span>
}
{inputSuffix}
</Button>:
<Input
placeholder="请选择"
readOnly
suffix={inputSuffix}
onClick={() => setState({ visible: !state.visible })}
{...rest}
className={`${prefix}__input`}
/>
}
</>
</Dropdown>
</div>
);
},
);
export interface InputSelectProps extends Omit<InputProps, 'onChange'> {
onChange?: (args: DropdownContentOptions) => void;
options?: DropdownContentOptions[];
dropdownProps?: DropdownContentProps;
disabled?: boolean;
popupOnBody?: boolean;
from?: string;
appName?: string;
}
export interface InputSelectState {
visible: boolean;
}