From 3846663ebe10a2788f0c04afbab3db72b2d890ce Mon Sep 17 00:00:00 2001 From: liaoxuezhi <2698393+2betop@users.noreply.github.com> Date: Fri, 21 Jun 2024 16:04:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20words=20=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=A4=8D=E6=9D=82=E5=AF=B9=E8=B1=A1=E6=97=B6=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E9=97=AE=E9=A2=98=20(#10491)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis/src/renderers/Words.tsx | 170 ++++++++++++++------------ 1 file changed, 89 insertions(+), 81 deletions(-) diff --git a/packages/amis/src/renderers/Words.tsx b/packages/amis/src/renderers/Words.tsx index fd21e38de..587af4945 100644 --- a/packages/amis/src/renderers/Words.tsx +++ b/packages/amis/src/renderers/Words.tsx @@ -2,7 +2,15 @@ * @file Words */ import React, {Fragment} from 'react'; -import {autobind, Renderer, RendererProps, Option, getTreeAncestors, resolveVariableAndFilter} from 'amis-core'; +import { + autobind, + Renderer, + RendererProps, + Option, + getTreeAncestors, + resolveVariableAndFilter, + labelToString +} from 'amis-core'; import {BaseSchema, SchemaObject} from '../Schema'; import {Tag} from 'amis-ui'; import {TagSchema} from './Tag'; @@ -15,8 +23,8 @@ export interface WordsSchema extends BaseSchema { type: 'words'; /** - * 展示限制, 为0时也无限制 - */ + * 展示限制, 为0时也无限制 + */ limit?: number; /** @@ -40,8 +48,8 @@ export interface WordsSchema extends BaseSchema { collapseButton?: SchemaObject; /** - * tags数据 - */ + * tags数据 + */ words: Words; /** @@ -50,8 +58,8 @@ export interface WordsSchema extends BaseSchema { inTag?: boolean | TagSchema; /** - * 分割符 - */ + * 分割符 + */ delimiter?: string; } @@ -59,22 +67,26 @@ export interface WordsProps extends RendererProps, Omit {} -function getLabel(item: Option, index: number, { - type, - labelField = 'label', - options = [], - enableNodePath, - hideNodePathLabel, - pathSeparator = '/', -}: any): string { - if (enableNodePath - || (type === 'nested-select' && !hideNodePathLabel) - ){ - // 将所有祖先节点也展现出来 +function getLabel( + item: Option, + index: number, + { + type, + labelField = 'label', + options = [], + enableNodePath, + hideNodePathLabel, + pathSeparator = '/' + }: any +): string { + if (enableNodePath || (type === 'nested-select' && !hideNodePathLabel)) { + // 将所有祖先节点也展现出来 const ancestors = getTreeAncestors(options, item, true); return `${ ancestors - ? ancestors.map(item => `${item[labelField || 'label']}`).join(` ${pathSeparator} `) + ? ancestors + .map(item => `${item[labelField || 'label']}`) + .join(` ${pathSeparator} `) : item[labelField || 'label'] }`; } @@ -104,11 +116,7 @@ export class WordsField extends React.Component { } renderContent(words: Words) { - const { - delimiter, - inTag, - classnames: cx - } = this.props; + const {delimiter, inTag, classnames: cx} = this.props; // 纯文字展示 if (!Array.isArray(words)) { @@ -119,11 +127,13 @@ export class WordsField extends React.Component { if (!inTag) { const lastIndex = words.length - 1; return words.map((item, index) => { - return - {item} - {index === lastIndex ? '' : delimiter ? delimiter : ', '} - - }) + return ( + + {labelToString(item)} + {index === lastIndex ? '' : delimiter ? delimiter : ', '} + + ); + }); } return words.map((label, key) => ( @@ -132,42 +142,40 @@ export class WordsField extends React.Component { key={key} label={label} className={'mb-1'} - {...typeof inTag === 'object' ? {...inTag, className: cx(inTag.className)} : undefined} + {...(typeof inTag === 'object' + ? {...inTag, className: cx(inTag.className)} + : undefined)} /> )); } renderAll(words: Words, hasBtn = false) { - const { - collapseButtonText = '收起', - collapseButton, - render - } = this.props; + const {collapseButtonText = '收起', collapseButton, render} = this.props; return ( <> {this.renderContent(words)} - {!hasBtn ? null : - render('collapseBtn', { - type: 'button', - level: 'link', - className: 'ml-1 v-baseline' - }, { - onClick: this.toggleExpend, - ...collapseButton, - label: collapseButtonText - }) - } + {!hasBtn + ? null + : render( + 'collapseBtn', + { + type: 'button', + level: 'link', + className: 'ml-1 v-baseline' + }, + { + onClick: this.toggleExpend, + ...collapseButton, + label: collapseButtonText + } + )} ); } renderPart(words: Words) { - const { - expendButtonText = '展开', - expendButton, - render - } = this.props; + const {expendButtonText = '展开', expendButton, render} = this.props; const limit = this.getLimit(words); let partContent = Array.isArray(words) @@ -178,25 +186,25 @@ export class WordsField extends React.Component { <> {this.renderContent(partContent)}  ... - {render('collapseBtn', { - type: 'button', - level: 'link', - className: 'ml-1 v-baseline' - }, { - onClick: this.toggleExpend, - ...expendButton, - label: expendButtonText - })} + {render( + 'collapseBtn', + { + type: 'button', + level: 'link', + className: 'ml-1 v-baseline' + }, + { + onClick: this.toggleExpend, + ...expendButton, + label: expendButtonText + } + )} - ) + ); } getWords() { - const { - selectedOptions = [], - words: oldWords, - data - } = this.props; + const {selectedOptions = [], words: oldWords, data} = this.props; let words; if (typeof oldWords === 'string') { @@ -208,19 +216,16 @@ export class WordsField extends React.Component { } if (selectedOptions?.length > 0) { - return selectedOptions - .map((option: Option, index: number) => getLabel(option, index, this.props)); + return selectedOptions.map((option: Option, index: number) => + getLabel(option, index, this.props) + ); } return null; } render() { - const { - classnames: cx, - className, - style - } = this.props; + const {classnames: cx, className, style} = this.props; const words = this.getWords(); @@ -231,20 +236,23 @@ export class WordsField extends React.Component { const limit = this.getLimit(words); let body; if ( - !limit - || (Array.isArray(words) && words.length <= limit) - || (!Array.isArray(words) && words.toString().length <= limit) + !limit || + (Array.isArray(words) && words.length <= limit) || + (!Array.isArray(words) && words.toString().length <= limit) ) { // 渲染全部,且无展开收起按钮 body = this.renderAll(words); - } - else { + } else { body = this.state.isExpend ? this.renderAll(words, true) : this.renderPart(words); } - return
{body}
+ return ( +
+ {body} +
+ ); } } @@ -259,5 +267,5 @@ export class WordsRenderer extends WordsField {} export class TagsRenderer extends WordsField { static defaultProps = { inTag: true - } + }; }