Skip to content

Commit

Permalink
feat(check-cascader): 新增自定义触发器(#2896)
Browse files Browse the repository at this point in the history
* feat(check-cascader): 新增自定义触发器(#2896)

* chore(check-cascader): 生成变更记录文件(#2896)

---------

Co-authored-by: yangxuexue <[email protected]>
  • Loading branch information
yang-x20 and yangxuexue authored Jun 28, 2024
1 parent 280dfc9 commit ec38ee1
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-ways-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(check-cascader): 新增自定义触发器
5 changes: 5 additions & 0 deletions .changeset/warm-zebras-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/check-cascader": minor
---

feat: 新增自定义触发器
70 changes: 48 additions & 22 deletions packages/ui/check-cascader/src/CheckCascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -74,6 +79,7 @@ export const CheckCascader = forwardRef<HTMLDivElement | null, CheckCascaderProp
suffix,
renderExtraFooter,
dropdownColumnRender,
customRender,
fieldNames,
...rest
},
Expand Down Expand Up @@ -213,6 +219,12 @@ export const CheckCascader = forwardRef<HTMLDivElement | null, CheckCascaderProp

const cls = cx(prefixCls, className, `${prefixCls}--${menuVisible ? 'open' : 'closed'}`)

const selectedItems = useMemo(() => {
return value.map((selectedId) => {
return flattedData.find((item) => item.id === selectedId)
})
}, [value, flattedData])

return (
<Picker
ref={ref}
Expand All @@ -233,27 +245,35 @@ export const CheckCascader = forwardRef<HTMLDivElement | null, CheckCascaderProp
footer={isFunction(renderExtraFooter) && renderExtraFooter()}
onSearch={callAllFuncs(onSearchProp, onSearch)}
trigger={
<TagInputMock
{...tagInputProps}
size={size}
clearable={clearable}
placeholder={placeholder}
// @ts-ignore
displayRender={displayRender}
prefix={prefix}
suffix={[menuVisible ? <UpOutlined /> : <DownOutlined />, 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
)
) : (
<TagInputMock
{...tagInputProps}
size={size}
clearable={clearable}
placeholder={placeholder}
// @ts-ignore
displayRender={displayRender}
prefix={prefix}
suffix={[menuVisible ? <UpOutlined /> : <DownOutlined />, suffix]}
focused={menuVisible}
appearance={appearance}
value={value}
// @ts-ignore
onChange={proxyOnChange}
data={flattedData}
invalid={invalid}
// onExpand={() => {
// // setViewSelected(true)
// menuVisibleAction.on()
// }}
/>
)
}
>
{isArrayNonEmpty(selectProps.data) ? (
Expand Down Expand Up @@ -406,6 +426,12 @@ export interface CheckCascaderProps extends Omit<PickerProps, 'trigger' | 'scrol
* 自定义下拉菜单每列渲染
*/
dropdownColumnRender?: (menu: React.ReactElement, level: number) => React.ReactNode
/**
* 自定义触发器
*/
customRender?:
| React.ReactNode
| ((selectItems: (FlattedCheckCascaderDataItem | undefined)[]) => React.ReactNode)
}

if (__DEV__) {
Expand Down
99 changes: 99 additions & 0 deletions packages/ui/check-cascader/stories/custom-render.stories.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<h1>CusotmRender</h1>
<div className="check-cascader-custom-render__wrap">
<CheckCascader
style={{ width: 240 }}
searchable={false}
placeholder="请选择品类"
changeOnSelect
data={dataOnlyLeafCheckable}
onChange={console.log}
customRender={(data) => {
let value = []
if (data) {
value = data.map((item) => item.title)
}
return <Input value={!value ? '' : value} placeholder="请选择" />
}}
></CheckCascader>
</div>
</>
)
}
1 change: 1 addition & 0 deletions packages/ui/check-cascader/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ec38ee1

Please sign in to comment.