diff --git a/src/components/UserTransferPicker.tsx b/src/components/UserTransferPicker.tsx index d115908..0d3a975 100644 --- a/src/components/UserTransferPicker.tsx +++ b/src/components/UserTransferPicker.tsx @@ -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; 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): string[] { return Array.from(event.currentTarget.selectedOptions).map((option) => option.value); } @@ -198,17 +242,30 @@ export class UserTransferPicker extends Component [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 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 ( <>
{displayText} {clearable && labels.length > 0 && !disabled && !readOnly ? ( diff --git a/src/shell/amis-theme.css b/src/shell/amis-theme.css index 1245038..5899509 100644 --- a/src/shell/amis-theme.css +++ b/src/shell/amis-theme.css @@ -457,7 +457,9 @@ .eip4-user-transfer-control { width: 100%; box-sizing: border-box; + height: 32px; min-height: 32px; + max-height: 32px; border: 1px solid #d7dde8; border-radius: 6px; background: #fff; @@ -468,6 +470,7 @@ cursor: pointer; max-width: 100%; overflow: hidden; + white-space: nowrap; } .eip4-user-transfer-control.is-empty {