@aura-react-comp/ui 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/esm/alert/index.d.ts +28 -0
- package/esm/alert/index.js +76 -0
- package/esm/alert/index.less +172 -0
- package/esm/avatar/index.d.ts +41 -0
- package/esm/avatar/index.js +117 -0
- package/esm/avatar/index.less +163 -0
- package/esm/badge/index.d.ts +29 -0
- package/esm/badge/index.js +84 -0
- package/esm/badge/index.less +215 -0
- package/esm/breadcrumb/index.d.ts +31 -0
- package/esm/breadcrumb/index.js +91 -0
- package/esm/breadcrumb/index.less +107 -0
- package/esm/button/index.d.ts +28 -0
- package/esm/button/index.js +59 -0
- package/esm/button/index.less +191 -0
- package/esm/card/index.d.ts +67 -0
- package/esm/card/index.js +156 -0
- package/esm/card/index.less +231 -0
- package/esm/checkbox/index.d.ts +35 -0
- package/esm/checkbox/index.js +118 -0
- package/esm/checkbox/index.less +175 -0
- package/esm/collapse/index.d.ts +45 -0
- package/esm/collapse/index.js +157 -0
- package/esm/collapse/index.less +144 -0
- package/esm/divider/index.d.ts +16 -0
- package/esm/divider/index.js +37 -0
- package/esm/divider/index.less +115 -0
- package/esm/dropdown/index.d.ts +38 -0
- package/esm/dropdown/index.js +108 -0
- package/esm/dropdown/index.less +247 -0
- package/esm/empty/index.d.ts +35 -0
- package/esm/empty/index.js +87 -0
- package/esm/empty/index.less +67 -0
- package/esm/flex/index.d.ts +24 -0
- package/esm/flex/index.js +64 -0
- package/esm/flex/index.less +9 -0
- package/esm/form/index.d.ts +121 -0
- package/esm/form/index.js +443 -0
- package/esm/form/index.less +258 -0
- package/esm/global.d.ts +6 -0
- package/esm/index.d.ts +78 -0
- package/esm/index.js +41 -0
- package/esm/input/index.d.ts +51 -0
- package/esm/input/index.js +217 -0
- package/esm/input/index.less +369 -0
- package/esm/layout/index.d.ts +46 -0
- package/esm/layout/index.js +135 -0
- package/esm/layout/index.less +132 -0
- package/esm/menu/index.d.ts +91 -0
- package/esm/menu/index.js +294 -0
- package/esm/menu/index.less +340 -0
- package/esm/message/index.d.ts +33 -0
- package/esm/message/index.js +199 -0
- package/esm/message/index.less +118 -0
- package/esm/notification/index.d.ts +33 -0
- package/esm/notification/index.js +229 -0
- package/esm/notification/index.less +175 -0
- package/esm/pagination/index.d.ts +38 -0
- package/esm/pagination/index.js +182 -0
- package/esm/pagination/index.less +230 -0
- package/esm/popconfirm/index.d.ts +37 -0
- package/esm/popconfirm/index.js +115 -0
- package/esm/popconfirm/index.less +229 -0
- package/esm/radio/index.d.ts +33 -0
- package/esm/radio/index.js +122 -0
- package/esm/radio/index.less +155 -0
- package/esm/rate/index.d.ts +30 -0
- package/esm/rate/index.js +143 -0
- package/esm/rate/index.less +87 -0
- package/esm/result/index.d.ts +19 -0
- package/esm/result/index.js +69 -0
- package/esm/result/index.less +104 -0
- package/esm/scrollbar/index.d.ts +18 -0
- package/esm/scrollbar/index.js +35 -0
- package/esm/scrollbar/index.less +67 -0
- package/esm/select/index.d.ts +63 -0
- package/esm/select/index.js +358 -0
- package/esm/select/index.less +471 -0
- package/esm/slider/index.d.ts +36 -0
- package/esm/slider/index.js +255 -0
- package/esm/slider/index.less +154 -0
- package/esm/space/index.d.ts +16 -0
- package/esm/space/index.js +34 -0
- package/esm/space/index.less +34 -0
- package/esm/spin/index.d.ts +44 -0
- package/esm/spin/index.js +152 -0
- package/esm/spin/index.less +179 -0
- package/esm/steps/index.d.ts +55 -0
- package/esm/steps/index.js +165 -0
- package/esm/steps/index.less +394 -0
- package/esm/switch/index.d.ts +30 -0
- package/esm/switch/index.js +56 -0
- package/esm/switch/index.less +160 -0
- package/esm/tabs/index.d.ts +45 -0
- package/esm/tabs/index.js +170 -0
- package/esm/tabs/index.less +228 -0
- package/esm/tag/index.d.ts +52 -0
- package/esm/tag/index.js +120 -0
- package/esm/tag/index.less +227 -0
- package/esm/textarea/index.d.ts +27 -0
- package/esm/textarea/index.js +84 -0
- package/esm/textarea/index.less +226 -0
- package/esm/theme/ThemeProvider.d.ts +15 -0
- package/esm/theme/ThemeProvider.js +32 -0
- package/esm/theme/index.d.ts +2 -0
- package/esm/theme/index.js +1 -0
- package/esm/theme/tokens.css +183 -0
- package/esm/theme/useTheme.d.ts +1 -0
- package/esm/theme/useTheme.js +1 -0
- package/esm/tooltip/index.d.ts +38 -0
- package/esm/tooltip/index.js +149 -0
- package/esm/tooltip/index.less +244 -0
- package/esm/typography/index.d.ts +26 -0
- package/esm/typography/index.js +92 -0
- package/esm/typography/index.less +107 -0
- package/esm/upload/index.d.ts +68 -0
- package/esm/upload/index.js +447 -0
- package/esm/upload/index.less +376 -0
- package/esm/upload/request.d.ts +26 -0
- package/esm/upload/request.js +38 -0
- package/package.json +56 -3
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
.aura-result {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-direction: column;
|
|
4
|
+
align-items: center;
|
|
5
|
+
justify-content: center;
|
|
6
|
+
text-align: center;
|
|
7
|
+
padding: var(--aura-spacing-8) var(--aura-spacing-4);
|
|
8
|
+
min-height: 300px;
|
|
9
|
+
|
|
10
|
+
/* ===== 图标区域 ===== */
|
|
11
|
+
&-icon {
|
|
12
|
+
display: flex;
|
|
13
|
+
align-items: center;
|
|
14
|
+
justify-content: center;
|
|
15
|
+
margin-bottom: var(--aura-spacing-4);
|
|
16
|
+
animation: aura-result-icon-in var(--aura-duration-slow)
|
|
17
|
+
cubic-bezier(0.34, 1.56, 0.64, 1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* ===== 标题 ===== */
|
|
21
|
+
&-title {
|
|
22
|
+
font-size: var(--aura-font-size-xl);
|
|
23
|
+
font-weight: 600;
|
|
24
|
+
color: var(--aura-text);
|
|
25
|
+
line-height: 1.5;
|
|
26
|
+
margin-bottom: var(--aura-spacing-2);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* ===== 副标题 ===== */
|
|
30
|
+
&-subtitle {
|
|
31
|
+
font-size: var(--aura-font-size-md);
|
|
32
|
+
color: var(--aura-text-secondary);
|
|
33
|
+
line-height: 1.6;
|
|
34
|
+
margin-bottom: var(--aura-spacing-4);
|
|
35
|
+
max-width: 480px;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/* ===== 附加内容 ===== */
|
|
39
|
+
&-extra {
|
|
40
|
+
display: flex;
|
|
41
|
+
align-items: center;
|
|
42
|
+
justify-content: center;
|
|
43
|
+
gap: var(--aura-spacing-3);
|
|
44
|
+
margin-top: var(--aura-spacing-2);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* ===== 变体颜色 ===== */
|
|
48
|
+
&&-success {
|
|
49
|
+
.aura-result-icon {
|
|
50
|
+
color: var(--aura-success);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
&&-error {
|
|
55
|
+
.aura-result-icon {
|
|
56
|
+
color: var(--aura-error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
&&-warning {
|
|
61
|
+
.aura-result-icon {
|
|
62
|
+
color: var(--aura-warning);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
&&-info {
|
|
67
|
+
.aura-result-icon {
|
|
68
|
+
color: var(--aura-info);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
&&-404,
|
|
73
|
+
&&-403,
|
|
74
|
+
&&-500 {
|
|
75
|
+
.aura-result-icon {
|
|
76
|
+
color: var(--aura-text-tertiary);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* ===== 图标缩放入场动画 ===== */
|
|
82
|
+
@keyframes aura-result-icon-in {
|
|
83
|
+
from {
|
|
84
|
+
opacity: 0;
|
|
85
|
+
transform: scale(0.5);
|
|
86
|
+
}
|
|
87
|
+
to {
|
|
88
|
+
opacity: 1;
|
|
89
|
+
transform: scale(1);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* ===== 暗色模式 ===== */
|
|
94
|
+
[data-theme='dark'] {
|
|
95
|
+
.aura-result {
|
|
96
|
+
&-title {
|
|
97
|
+
color: var(--aura-text);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
&-subtitle {
|
|
101
|
+
color: var(--aura-text-secondary);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface ScrollbarProps {
|
|
3
|
+
/** 最大高度 */
|
|
4
|
+
maxHeight?: number | string;
|
|
5
|
+
/** 最大宽度 */
|
|
6
|
+
maxWidth?: number | string;
|
|
7
|
+
/** 是否始终显示滚动条
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
alwaysShow?: boolean;
|
|
11
|
+
/** 自定义类名 */
|
|
12
|
+
className?: string;
|
|
13
|
+
/** 自定义样式 */
|
|
14
|
+
style?: React.CSSProperties;
|
|
15
|
+
/** 内容 */
|
|
16
|
+
children?: React.ReactNode;
|
|
17
|
+
}
|
|
18
|
+
export declare const Scrollbar: React.ForwardRefExoticComponent<ScrollbarProps & React.RefAttributes<HTMLDivElement>>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React, { forwardRef } from 'react';
|
|
2
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
3
|
+
import "./index.less";
|
|
4
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
5
|
+
export const Scrollbar = /*#__PURE__*/forwardRef(({
|
|
6
|
+
maxHeight,
|
|
7
|
+
maxWidth,
|
|
8
|
+
alwaysShow = false,
|
|
9
|
+
className,
|
|
10
|
+
style,
|
|
11
|
+
children
|
|
12
|
+
}, ref) => {
|
|
13
|
+
const cls = classNames(prefixCls('scrollbar'), alwaysShow && prefixCls('scrollbar-always'), className);
|
|
14
|
+
|
|
15
|
+
// 处理尺寸值
|
|
16
|
+
const normalizeSize = value => {
|
|
17
|
+
if (value === undefined) return undefined;
|
|
18
|
+
return typeof value === 'number' ? `${value}px` : value;
|
|
19
|
+
};
|
|
20
|
+
const scrollbarStyle = {
|
|
21
|
+
...style,
|
|
22
|
+
maxHeight: normalizeSize(maxHeight),
|
|
23
|
+
maxWidth: normalizeSize(maxWidth)
|
|
24
|
+
};
|
|
25
|
+
return /*#__PURE__*/_jsx("div", {
|
|
26
|
+
ref: ref,
|
|
27
|
+
className: cls,
|
|
28
|
+
style: scrollbarStyle,
|
|
29
|
+
role: "region",
|
|
30
|
+
"aria-label": "\u53EF\u6EDA\u52A8\u533A\u57DF",
|
|
31
|
+
tabIndex: 0,
|
|
32
|
+
children: children
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
Scrollbar.displayName = 'Scrollbar';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/* ===== Scrollbar 组件样式 ===== */
|
|
2
|
+
|
|
3
|
+
.aura-scrollbar {
|
|
4
|
+
overflow: auto;
|
|
5
|
+
box-sizing: border-box;
|
|
6
|
+
|
|
7
|
+
// ===== 自定义 webkit 滚动条 =====
|
|
8
|
+
&::-webkit-scrollbar {
|
|
9
|
+
width: 6px;
|
|
10
|
+
height: 6px;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
&::-webkit-scrollbar-track {
|
|
14
|
+
background: transparent;
|
|
15
|
+
border-radius: var(--aura-radius-full);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
&::-webkit-scrollbar-thumb {
|
|
19
|
+
background: transparent;
|
|
20
|
+
border-radius: var(--aura-radius-full);
|
|
21
|
+
transition: background var(--aura-duration-normal) var(--aura-easing);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ===== 悬停时显示滚动条 =====
|
|
25
|
+
&:hover::-webkit-scrollbar-thumb {
|
|
26
|
+
background: var(--aura-primary-400);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
&::-webkit-scrollbar-thumb:hover {
|
|
30
|
+
background: var(--aura-primary-600);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ===== 焦点时显示滚动条 =====
|
|
34
|
+
&:focus::-webkit-scrollbar-thumb {
|
|
35
|
+
background: var(--aura-primary-400);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ===== 始终显示滚动条 =====
|
|
39
|
+
&&-always {
|
|
40
|
+
&::-webkit-scrollbar-thumb {
|
|
41
|
+
background: var(--aura-primary-400);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ===== 暗色模式 =====
|
|
47
|
+
[data-theme='dark'] {
|
|
48
|
+
.aura-scrollbar {
|
|
49
|
+
&:hover::-webkit-scrollbar-thumb {
|
|
50
|
+
background: var(--aura-primary-500);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
&::-webkit-scrollbar-thumb:hover {
|
|
54
|
+
background: var(--aura-primary-400);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
&:focus::-webkit-scrollbar-thumb {
|
|
58
|
+
background: var(--aura-primary-500);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
&.aura-scrollbar-always {
|
|
62
|
+
&::-webkit-scrollbar-thumb {
|
|
63
|
+
background: var(--aura-primary-500);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/** 单个选项 */
|
|
3
|
+
export interface SelectOption {
|
|
4
|
+
label: React.ReactNode;
|
|
5
|
+
value: string | number;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** labelInValue 模式下的值形态 */
|
|
9
|
+
export interface LabelInValueObject {
|
|
10
|
+
value: string | number;
|
|
11
|
+
label?: React.ReactNode;
|
|
12
|
+
}
|
|
13
|
+
/** 对外值形态(受 labelInValue 影响) */
|
|
14
|
+
export type SelectOutValue = string | number | (string | number)[] | LabelInValueObject | LabelInValueObject[];
|
|
15
|
+
export interface SelectProps extends React.AriaAttributes {
|
|
16
|
+
/** 变体样式
|
|
17
|
+
* @default 'default'
|
|
18
|
+
*/
|
|
19
|
+
variant?: 'default' | 'filled' | 'bordered';
|
|
20
|
+
/** 尺寸
|
|
21
|
+
* @default 'md'
|
|
22
|
+
*/
|
|
23
|
+
size?: 'sm' | 'md' | 'lg';
|
|
24
|
+
/** 是否禁用 */
|
|
25
|
+
disabled?: boolean;
|
|
26
|
+
/** 是否加载中 */
|
|
27
|
+
loading?: boolean;
|
|
28
|
+
/** 选项列表 */
|
|
29
|
+
options: SelectOption[];
|
|
30
|
+
/** 当前值(受控);labelInValue 时为 `{ value, label }` 形态 */
|
|
31
|
+
value?: SelectOutValue;
|
|
32
|
+
/** 默认值(非受控);labelInValue 时为 `{ value, label }` 形态 */
|
|
33
|
+
defaultValue?: SelectOutValue;
|
|
34
|
+
/** 是否多选 */
|
|
35
|
+
multiple?: boolean;
|
|
36
|
+
/** 取值是否携带 label(`{ value, label }` 形态),影响 value / defaultValue / onChange
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
labelInValue?: boolean;
|
|
40
|
+
/** 是否可搜索 */
|
|
41
|
+
searchable?: boolean;
|
|
42
|
+
/** 是否可清除 */
|
|
43
|
+
clearable?: boolean;
|
|
44
|
+
/** 搜索过滤行为:默认按 label 本地包含匹配;传 `false` 关闭本地过滤(配合 onSearch 远程搜索);传函数自定义
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
filterOption?: boolean | ((inputValue: string, option: SelectOption) => boolean);
|
|
48
|
+
/** 搜索输入变化回调(配合 filterOption={false} 做远程搜索) */
|
|
49
|
+
onSearch?: (value: string) => void;
|
|
50
|
+
/** 多选模式下最多显示的标签数量,超出以 `+N...` 收敛 */
|
|
51
|
+
maxTagCount?: number;
|
|
52
|
+
/** 下拉列表为空时展示的内容 */
|
|
53
|
+
notFoundContent?: React.ReactNode;
|
|
54
|
+
/** 占位文本 */
|
|
55
|
+
placeholder?: string;
|
|
56
|
+
/** 值变化回调 */
|
|
57
|
+
onChange?: (value: SelectOutValue) => void;
|
|
58
|
+
/** 自定义类名 */
|
|
59
|
+
className?: string;
|
|
60
|
+
/** 自定义样式 */
|
|
61
|
+
style?: React.CSSProperties;
|
|
62
|
+
}
|
|
63
|
+
export declare const Select: React.ForwardRefExoticComponent<SelectProps & React.RefAttributes<HTMLDivElement>>;
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import React, { forwardRef, useState, useRef, useEffect, useCallback, useId } from 'react';
|
|
2
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
3
|
+
import "./index.less";
|
|
4
|
+
|
|
5
|
+
/** 单个选项 */
|
|
6
|
+
|
|
7
|
+
/** labelInValue 模式下的值形态 */
|
|
8
|
+
|
|
9
|
+
/** 对外值形态(受 labelInValue 影响) */
|
|
10
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
11
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
12
|
+
/** 获取选项的显示标签 */
|
|
13
|
+
function getOptionLabel(options, val) {
|
|
14
|
+
const opt = options.find(o => o.value === val);
|
|
15
|
+
return opt ? opt.label : val;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 把外部传入的值(可能 labelInValue)归一化为内部原始值 */
|
|
19
|
+
function toRawValue(val, labelInValue) {
|
|
20
|
+
if (val === undefined) return undefined;
|
|
21
|
+
if (labelInValue) {
|
|
22
|
+
if (Array.isArray(val)) {
|
|
23
|
+
return val.map(o => o.value);
|
|
24
|
+
}
|
|
25
|
+
return val.value;
|
|
26
|
+
}
|
|
27
|
+
return val;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 把内部原始值转换为对外值(labelInValue 时包裹 label) */
|
|
31
|
+
function toOutValue(raw, labelInValue, options) {
|
|
32
|
+
if (labelInValue) {
|
|
33
|
+
const wrap = val => ({
|
|
34
|
+
value: val,
|
|
35
|
+
label: getOptionLabel(options, val)
|
|
36
|
+
});
|
|
37
|
+
return Array.isArray(raw) ? raw.map(wrap) : wrap(raw);
|
|
38
|
+
}
|
|
39
|
+
return raw;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 判断某个值是否被选中 */
|
|
43
|
+
function isSelected(currentValue, optionValue) {
|
|
44
|
+
if (currentValue === undefined) return false;
|
|
45
|
+
if (Array.isArray(currentValue)) return currentValue.includes(optionValue);
|
|
46
|
+
return currentValue === optionValue;
|
|
47
|
+
}
|
|
48
|
+
export const Select = /*#__PURE__*/forwardRef(({
|
|
49
|
+
variant = 'default',
|
|
50
|
+
size = 'md',
|
|
51
|
+
disabled = false,
|
|
52
|
+
loading = false,
|
|
53
|
+
options,
|
|
54
|
+
value: controlledValue,
|
|
55
|
+
defaultValue,
|
|
56
|
+
multiple = false,
|
|
57
|
+
labelInValue = false,
|
|
58
|
+
searchable = false,
|
|
59
|
+
clearable = false,
|
|
60
|
+
filterOption,
|
|
61
|
+
onSearch,
|
|
62
|
+
maxTagCount,
|
|
63
|
+
notFoundContent,
|
|
64
|
+
placeholder = '请选择',
|
|
65
|
+
onChange,
|
|
66
|
+
className,
|
|
67
|
+
style,
|
|
68
|
+
// role="combobox" 的元素无法被 <label> 关联,名称只能由 aria-* 提供
|
|
69
|
+
'aria-label': ariaLabel,
|
|
70
|
+
'aria-labelledby': ariaLabelledBy,
|
|
71
|
+
'aria-describedby': ariaDescribedBy,
|
|
72
|
+
'aria-invalid': ariaInvalid
|
|
73
|
+
}, ref) => {
|
|
74
|
+
// 内部维护的值(非受控模式,一律为原始值)
|
|
75
|
+
const [internalValue, setInternalValue] = useState(() => {
|
|
76
|
+
const rawDefault = toRawValue(defaultValue, labelInValue);
|
|
77
|
+
if (rawDefault !== undefined) return rawDefault;
|
|
78
|
+
return multiple ? [] : '';
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// 当前生效的值(内部一律使用原始值,labelInValue 的转换发生在边界)
|
|
82
|
+
const rawControlled = toRawValue(controlledValue, labelInValue);
|
|
83
|
+
const currentValue = rawControlled !== undefined ? rawControlled : internalValue;
|
|
84
|
+
|
|
85
|
+
// 下拉面板展开状态
|
|
86
|
+
const [open, setOpen] = useState(false);
|
|
87
|
+
// 搜索关键字
|
|
88
|
+
const [searchText, setSearchText] = useState('');
|
|
89
|
+
// 键盘聚焦高亮的选项索引
|
|
90
|
+
const [activeIndex, setActiveIndex] = useState(-1);
|
|
91
|
+
const containerRef = useRef(null);
|
|
92
|
+
const searchInputRef = useRef(null);
|
|
93
|
+
const listId = useId();
|
|
94
|
+
|
|
95
|
+
// 监听打开状态或过滤关键字变化,重置高亮索引
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
setActiveIndex(-1);
|
|
98
|
+
}, [open, searchText]);
|
|
99
|
+
|
|
100
|
+
// 当 activeIndex 变化时,自动将高亮项滚动到可视区域
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (open && activeIndex >= 0) {
|
|
103
|
+
const dropdown = document.getElementById(listId);
|
|
104
|
+
if (dropdown) {
|
|
105
|
+
const activeEl = dropdown.querySelectorAll(`.${prefixCls('select-option')}`)[activeIndex];
|
|
106
|
+
if (activeEl && typeof activeEl.scrollIntoView === 'function') {
|
|
107
|
+
activeEl.scrollIntoView({
|
|
108
|
+
block: 'nearest'
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}, [activeIndex, open, listId]);
|
|
114
|
+
|
|
115
|
+
// 点击外部关闭下拉面板
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
const handleClickOutside = e => {
|
|
118
|
+
if (containerRef.current && !containerRef.current.contains(e.target)) {
|
|
119
|
+
setOpen(false);
|
|
120
|
+
setSearchText('');
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
document.addEventListener('mousedown', handleClickOutside);
|
|
124
|
+
return () => document.removeEventListener('mousedown', handleClickOutside);
|
|
125
|
+
}, []);
|
|
126
|
+
|
|
127
|
+
// 展开时自动聚焦搜索框
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
if (open && searchable && searchInputRef.current) {
|
|
130
|
+
searchInputRef.current.focus();
|
|
131
|
+
}
|
|
132
|
+
}, [open, searchable]);
|
|
133
|
+
|
|
134
|
+
// 过滤选项:filterOption=false 关闭本地过滤(远程搜索场景),函数为自定义过滤
|
|
135
|
+
const filteredOptions = options.filter(opt => {
|
|
136
|
+
if (!searchText) return true;
|
|
137
|
+
if (filterOption === false) return true;
|
|
138
|
+
if (typeof filterOption === 'function') {
|
|
139
|
+
return filterOption(searchText, opt);
|
|
140
|
+
}
|
|
141
|
+
const label = typeof opt.label === 'string' ? opt.label : String(opt.label);
|
|
142
|
+
return label.toLowerCase().includes(searchText.toLowerCase());
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// 选中/取消选中某个值
|
|
146
|
+
const handleSelect = useCallback(optionValue => {
|
|
147
|
+
let nextValue;
|
|
148
|
+
if (multiple) {
|
|
149
|
+
const arr = Array.isArray(currentValue) ? [...currentValue] : [];
|
|
150
|
+
const idx = arr.indexOf(optionValue);
|
|
151
|
+
if (idx >= 0) {
|
|
152
|
+
arr.splice(idx, 1);
|
|
153
|
+
} else {
|
|
154
|
+
arr.push(optionValue);
|
|
155
|
+
}
|
|
156
|
+
nextValue = arr;
|
|
157
|
+
} else {
|
|
158
|
+
nextValue = optionValue;
|
|
159
|
+
}
|
|
160
|
+
if (controlledValue === undefined) {
|
|
161
|
+
setInternalValue(nextValue);
|
|
162
|
+
}
|
|
163
|
+
onChange?.(toOutValue(nextValue, labelInValue, options));
|
|
164
|
+
|
|
165
|
+
// 单选模式下选完关闭面板
|
|
166
|
+
if (!multiple) {
|
|
167
|
+
setOpen(false);
|
|
168
|
+
setSearchText('');
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
// currentValue 参与 nextValue 的计算(多选分支读取当前数组),
|
|
172
|
+
// 遗漏它会让连续两次选择都基于同一份过期快照而互相覆盖。
|
|
173
|
+
[controlledValue, currentValue, multiple, labelInValue, options, onChange]);
|
|
174
|
+
|
|
175
|
+
// 清除选择
|
|
176
|
+
const handleClear = useCallback(e => {
|
|
177
|
+
e.stopPropagation();
|
|
178
|
+
const nextValue = multiple ? [] : '';
|
|
179
|
+
if (controlledValue === undefined) {
|
|
180
|
+
setInternalValue(nextValue);
|
|
181
|
+
}
|
|
182
|
+
onChange?.(toOutValue(nextValue, labelInValue, options));
|
|
183
|
+
}, [controlledValue, multiple, labelInValue, options, onChange]);
|
|
184
|
+
|
|
185
|
+
// 判断是否有值
|
|
186
|
+
const hasValue = Array.isArray(currentValue) ? currentValue.length > 0 : currentValue !== '' && currentValue !== undefined;
|
|
187
|
+
|
|
188
|
+
// 渲染选中值的显示内容
|
|
189
|
+
const renderDisplay = () => {
|
|
190
|
+
if (multiple && Array.isArray(currentValue) && currentValue.length > 0) {
|
|
191
|
+
const shownTags = maxTagCount !== undefined ? currentValue.slice(0, maxTagCount) : currentValue;
|
|
192
|
+
const omittedCount = currentValue.length - shownTags.length;
|
|
193
|
+
return /*#__PURE__*/_jsxs("span", {
|
|
194
|
+
className: prefixCls('select-tags'),
|
|
195
|
+
children: [shownTags.map(val => /*#__PURE__*/_jsxs("span", {
|
|
196
|
+
className: prefixCls('select-tag'),
|
|
197
|
+
children: [getOptionLabel(options, val), /*#__PURE__*/_jsx("span", {
|
|
198
|
+
className: prefixCls('select-tag-close'),
|
|
199
|
+
onClick: e => {
|
|
200
|
+
e.stopPropagation();
|
|
201
|
+
handleSelect(val);
|
|
202
|
+
},
|
|
203
|
+
children: "\xD7"
|
|
204
|
+
})]
|
|
205
|
+
}, val)), omittedCount > 0 && /*#__PURE__*/_jsxs("span", {
|
|
206
|
+
className: prefixCls('select-tag'),
|
|
207
|
+
title: `还有 ${omittedCount} 项未显示`,
|
|
208
|
+
children: ["+", omittedCount, "..."]
|
|
209
|
+
})]
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
if (!multiple && currentValue !== '' && currentValue !== undefined && !Array.isArray(currentValue)) {
|
|
213
|
+
return /*#__PURE__*/_jsx("span", {
|
|
214
|
+
className: prefixCls('select-selection-item'),
|
|
215
|
+
children: getOptionLabel(options, currentValue)
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return /*#__PURE__*/_jsx("span", {
|
|
219
|
+
className: prefixCls('select-placeholder'),
|
|
220
|
+
children: placeholder
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
const cls = classNames(prefixCls('select'), variant !== 'default' && prefixCls(`select-${variant}`), prefixCls(`select-${size}`), open && prefixCls('select-open'), disabled && prefixCls('select-disabled'), multiple && prefixCls('select-multiple'), className);
|
|
224
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
225
|
+
ref: containerRef,
|
|
226
|
+
className: cls,
|
|
227
|
+
style: style,
|
|
228
|
+
children: [/*#__PURE__*/_jsxs("div", {
|
|
229
|
+
ref: ref,
|
|
230
|
+
className: prefixCls('select-selector'),
|
|
231
|
+
onClick: () => {
|
|
232
|
+
if (disabled) return;
|
|
233
|
+
setOpen(prev => !prev);
|
|
234
|
+
},
|
|
235
|
+
role: "combobox",
|
|
236
|
+
"aria-expanded": open,
|
|
237
|
+
"aria-haspopup": "listbox",
|
|
238
|
+
"aria-controls": open ? listId : undefined,
|
|
239
|
+
"aria-label": ariaLabel,
|
|
240
|
+
"aria-labelledby": ariaLabelledBy,
|
|
241
|
+
"aria-describedby": ariaDescribedBy,
|
|
242
|
+
"aria-invalid": ariaInvalid,
|
|
243
|
+
tabIndex: disabled ? -1 : 0,
|
|
244
|
+
onKeyDown: e => {
|
|
245
|
+
if (disabled) return;
|
|
246
|
+
const isInput = e.target instanceof HTMLInputElement;
|
|
247
|
+
if (isInput && e.key === ' ') {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!open) {
|
|
251
|
+
if (e.key === 'ArrowDown' || e.key === 'ArrowUp' || e.key === 'Enter' || e.key === ' ') {
|
|
252
|
+
e.preventDefault();
|
|
253
|
+
setOpen(true);
|
|
254
|
+
}
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (e.key === 'ArrowDown') {
|
|
258
|
+
e.preventDefault();
|
|
259
|
+
if (filteredOptions.length === 0) return;
|
|
260
|
+
let nextIndex = activeIndex;
|
|
261
|
+
let count = 0;
|
|
262
|
+
do {
|
|
263
|
+
nextIndex = (nextIndex + 1) % filteredOptions.length;
|
|
264
|
+
count++;
|
|
265
|
+
} while (filteredOptions[nextIndex].disabled && count < filteredOptions.length);
|
|
266
|
+
if (!filteredOptions[nextIndex].disabled) {
|
|
267
|
+
setActiveIndex(nextIndex);
|
|
268
|
+
}
|
|
269
|
+
} else if (e.key === 'ArrowUp') {
|
|
270
|
+
e.preventDefault();
|
|
271
|
+
if (filteredOptions.length === 0) return;
|
|
272
|
+
let prevIndex = activeIndex < 0 ? filteredOptions.length : activeIndex;
|
|
273
|
+
let count = 0;
|
|
274
|
+
do {
|
|
275
|
+
prevIndex = (prevIndex - 1 + filteredOptions.length) % filteredOptions.length;
|
|
276
|
+
count++;
|
|
277
|
+
} while (filteredOptions[prevIndex].disabled && count < filteredOptions.length);
|
|
278
|
+
if (!filteredOptions[prevIndex].disabled) {
|
|
279
|
+
setActiveIndex(prevIndex);
|
|
280
|
+
}
|
|
281
|
+
} else if (e.key === 'Enter' || e.key === ' ') {
|
|
282
|
+
e.preventDefault();
|
|
283
|
+
if (activeIndex >= 0 && activeIndex < filteredOptions.length) {
|
|
284
|
+
const activeOpt = filteredOptions[activeIndex];
|
|
285
|
+
if (!activeOpt.disabled) {
|
|
286
|
+
handleSelect(activeOpt.value);
|
|
287
|
+
}
|
|
288
|
+
} else {
|
|
289
|
+
setOpen(false);
|
|
290
|
+
setSearchText('');
|
|
291
|
+
}
|
|
292
|
+
} else if (e.key === 'Escape') {
|
|
293
|
+
e.preventDefault();
|
|
294
|
+
setOpen(false);
|
|
295
|
+
setSearchText('');
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
children: [searchable && open ? /*#__PURE__*/_jsx("input", {
|
|
299
|
+
ref: searchInputRef,
|
|
300
|
+
className: prefixCls('select-search-input'),
|
|
301
|
+
value: searchText,
|
|
302
|
+
onChange: e => {
|
|
303
|
+
setSearchText(e.target.value);
|
|
304
|
+
onSearch?.(e.target.value);
|
|
305
|
+
},
|
|
306
|
+
placeholder: multiple && Array.isArray(currentValue) && currentValue.length > 0 ? '' : placeholder,
|
|
307
|
+
onClick: e => e.stopPropagation()
|
|
308
|
+
}) : renderDisplay(), /*#__PURE__*/_jsx("span", {
|
|
309
|
+
className: prefixCls('select-arrow'),
|
|
310
|
+
children: clearable && hasValue && !disabled ? /*#__PURE__*/_jsx("span", {
|
|
311
|
+
className: prefixCls('select-clear'),
|
|
312
|
+
onClick: handleClear,
|
|
313
|
+
children: "\xD7"
|
|
314
|
+
}) : /*#__PURE__*/_jsx("span", {
|
|
315
|
+
className: prefixCls('select-arrow-icon')
|
|
316
|
+
})
|
|
317
|
+
})]
|
|
318
|
+
}), open && !disabled && /*#__PURE__*/_jsx("div", {
|
|
319
|
+
className: prefixCls('select-dropdown'),
|
|
320
|
+
role: "listbox",
|
|
321
|
+
id: listId,
|
|
322
|
+
"aria-multiselectable": multiple,
|
|
323
|
+
children: loading ? /*#__PURE__*/_jsxs("div", {
|
|
324
|
+
className: prefixCls('select-loading'),
|
|
325
|
+
children: [/*#__PURE__*/_jsx("span", {
|
|
326
|
+
className: prefixCls('select-loading-icon')
|
|
327
|
+
}), "\u52A0\u8F7D\u4E2D..."]
|
|
328
|
+
}) : filteredOptions.length === 0 ? /*#__PURE__*/_jsx("div", {
|
|
329
|
+
className: prefixCls('select-empty'),
|
|
330
|
+
children: notFoundContent ?? '无匹配选项'
|
|
331
|
+
}) : filteredOptions.map((opt, index) => {
|
|
332
|
+
const selected = isSelected(currentValue, opt.value);
|
|
333
|
+
const active = activeIndex === index;
|
|
334
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
335
|
+
className: classNames(prefixCls('select-option'), selected && prefixCls('select-option-selected'), active && prefixCls('select-option-active'), opt.disabled && prefixCls('select-option-disabled')),
|
|
336
|
+
role: "option",
|
|
337
|
+
"aria-selected": selected,
|
|
338
|
+
onClick: () => {
|
|
339
|
+
if (opt.disabled) return;
|
|
340
|
+
handleSelect(opt.value);
|
|
341
|
+
},
|
|
342
|
+
children: [multiple && /*#__PURE__*/_jsx("span", {
|
|
343
|
+
className: prefixCls('select-option-checkbox'),
|
|
344
|
+
children: selected && /*#__PURE__*/_jsx("span", {
|
|
345
|
+
className: prefixCls('select-option-check')
|
|
346
|
+
})
|
|
347
|
+
}), /*#__PURE__*/_jsx("span", {
|
|
348
|
+
className: prefixCls('select-option-label'),
|
|
349
|
+
children: opt.label
|
|
350
|
+
}), !multiple && selected && /*#__PURE__*/_jsx("span", {
|
|
351
|
+
className: prefixCls('select-option-tick')
|
|
352
|
+
})]
|
|
353
|
+
}, opt.value);
|
|
354
|
+
})
|
|
355
|
+
})]
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
Select.displayName = 'Select';
|