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(select): add customRender api (#2765) #2768

Merged
merged 1 commit into from
Mar 15, 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/hip-falcons-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(select): add customRender api
5 changes: 5 additions & 0 deletions .changeset/olive-goats-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/select": minor
---

feat: add customRender api
57 changes: 35 additions & 22 deletions packages/ui/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
onSelect: onSelectProp,
onSearch: onSearchProp,
onKeyDown: onKeyDownProp,
customRender,
...rest
},
ref
Expand Down Expand Up @@ -241,28 +242,36 @@ export const Select = forwardRef<HTMLDivElement | null, SelectProps>(
footer={renderExtraFooter ? renderExtraFooter() : null}
scrollable={!inVirtual}
trigger={
<MockInput
clearable={clearable}
placeholder={placeholder}
displayRender={
displayRenderProp
? (item: any) => {
return displayRenderProp(getSelectItemEventData(item))
}
: undefined
}
prefix={prefix}
suffix={[menuVisible ? <UpOutlined /> : <DownOutlined />, suffix]}
focused={menuVisible}
value={value}
onChange={(value, item) => {
tryChangeValue(value, item.raw)
}}
size={size}
data={mergedData}
invalid={invalid}
appearance={appearance}
/>
customRender ? (
typeof customRender === 'function' ? (
customRender(showData.find((d: SelectDataItem) => d.id === value))
) : (
customRender
)
) : (
<MockInput
clearable={clearable}
placeholder={placeholder}
displayRender={
displayRenderProp
? (item: any) => {
return displayRenderProp(getSelectItemEventData(item))
}
: undefined
}
prefix={prefix}
suffix={[menuVisible ? <UpOutlined /> : <DownOutlined />, suffix]}
focused={menuVisible}
value={value}
onChange={(value, item) => {
tryChangeValue(value, item.raw)
}}
size={size}
data={mergedData}
invalid={invalid}
appearance={appearance}
/>
)
}
>
{isArrayNonEmpty(showData) ? (
Expand Down Expand Up @@ -380,6 +389,10 @@ export interface SelectProps
* 选择框后置内容
*/
suffix?: React.ReactNode
/**
* 自定义触发器
*/
customRender?: React.ReactNode | ((option: SelectItemEventData) => React.ReactNode)
}

;(Select as any).HiName = 'Select'
Expand Down
37 changes: 37 additions & 0 deletions packages/ui/select/stories/custom-render.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react'
import Input from '@hi-ui/input'
import Select from '../src'

/**
* @title 自定义触发器
*/
export const CustomRender = () => {
const [data] = React.useState([
{ title: '电视', id: '3', disabled: false },
{ title: '手机', id: '2' },
{ title: '笔记本', id: '4', disabled: false },
{
title: '生活周边超长文案展示超长文案展示',
id: '5',
},
{ title: '办公', id: '6' },
{ title: '生活周边7', id: '7' },
{ title: '办公8', id: '8' },
])

return (
<>
<h1>CustomRender</h1>
<div className="select-custom-render__wrap">
<Select
style={{ width: 240 }}
clearable={false}
data={data}
customRender={(data) => {
return <Input value={!data ? '' : data.title + ''} readOnly placeholder="请选择" />
}}
/>
</div>
</>
)
}
1 change: 1 addition & 0 deletions packages/ui/select/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './virtual-list.stories'
export * from './empty-content.stories'
export * from './tip.stories'
export * from './addon.stories'
export * from './custom-render.stories'

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