fix(web): normalize legacy user pickers to amis picker

This commit is contained in:
2026-06-17 23:48:39 +08:00
parent 492dad7383
commit b0537f87ae
4 changed files with 218 additions and 113 deletions

View File

@@ -1,4 +1,5 @@
import { Component, type ChangeEvent, type CSSProperties, type MouseEvent } from 'react';
import { createPortal } from 'react-dom';
import { http } from '../api/http';
const ALL_VALUE = 'ALL ';
@@ -273,17 +274,20 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
private async loadBootstrap() {
const resp = await http.get('/api/user-select/bootstrap', { params: { mode: this.mode() } });
const data = getBootstrap(resp);
this.setState((current) => ({
bootstrap: data,
selectedGroup: {
...current.selectedGroup,
depart: current.selectedGroup.depart || '',
positionType: current.selectedGroup.positionType
|| (data.positionTypes[0] ? `positionType:${data.positionTypes[0].value}` : ''),
category: current.selectedGroup.category
|| (data.categories[0] ? `category:${data.categories[0].value}` : ''),
},
}));
await new Promise<void>((resolve) => {
this.setState((current) => ({
bootstrap: data,
selectedGroup: {
...current.selectedGroup,
depart: current.selectedGroup.depart
|| (data.departGroups[0] ? String(data.departGroups[0].value) : ''),
positionType: current.selectedGroup.positionType
|| (data.positionTypes[0] ? `positionType:${data.positionTypes[0].value}` : ''),
category: current.selectedGroup.category
|| (data.categories[0] ? `category:${data.categories[0].value}` : ''),
},
}), resolve);
});
}
private objectPaneOptions(pane: PaneKey): Option[] {
@@ -345,13 +349,14 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
private openDialog = async () => {
if (this.props.disabled || this.props.readOnly) return;
const isMultiple = this.props.multiple === true;
this.setState({
open: true,
draft: this.state.labels,
availableSearch: '',
selectedSearch: '',
groupSearch: '',
leftPicked: [],
leftPicked: isMultiple ? [] : this.state.labels.slice(0, 1).map((item) => optionKey(item.value)),
rightPicked: [],
}, async () => {
await this.loadBootstrap();
@@ -457,9 +462,11 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
open,
} = this.state;
const isMultiple = this.props.multiple === true;
const currentGroups = this.currentGroups();
const filteredGroups = filterOptions(currentGroups, groupSearch);
const availableWithoutSelected = this.availableWithoutSelected();
const availableOptions = isMultiple ? availableWithoutSelected : this.state.available;
const selectedFiltered = filterOptions(draft, selectedSearch);
const displayText = displayTextFor(labels, placeholder);
const fullTitle = labels.map((item) => item.label).join(', ');
@@ -469,93 +476,84 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
disabled || readOnly ? 'is-disabled' : '',
labels.length ? '' : 'is-empty',
].filter(Boolean).join(' ');
return (
<>
<div
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 ? (
<button type="button" onClick={this.clearValue} aria-label="清空">
×
</button>
) : null}
<span className="eip4-user-transfer-icon" aria-hidden="true" />
</div>
{open ? (
<div className="eip4-user-transfer-mask" onMouseDown={(event) => {
if (event.target === event.currentTarget) this.closeDialog();
}}
>
<div className="eip4-user-transfer-dialog">
<div className="eip4-user-transfer-head">
<h3>用户选择框</h3>
<button type="button" onClick={this.closeDialog}>×</button>
const dialog = open ? (
<div className="eip4-user-transfer-mask" onMouseDown={(event) => {
if (event.target === event.currentTarget) this.closeDialog();
}}
>
<div className={`eip4-user-transfer-dialog${isMultiple ? '' : ' is-single'}`}>
<div className="eip4-user-transfer-head">
<h3>{isMultiple ? '用户选择框' : '选择用户'}</h3>
<button type="button" onClick={this.closeDialog}>×</button>
</div>
<div className={`eip4-user-transfer-body${isMultiple ? '' : ' is-single'}`}>
<section className="eip4-user-transfer-panel">
<div className="eip4-user-transfer-title">-可选项-</div>
<div className="eip4-user-transfer-tabs">
{this.panes().map((pane) => (
<button
type="button"
key={pane.key}
className={activePane === pane.key ? 'is-active' : ''}
onClick={() => this.changePane(pane.key)}
>
{pane.label}
</button>
))}
</div>
<div className="eip4-user-transfer-body">
<section className="eip4-user-transfer-panel">
<div className="eip4-user-transfer-title">-可选项-</div>
<div className="eip4-user-transfer-tabs">
{this.panes().map((pane) => (
<button
type="button"
key={pane.key}
className={activePane === pane.key ? 'is-active' : ''}
onClick={() => this.changePane(pane.key)}
>
{pane.label}
</button>
))}
</div>
{currentGroups.length > 0 ? (
<div className="eip4-user-transfer-group">
<input
value={groupSearch}
onChange={(event) => this.setState({ groupSearch: event.target.value })}
placeholder={activePane === 'depart' ? '搜索公司或部门' : '搜索分类'}
/>
<select
value={selectedGroup[activePane] || ''}
onChange={(event) => this.setGroupValue(event.target.value)}
>
<option value="">
{activePane === 'depart' ? '下拉选择一个公司或部门' : '下拉选择一个分类'}
</option>
{filteredGroups.map((item) => (
<option key={item.value} value={item.value}>{item.label}</option>
))}
</select>
</div>
) : null}
{currentGroups.length > 0 ? (
<div className="eip4-user-transfer-group">
<input
className="eip4-user-transfer-search"
value={availableSearch}
onChange={(event) => this.setAvailableSearch(event.target.value)}
placeholder="输入关键字过滤可选项"
value={groupSearch}
onChange={(event) => this.setState({ groupSearch: event.target.value })}
placeholder={activePane === 'depart' ? '搜索公司或部门' : '搜索分类'}
/>
<select
multiple
className="eip4-user-transfer-list"
value={leftPicked}
onChange={(event) => this.setState({ leftPicked: selectedValues(event) })}
onDoubleClick={this.addPicked}
value={selectedGroup[activePane] || ''}
onChange={(event) => this.setGroupValue(event.target.value)}
>
{availableWithoutSelected.map((item) => (
<option key={optionKey(item.value)} value={optionKey(item.value)}>{item.label}</option>
<option value="">
{activePane === 'depart' ? '下拉选择一个公司或部门' : '下拉选择一个分类'}
</option>
{filteredGroups.map((item) => (
<option key={item.value} value={item.value}>{item.label}</option>
))}
</select>
<div className="eip4-user-transfer-count">
{loading ? '加载中...' : `共${availableWithoutSelected.length}条`}
</div>
</section>
</div>
) : null}
<input
className="eip4-user-transfer-search"
value={availableSearch}
onChange={(event) => this.setAvailableSearch(event.target.value)}
placeholder="输入关键字过滤可选项"
/>
<select
multiple={isMultiple}
size={isMultiple ? undefined : 12}
className="eip4-user-transfer-list"
value={isMultiple ? leftPicked : (leftPicked[0] ?? '')}
onChange={(event) => {
const values = selectedValues(event);
if (isMultiple) {
this.setState({ leftPicked: values });
return;
}
const picked = this.state.available.find((item) => values.includes(optionKey(item.value)));
this.setState({ leftPicked: values, draft: picked ? [picked] : [] });
}}
onDoubleClick={this.addPicked}
>
{availableOptions.map((item) => (
<option key={optionKey(item.value)} value={optionKey(item.value)}>{item.label}</option>
))}
</select>
<div className="eip4-user-transfer-count">
{loading ? '加载中...' : `共${availableOptions.length}条`}
</div>
</section>
{isMultiple ? (
<>
<div className="eip4-user-transfer-actions">
<button type="button" onClick={this.addPicked} disabled={leftPicked.length === 0}>&gt;</button>
<button type="button" onClick={this.removePicked} disabled={rightPicked.length === 0}>&lt;</button>
@@ -584,14 +582,37 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
</select>
<div className="eip4-user-transfer-count">共{draft.length}条</div>
</section>
</div>
<div className="eip4-user-transfer-foot">
<button type="button" className="primary" onClick={this.commit}>确认</button>
<button type="button" onClick={this.closeDialog}>取消</button>
</div>
</div>
</>
) : null}
</div>
) : null}
<div className="eip4-user-transfer-foot">
<button type="button" className="primary" onClick={this.commit}>确认</button>
<button type="button" onClick={this.closeDialog}>取消</button>
</div>
</div>
</div>
) : null;
return (
<>
<div
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 ? (
<button type="button" onClick={this.clearValue} aria-label="清空">
×
</button>
) : null}
<span className="eip4-user-transfer-icon" aria-hidden="true" />
</div>
{dialog && typeof document !== 'undefined' ? createPortal(dialog, document.body) : dialog}
</>
);
}