fix(web): constrain user picker bulk display
This commit is contained in:
@@ -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';
|
import { http } from '../api/http';
|
||||||
|
|
||||||
const ALL_VALUE = 'ALL ';
|
const ALL_VALUE = 'ALL ';
|
||||||
@@ -38,6 +38,8 @@ type UserTransferPickerProps = {
|
|||||||
data?: Record<string, unknown>;
|
data?: Record<string, unknown>;
|
||||||
name?: string;
|
name?: string;
|
||||||
label?: string | false;
|
label?: string | false;
|
||||||
|
className?: string;
|
||||||
|
style?: CSSProperties;
|
||||||
onChange?: (value: string | string[] | undefined) => void;
|
onChange?: (value: string | string[] | undefined) => void;
|
||||||
multiple?: boolean;
|
multiple?: boolean;
|
||||||
delimiter?: string;
|
delimiter?: string;
|
||||||
@@ -133,6 +135,48 @@ function filterOptions(items: Option[], keyword: string): Option[] {
|
|||||||
return items.filter((item) => `${item.label} ${item.value}`.toLowerCase().includes(q));
|
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[] {
|
function selectedValues(event: ChangeEvent<HTMLSelectElement>): string[] {
|
||||||
return Array.from(event.currentTarget.selectedOptions).map((option) => option.value);
|
return Array.from(event.currentTarget.selectedOptions).map((option) => option.value);
|
||||||
}
|
}
|
||||||
@@ -198,17 +242,30 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
|
|||||||
this.setState({ labels: [] });
|
this.setState({ labels: [] });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const existing = new Map(this.state.labels.map((item) => [optionKey(item.value), item]));
|
||||||
try {
|
try {
|
||||||
|
const resolvedParts: Option[] = [];
|
||||||
|
for (const chunk of chunksByLength(ids, this.delimiter())) {
|
||||||
const resp = await http.get('/api/user-select/resolve', {
|
const resp = await http.get('/api/user-select/resolve', {
|
||||||
params: { mode: this.mode(), ids: ids.join(this.delimiter()) },
|
params: { mode: this.mode(), ids: chunk.join(this.delimiter()) },
|
||||||
});
|
});
|
||||||
const resolved = dedupeOptions(getOptions(resp));
|
resolvedParts.push(...getOptions(resp));
|
||||||
|
}
|
||||||
|
const resolved = dedupeOptions(resolvedParts);
|
||||||
const byValue = new Map(resolved.map((item) => [optionKey(item.value), item]));
|
const byValue = new Map(resolved.map((item) => [optionKey(item.value), item]));
|
||||||
this.setState({
|
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 {
|
} 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() {
|
override render() {
|
||||||
const {
|
const {
|
||||||
|
className,
|
||||||
disabled,
|
disabled,
|
||||||
readOnly,
|
readOnly,
|
||||||
placeholder = '请选择',
|
placeholder = '请选择',
|
||||||
clearable = true,
|
clearable = true,
|
||||||
|
style,
|
||||||
} = this.props;
|
} = this.props;
|
||||||
const {
|
const {
|
||||||
activePane,
|
activePane,
|
||||||
@@ -401,15 +460,24 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
|
|||||||
const filteredGroups = filterOptions(currentGroups, groupSearch);
|
const filteredGroups = filterOptions(currentGroups, groupSearch);
|
||||||
const availableWithoutSelected = this.availableWithoutSelected();
|
const availableWithoutSelected = this.availableWithoutSelected();
|
||||||
const selectedFiltered = filterOptions(draft, selectedSearch);
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<div
|
<div
|
||||||
className={`eip4-user-transfer-control${disabled || readOnly ? ' is-disabled' : ''}${labels.length ? '' : ' is-empty'}`}
|
className={controlClassName}
|
||||||
onClick={this.openDialog}
|
onClick={this.openDialog}
|
||||||
role="button"
|
role="button"
|
||||||
|
style={style}
|
||||||
tabIndex={disabled || readOnly ? -1 : 0}
|
tabIndex={disabled || readOnly ? -1 : 0}
|
||||||
|
title={fullTitle || undefined}
|
||||||
>
|
>
|
||||||
<span className="eip4-user-transfer-text">{displayText}</span>
|
<span className="eip4-user-transfer-text">{displayText}</span>
|
||||||
{clearable && labels.length > 0 && !disabled && !readOnly ? (
|
{clearable && labels.length > 0 && !disabled && !readOnly ? (
|
||||||
|
|||||||
@@ -457,7 +457,9 @@
|
|||||||
.eip4-user-transfer-control {
|
.eip4-user-transfer-control {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
|
height: 32px;
|
||||||
min-height: 32px;
|
min-height: 32px;
|
||||||
|
max-height: 32px;
|
||||||
border: 1px solid #d7dde8;
|
border: 1px solid #d7dde8;
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
@@ -468,6 +470,7 @@
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
.eip4-user-transfer-control.is-empty {
|
.eip4-user-transfer-control.is-empty {
|
||||||
|
|||||||
Reference in New Issue
Block a user