Skip to content

Commit

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

* chore(cascader): 生成变更记录文件(#2888)

---------

Co-authored-by: yangxuexue <[email protected]>
  • Loading branch information
yang-x20 and yangxuexue authored Jun 28, 2024
1 parent ec38ee1 commit c6d9225
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-items-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/cascader": minor
---

feat: 新增自定义触发器
5 changes: 5 additions & 0 deletions .changeset/smooth-pears-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(cascader): 新增自定义触发器
45 changes: 29 additions & 16 deletions packages/ui/cascader/src/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const Cascader = forwardRef<HTMLDivElement | null, CascaderProps>((props,
renderExtraFooter,
dropdownColumnRender,
closeOnSelect = true,
customRender,
...rest
} = props
const i18n = useLocaleContext()
Expand Down Expand Up @@ -237,22 +238,30 @@ export const Cascader = forwardRef<HTMLDivElement | null, CascaderProps>((props,
footer={isFunction(renderExtraFooter) && renderExtraFooter()}
onSearch={callAllFuncs(onSearchProp, onSearch)}
trigger={
<MockInput
size={size}
clearable={clearable}
placeholder={placeholder}
displayRender={displayRender as any}
prefix={prefix}
suffix={[menuVisible ? <UpOutlined /> : <DownOutlined />, suffix]}
focused={menuVisible}
value={value[value.length - 1]}
onChange={() => {
tryChangeValue([])
}}
data={mergedData}
invalid={invalid}
appearance={appearance}
/>
customRender ? (
typeof customRender === 'function' ? (
customRender(selectedItem)
) : (
customRender
)
) : (
<MockInput
size={size}
clearable={clearable}
placeholder={placeholder}
displayRender={displayRender as any}
prefix={prefix}
suffix={[menuVisible ? <UpOutlined /> : <DownOutlined />, suffix]}
focused={menuVisible}
value={value[value.length - 1]}
onChange={() => {
tryChangeValue([])
}}
data={mergedData}
invalid={invalid}
appearance={appearance}
/>
)
}
>
{isArrayNonEmpty(showData) ? <CascaderMenuList /> : null}
Expand Down Expand Up @@ -342,6 +351,10 @@ export interface CascaderProps
* 选择框后置内容
*/
suffix?: React.ReactNode
/**
* 自定义触发器
*/
customRender?: React.ReactNode | ((selectedItem: CascaderItemEventData | null) => React.ReactNode)
}

if (__DEV__) {
Expand Down
76 changes: 76 additions & 0 deletions packages/ui/cascader/stories/custom-render.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react'
import Cascader from '../src'
import Input from '@hi-ui/input'

/**
* @title 自定义触发器
*/
export const CustomRender = () => {
const [data] = React.useState([
{
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',
},
],
},
])

return (
<>
<h1>CustomRender</h1>
<div className="cascader-custom-render__wrap">
<Cascader
style={{ width: 240 }}
clearable
placeholder="请选择品类"
data={data}
customRender={(data) => {
return <Input value={!data ? '' : data.title + ''} placeholder="请选择" />
}}
></Cascader>
</div>
</>
)
}
1 change: 1 addition & 0 deletions packages/ui/cascader/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './footer.stories'
export * from './dropdown-column-render.stories'
export * from './field-names.stories'
export * from './visible.stories'
export * from './custom-render.stories'

export default {
title: 'Data Input/Cascader',
Expand Down

0 comments on commit c6d9225

Please sign in to comment.