Files
monkey-ai-web/src/components/creation/params/ParamSelect.jsx
T
qiubaishen cfb9f85c90 refactor(ui): 将合并引入的 Semi 组件迁移到 Ant Design 适配层
- 创作台、WeyAi 设置、OrgResolver 共 23 个文件改从 @/components/ui 引入
- Popover 改用 open/trigger='click'/onOpenChange,移除 .semi-portal 判定
- Dropdown.Title / Item active 改为 menu group / selectedKeys
- Slider tooltip.formatter、Checkbox.Group、TagInput maxCount、DatePicker 取 Date 参数
- --semi-color-* 改为 --ui-color-*,遮罩色用 antd 默认,.semi-* 选择器改为 .ant-*
- docs/UI_MIGRATION.md 记录组件映射、视觉差异与风险
2026-11-10 12:48:02 +08:00

108 lines
3.5 KiB
React

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useMemo } from 'react';
import { Select, Tooltip } from '@/components/ui';
import { useTranslation } from 'react-i18next';
import { optionKey, optionValue } from './constraints';
import { optionLabel, paramLabel } from './labels';
import './creation-params.css';
/**
* 下拉选择(schema type = 'select'),也用于运镜预设:
* options 可为原始值,或 { value, label, description } 对象(description 作为说明文字显示)。
* 被约束禁用的选项置灰,并以说明行展示原因(下拉内不依赖 Tooltip,键盘用户同样可见)。
*
* props: { param, value, onChange(value), optionDisabledReasons?, disabledReason? }
*/
const ParamSelect = ({
param,
value,
onChange,
optionDisabledReasons = {},
disabledReason,
}) => {
const { t } = useTranslation();
const options = useMemo(() => param?.options || [], [param]);
const optionList = useMemo(
() =>
options.map((opt) => {
const reason = optionDisabledReasons[optionKey(opt)] || '';
const desc =
opt && typeof opt === 'object' && opt.description
? t(opt.description)
: '';
const text = optionLabel(t, param, opt);
return {
value: optionValue(opt),
disabled: Boolean(reason),
showTick: true,
label: (
<span className='cp-select-option'>
<span className='cp-select-option-label'>{text}</span>
{reason || desc ? (
<span className='cp-select-option-desc'>{reason || desc}</span>
) : null}
</span>
),
};
}),
[options, optionDisabledReasons, param, t],
);
// 选项可能是数字,取值可能是字符串:映射到选项的规范值,保证 Select 能命中
const hit = options.find((o) => optionKey(o) === optionKey(value));
const current = hit !== undefined ? optionValue(hit) : undefined;
const sel = (
<Select
className='cp-select'
size='small'
value={current}
optionList={optionList}
disabled={Boolean(disabledReason)}
onChange={(v) => onChange && onChange(v)}
aria-label={paramLabel(t, param)}
placeholder={t('请选择')}
dropdownClassName='cp-select-dropdown'
renderSelectedItem={(item) =>
item && item.value !== undefined
? optionLabel(
t,
param,
options.find((o) => optionKey(o) === optionKey(item.value)) ??
item.value,
)
: ''
}
/>
);
return disabledReason ? (
<Tooltip content={disabledReason} position='top'>
<span className='cp-inline-trigger cp-inline-trigger--block'>{sel}</span>
</Tooltip>
) : (
sel
);
};
export default ParamSelect;