From 1dd9b9e3298966ecf9ebd913f23deb7e088bce4a Mon Sep 17 00:00:00 2001 From: zhouyun1 Date: Mon, 6 Nov 2023 19:33:49 +0800 Subject: [PATCH] feat(tag): add editorRender api (#2657) --- .changeset/lucky-owls-laugh.md | 5 ++ .changeset/strange-nails-sniff.md | 5 ++ .../ui/alert/stories/duration.stories.tsx | 2 +- packages/ui/tag/src/TagGroup.tsx | 40 +++++++---- .../ui/tag/stories/editor-render.stories.tsx | 67 +++++++++++++++++++ packages/ui/tag/stories/index.stories.tsx | 1 + 6 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 .changeset/lucky-owls-laugh.md create mode 100644 .changeset/strange-nails-sniff.md create mode 100644 packages/ui/tag/stories/editor-render.stories.tsx diff --git a/.changeset/lucky-owls-laugh.md b/.changeset/lucky-owls-laugh.md new file mode 100644 index 000000000..1f8cdc0d5 --- /dev/null +++ b/.changeset/lucky-owls-laugh.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/tag": minor +--- + +feat: add editorRender api diff --git a/.changeset/strange-nails-sniff.md b/.changeset/strange-nails-sniff.md new file mode 100644 index 000000000..9e480d916 --- /dev/null +++ b/.changeset/strange-nails-sniff.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/hiui": patch +--- + +feat(tag): add editorRender api diff --git a/packages/ui/alert/stories/duration.stories.tsx b/packages/ui/alert/stories/duration.stories.tsx index d05d4a649..978a3ee44 100644 --- a/packages/ui/alert/stories/duration.stories.tsx +++ b/packages/ui/alert/stories/duration.stories.tsx @@ -3,7 +3,7 @@ import Alert from '../src' /** * @title 倒计时自动关闭 - * @desc 倒计时自动关闭反馈信息在出现一定时间后自动关闭,不打扰 + * @desc 倒计时自动关闭反馈信息在出现一定时间后自动关闭,不打扰。注:最大值不能超过 2^31-1 */ export const Duration = () => { return ( diff --git a/packages/ui/tag/src/TagGroup.tsx b/packages/ui/tag/src/TagGroup.tsx index 263c1424d..be7a54419 100644 --- a/packages/ui/tag/src/TagGroup.tsx +++ b/packages/ui/tag/src/TagGroup.tsx @@ -27,6 +27,8 @@ export const TagGroup = forwardRef( onDelete, onEdit, addButton, + render, + editorRender, ...rest }, ref @@ -76,10 +78,11 @@ export const TagGroup = forwardRef( itemOnEdit && itemOnEdit(e) onEdit && onEdit(e, { ...item }, index) }} + render={render} /> ) }) - }, [data, editable, maxWidth, onDelete, onEdit, prefixCls]) + }, [data, editable, maxWidth, onDelete, onEdit, prefixCls, render]) const addNewButton = useMemo(() => { return ( @@ -99,17 +102,20 @@ export const TagGroup = forwardRef(
{displayTags} {editable && !isInAdding && addNewButton} - {isInAdding && ( - { - onAdd?.(e, data.length) - setIsInAdding(false) - }} - className={`${prefixCls}__item`} - /> - )} + {isInAdding && + (editorRender ? ( + editorRender(() => setIsInAdding(false)) + ) : ( + { + onAdd?.(e, data.length) + setIsInAdding(false) + }} + className={`${prefixCls}__item`} + /> + ))}
) } @@ -151,6 +157,16 @@ export interface TagGroupProps extends HiBaseHTMLProps<'div'> { * @private */ addButton?: React.ReactNode + /** + * 内容渲染器 + * @param children 子代对象 + * @default e => e + */ + render?: (children?: React.ReactText) => React.ReactNode + /** + * 自定义编辑器渲染 + */ + editorRender?: (updated: () => void) => React.ReactNode } export interface TagGroupDataItem diff --git a/packages/ui/tag/stories/editor-render.stories.tsx b/packages/ui/tag/stories/editor-render.stories.tsx new file mode 100644 index 000000000..bdbad75f8 --- /dev/null +++ b/packages/ui/tag/stories/editor-render.stories.tsx @@ -0,0 +1,67 @@ +import React, { useState } from 'react' +import Tag, { TagGroupDataItem } from '../src' +import Select from '@hi-ui/select' + +/** + * @title 自定义编辑器渲染 + */ +export const EditorRender = () => { + const [baseData, setBaseData] = useState([ + { + children: 'Test', + id: 1, + }, + ]) + const [tagsData] = useState([ + { id: 1, title: 'Test' }, + { id: 2, title: 'Test2' }, + { id: 3, title: 'Test3' }, + { id: 4, title: 'Test4' }, + { id: 5, title: 'Test5' }, + ]) + + const selectData = React.useMemo(() => { + return tagsData.filter((item) => !baseData.find((d) => d.id === item.id)) + }, [baseData, tagsData]) + + return ( + <> +

EditorRender

+ { + return ( +