Skip to content

Commit

Permalink
feat(select): add customRender api (#2765)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyprepare committed Mar 12, 2024
1 parent 1f88f9a commit 32b4c01
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 22 deletions.
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

0 comments on commit 32b4c01

Please sign in to comment.