index.tsx
2.68 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
import { DownOutlined, UpOutlined } from '@ant-design/icons';
import { useSetState } from 'ahooks';
import { 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';
const prefix = 'qx-input-select';
/**
* 下拉选择
*/
export const QxInputSelect = React.forwardRef<any, InputSelectProps>(
(props, ref) => {
const {
className,
options = [],
dropdownProps,
onChange,
disabled,
...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={disabled}
>
<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;
}
export interface InputSelectState {
visible: boolean;
}