From ec38ee1a62f1716106ecd4617840295c1f22ed5b Mon Sep 17 00:00:00 2001 From: yang-x20 <67466496+yang-x20@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:09:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(check-cascader):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A7=A6=E5=8F=91=E5=99=A8(#2896)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(check-cascader): 新增自定义触发器(#2896) * chore(check-cascader): 生成变更记录文件(#2896) --------- Co-authored-by: yangxuexue --- .changeset/hot-ways-knock.md | 5 + .changeset/warm-zebras-sip.md | 5 + .../ui/check-cascader/src/CheckCascader.tsx | 70 ++++++++----- .../stories/custom-render.stories.tsx | 99 +++++++++++++++++++ .../check-cascader/stories/index.stories.tsx | 1 + 5 files changed, 158 insertions(+), 22 deletions(-) create mode 100644 .changeset/hot-ways-knock.md create mode 100644 .changeset/warm-zebras-sip.md create mode 100644 packages/ui/check-cascader/stories/custom-render.stories.tsx diff --git a/.changeset/hot-ways-knock.md b/.changeset/hot-ways-knock.md new file mode 100644 index 000000000..5f5f1d9af --- /dev/null +++ b/.changeset/hot-ways-knock.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/hiui": patch +--- + +feat(check-cascader): 新增自定义触发器 diff --git a/.changeset/warm-zebras-sip.md b/.changeset/warm-zebras-sip.md new file mode 100644 index 000000000..e36f650c4 --- /dev/null +++ b/.changeset/warm-zebras-sip.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/check-cascader": minor +--- + +feat: 新增自定义触发器 diff --git a/packages/ui/check-cascader/src/CheckCascader.tsx b/packages/ui/check-cascader/src/CheckCascader.tsx index 2e14b97fe..d90cf7e08 100644 --- a/packages/ui/check-cascader/src/CheckCascader.tsx +++ b/packages/ui/check-cascader/src/CheckCascader.tsx @@ -24,7 +24,12 @@ import { flattenTreeData } from './utils' import { getNodeAncestorsWithMe, getTopDownAncestors } from '@hi-ui/tree-utils' import { useLatestCallback } from '@hi-ui/use-latest' import { isArrayNonEmpty, isFunction, isUndef } from '@hi-ui/type-assertion' -import { HiBaseAppearanceEnum, HiBaseFieldNames, HiBaseSizeEnum, useLocaleContext } from '@hi-ui/core' +import { + HiBaseAppearanceEnum, + HiBaseFieldNames, + HiBaseSizeEnum, + useLocaleContext, +} from '@hi-ui/core' import { callAllFuncs } from '@hi-ui/func-utils' @@ -74,6 +79,7 @@ export const CheckCascader = forwardRef { + return value.map((selectedId) => { + return flattedData.find((item) => item.id === selectedId) + }) + }, [value, flattedData]) + return ( : , suffix]} - focused={menuVisible} - appearance={appearance} - value={value} - // @ts-ignore - onChange={proxyOnChange} - data={flattedData} - invalid={invalid} - // onExpand={() => { - // // setViewSelected(true) - // menuVisibleAction.on() - // }} - /> + customRender ? ( + typeof customRender === 'function' ? ( + customRender(selectedItems) + ) : ( + customRender + ) + ) : ( + : , suffix]} + focused={menuVisible} + appearance={appearance} + value={value} + // @ts-ignore + onChange={proxyOnChange} + data={flattedData} + invalid={invalid} + // onExpand={() => { + // // setViewSelected(true) + // menuVisibleAction.on() + // }} + /> + ) } > {isArrayNonEmpty(selectProps.data) ? ( @@ -406,6 +426,12 @@ export interface CheckCascaderProps extends Omit React.ReactNode + /** + * 自定义触发器 + */ + customRender?: + | React.ReactNode + | ((selectItems: (FlattedCheckCascaderDataItem | undefined)[]) => React.ReactNode) } if (__DEV__) { diff --git a/packages/ui/check-cascader/stories/custom-render.stories.tsx b/packages/ui/check-cascader/stories/custom-render.stories.tsx new file mode 100644 index 000000000..8d95ea295 --- /dev/null +++ b/packages/ui/check-cascader/stories/custom-render.stories.tsx @@ -0,0 +1,99 @@ +import React from 'react' +import CheckCascader from '../src' +import Input from '@hi-ui/input' + +/** + * @title 自定义触发器 + */ +export const CusotmRender = () => { + const [dataOnlyLeafCheckable] = React.useState(() => { + const data = [ + { + id: '手机', + title: '手机t', + children: [ + { + id: '小米', + title: '小米t', + children: [ + { + id: '小米3', + title: '小米3t', + }, + { + id: '小米4', + title: '小米4t', + }, + ], + }, + { + id: '红米', + title: '红米t', + children: [ + { + id: '红米3', + title: '红米3t', + }, + { + id: '红米4', + title: '红米4t', + }, + ], + }, + ], + }, + { + id: '电视', + title: '电视t', + children: [ + { + id: '小米电视4A', + title: '小米电视4At', + }, + { + id: '小米电视4C', + title: '小米电视4Ct', + }, + ], + }, + ] + + const getDataOnlyLeafCheckable = (data: any) => { + return data.map((item: any) => { + if (item.children) { + item.checkable = item.checkable ?? false + item.children = getDataOnlyLeafCheckable(item.children) + } + + return item + }) + } + + const dataOnlyLeafCheckable = getDataOnlyLeafCheckable(data) + + return dataOnlyLeafCheckable + }) + + return ( + <> +

CusotmRender

+
+ { + let value = [] + if (data) { + value = data.map((item) => item.title) + } + return + }} + > +
+ + ) +} diff --git a/packages/ui/check-cascader/stories/index.stories.tsx b/packages/ui/check-cascader/stories/index.stories.tsx index 4a3f79271..3f1204bff 100644 --- a/packages/ui/check-cascader/stories/index.stories.tsx +++ b/packages/ui/check-cascader/stories/index.stories.tsx @@ -13,6 +13,7 @@ export * from './display-render.stories' export * from './footer.stories' export * from './dropdown-column-render.stories' export * from './addon.stories' +export * from './custom-render.stories' // export * from './flatted.stories' export default {