Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions apps/console/src/features/catalog/remote-resource-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface ResourceOption {
name: string;
disabled?: boolean;
detail?: string;
/** 名称后追加的状态节点(如「已删除」Tag);下拉项与选中态同样渲染。 */
tag?: ReactNode;
}

interface RemoteResourceQuery {
Expand All @@ -30,6 +32,10 @@ type RemoteResourcePickerProps = {
triggerRef?: Ref<{ focus: () => void }>;
showId?: boolean;
emptyText?: string;
/** 展开时是否重拉远端候选(数据与页面主内容同源时关闭,避免展开触发整页刷新)。 */
refreshOnOpen?: boolean;
/** 隐藏 Form.Item 标签(Select 保留 aria-label):内嵌工具栏行时由外部提供行内标签。 */
hideLabel?: boolean;
} & (
| {
multiple?: false;
Expand Down Expand Up @@ -84,7 +90,10 @@ function optionDetail(source: ResourceOption | undefined, showId: boolean): Reac
if (!source) return null;
return (
<div className={styles.optionDetail}>
<div className={styles.optionName}>{source.name}</div>
<div className={styles.optionName}>
{source.name}
{source.tag}
</div>
{showId && source.id ? <div className={styles.optionId}>{source.id}</div> : null}
{source.detail ? <div className={styles.optionDetailText}>{source.detail}</div> : null}
</div>
Expand All @@ -101,7 +110,19 @@ function sourceOf(option: DefaultOptionType | undefined, byId: Map<string, Resou

/** 远端资源共用的表单下拉:展示全部候选,键盘与搜索交互保持一致。 */
export function RemoteResourcePicker(props: RemoteResourcePickerProps) {
const { remote, label, resource, placeholder, required, error, disabled, triggerRef, showId = true } = props;
const {
remote,
label,
resource,
placeholder,
required,
error,
disabled,
triggerRef,
showId = true,
refreshOnOpen = true,
hideLabel = false,
} = props;
const selectRef = useRef<ComponentRef<typeof Select>>(null);
useImperativeHandle(triggerRef, () => ({ focus: () => selectRef.current?.focus() }), []);
const [search, setSearch] = useState("");
Expand All @@ -116,29 +137,38 @@ export function RemoteResourcePicker(props: RemoteResourcePickerProps) {
const optionById = new Map(all.map((option) => [option.id, option]));
const options: DefaultOptionType[] = all.map((option) => ({
value: option.id,
label: option.name,
label: option.tag ? (
<span>
{option.name}
{option.tag}
</span>
) : (
option.name
),
disabled: props.multiple ? !selectedIds.has(option.id) && (option.disabled || atLimit) : option.disabled,
}));
const changeOpen = (next: boolean) => {
if (disabled) return;
if (next) {
setSearch("");
void remote.refresh();
if (refreshOnOpen) void remote.refresh();
}
};

return (
<Form component={false} layout="vertical" requiredMark={false}>
<Form.Item
label={
<>
{label}
{required ? (
<span aria-hidden="true" className={styles.requiredStar}>
*
</span>
) : null}
</>
hideLabel ? undefined : (
<>
{label}
{required ? (
<span aria-hidden="true" className={styles.requiredStar}>
*
</span>
) : null}
</>
)
}
validateStatus={error ? "error" : undefined}
help={error}
Expand Down
Loading