Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cascader): 新增自定义触发器(#2888) #2921

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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