fix(web): constrain user picker bulk display

This commit is contained in:
2026-06-17 22:52:34 +08:00
parent 9e5ece3640
commit 045d522ea0
2 changed files with 80 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { Component, type ChangeEvent, type MouseEvent } from 'react';
import { Component, type ChangeEvent, type CSSProperties, type MouseEvent } from 'react';
import { http } from '../api/http';
const ALL_VALUE = 'ALL ';
@@ -38,6 +38,8 @@ type UserTransferPickerProps = {
data?: Record<string, unknown>;
name?: string;
label?: string | false;
className?: string;
style?: CSSProperties;
onChange?: (value: string | string[] | undefined) => void;
multiple?: boolean;
delimiter?: string;
@@ -133,6 +135,48 @@ function filterOptions(items: Option[], keyword: string): Option[] {
return items.filter((item) => `${item.label} ${item.value}`.toLowerCase().includes(q));
}
function looksLikeRawId(value: string): boolean {
const text = value.trim();
return /^[0-9a-f]{32}$/i.test(text)
|| /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(text)
|| (text.length >= 24 && /^[0-9a-f-]+$/i.test(text));
}
function fallbackLabel(id: string, index: number): string {
const text = id.trim();
return looksLikeRawId(text) ? `已选人员${index + 1}` : text;
}
function chunksByLength(ids: string[], delimiter: string, maxLength = 1400): string[][] {
const chunks: string[][] = [];
let current: string[] = [];
let currentLength = 0;
for (const id of ids) {
const nextLength = currentLength + id.length + (current.length ? delimiter.length : 0);
if (current.length > 0 && nextLength > maxLength) {
chunks.push(current);
current = [];
currentLength = 0;
}
current.push(id);
currentLength += id.length + (current.length > 1 ? delimiter.length : 0);
}
if (current.length > 0) chunks.push(current);
return chunks;
}
function displayTextFor(items: Option[], placeholder: string): string {
if (items.length === 0) return placeholder;
const labels = items.map((item) => item.label.trim()).filter(Boolean);
if (labels.length === 0) return placeholder;
if (labels.every(looksLikeRawId)) {
return labels.length === 1 ? '已选择 1 人' : `已选择 ${labels.length} 人`;
}
const previewSize = 6;
const preview = labels.slice(0, previewSize).join(', ');
return labels.length > previewSize ? `${preview} 等 ${labels.length} 人` : preview;
}
function selectedValues(event: ChangeEvent<HTMLSelectElement>): string[] {
return Array.from(event.currentTarget.selectedOptions).map((option) => option.value);
}
@@ -198,17 +242,30 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
this.setState({ labels: [] });
return;
}
const existing = new Map(this.state.labels.map((item) => [optionKey(item.value), item]));
try {
const resp = await http.get('/api/user-select/resolve', {
params: { mode: this.mode(), ids: ids.join(this.delimiter()) },
});
const resolved = dedupeOptions(getOptions(resp));
const resolvedParts: Option[] = [];
for (const chunk of chunksByLength(ids, this.delimiter())) {
const resp = await http.get('/api/user-select/resolve', {
params: { mode: this.mode(), ids: chunk.join(this.delimiter()) },
});
resolvedParts.push(...getOptions(resp));
}
const resolved = dedupeOptions(resolvedParts);
const byValue = new Map(resolved.map((item) => [optionKey(item.value), item]));
this.setState({
labels: ids.map((id) => byValue.get(optionKey(id)) ?? { label: id.trim(), value: id }),
labels: ids.map((id, index) => (
byValue.get(optionKey(id))
?? existing.get(optionKey(id))
?? { label: fallbackLabel(id, index), value: id }
)),
});
} catch {
this.setState({ labels: ids.map((id) => ({ label: id.trim(), value: id })) });
this.setState({
labels: ids.map((id, index) => (
existing.get(optionKey(id)) ?? { label: fallbackLabel(id, index), value: id }
)),
});
}
}
@@ -378,10 +435,12 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
override render() {
const {
className,
disabled,
readOnly,
placeholder = '请选择',
clearable = true,
style,
} = this.props;
const {
activePane,
@@ -401,15 +460,24 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
const filteredGroups = filterOptions(currentGroups, groupSearch);
const availableWithoutSelected = this.availableWithoutSelected();
const selectedFiltered = filterOptions(draft, selectedSearch);
const displayText = labels.length > 0 ? labels.map((item) => item.label).join(', ') : placeholder;
const displayText = displayTextFor(labels, placeholder);
const fullTitle = labels.map((item) => item.label).join(', ');
const controlClassName = [
className,
'eip4-user-transfer-control',
disabled || readOnly ? 'is-disabled' : '',
labels.length ? '' : 'is-empty',
].filter(Boolean).join(' ');
return (
<>
<div
className={`eip4-user-transfer-control${disabled || readOnly ? ' is-disabled' : ''}${labels.length ? '' : ' is-empty'}`}
className={controlClassName}
onClick={this.openDialog}
role="button"
style={style}
tabIndex={disabled || readOnly ? -1 : 0}
title={fullTitle || undefined}
>
<span className="eip4-user-transfer-text">{displayText}</span>
{clearable && labels.length > 0 && !disabled && !readOnly ? (