fix(web): preserve user transfer form labels

This commit is contained in:
2026-06-17 22:46:43 +08:00
parent ba41a76a70
commit 9e5ece3640
4 changed files with 131 additions and 12 deletions

View File

@@ -11,6 +11,8 @@ type Option = {
groupId?: string;
};
type PickerValue = string | string[] | Option | Option[] | null | undefined;
type BootstrapData = {
departGroups: Option[];
positionTypes: Option[];
@@ -31,7 +33,11 @@ type PaneKey =
| 'positionTypes';
type UserTransferPickerProps = {
value?: string | string[] | Option | Option[];
value?: PickerValue;
defaultValue?: PickerValue;
data?: Record<string, unknown>;
name?: string;
label?: string | false;
onChange?: (value: string | string[] | undefined) => void;
multiple?: boolean;
delimiter?: string;
@@ -76,7 +82,7 @@ function optionKey(value: string): string {
return value.trim().toUpperCase() === 'ALL' ? ALL_VALUE : value;
}
function normalizeValue(value: UserTransferPickerProps['value'], delimiter: string): string[] {
function normalizeValue(value: PickerValue, delimiter: string): string[] {
if (Array.isArray(value)) {
return value
.map((item) => (typeof item === 'string' ? item : item?.value))
@@ -154,8 +160,8 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
override componentDidUpdate(prevProps: UserTransferPickerProps) {
const delimiter = this.delimiter();
const before = normalizeValue(prevProps.value, delimiter).join('\u0000');
const after = normalizeValue(this.props.value, delimiter).join('\u0000');
const before = normalizeValue(this.effectiveValue(prevProps), delimiter).join('\u0000');
const after = normalizeValue(this.effectiveValue(this.props), delimiter).join('\u0000');
if (before !== after || prevProps.mode !== this.props.mode) {
void this.resolveCurrentValue();
}
@@ -169,8 +175,17 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
return this.props.mode ?? 'controlled';
}
private effectiveValue(props: UserTransferPickerProps = this.props): PickerValue {
if (props.value !== undefined) return props.value;
if (props.defaultValue !== undefined) return props.defaultValue;
if (props.name && props.data && Object.prototype.hasOwnProperty.call(props.data, props.name)) {
return props.data[props.name] as PickerValue;
}
return undefined;
}
private valueIds() {
return normalizeValue(this.props.value, this.delimiter());
return normalizeValue(this.effectiveValue(), this.delimiter());
}
private panes() {
@@ -396,12 +411,13 @@ export class UserTransferPicker extends Component<UserTransferPickerProps, UserT
role="button"
tabIndex={disabled || readOnly ? -1 : 0}
>
<span>{displayText}</span>
<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 ? (