-
-
Notifications
You must be signed in to change notification settings - Fork 457
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: Implement optionRender
API
#987
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
React Select | ||
|
||
<!-- prettier-ignore --> | ||
[![NPM version][npm-image]][npm-url] | ||
[![npm download][download-image]][download-url] | ||
[![build status][github-actions-image]][github-actions-url] | ||
|
@@ -67,6 +68,7 @@ export default () => ( | |
|
||
### Select props | ||
|
||
<!-- prettier-ignore --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个格式化,可以在 prettierrc 里加
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改是直接加方便点,这样就是全局配了,应该也可以 |
||
| name | description | type | default | | ||
| --- | --- | --- | --- | | ||
| id | html id to set on the component wrapper | String | '' | | ||
|
@@ -126,6 +128,7 @@ export default () => ( | |
| loading | show loading icon in arrow | boolean | false | | ||
| virtual | Disable virtual scroll | boolean | true | | ||
| direction | direction of dropdown | 'ltr' \| 'rtl' | 'ltr' | | ||
| optionRender | Custom rendering options | (params: OptionRenderParams) => React.ReactNode | - | | ||
|
||
### Methods | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
title: optionRender | ||
nav: | ||
title: Demo | ||
path: /demo | ||
--- | ||
|
||
<code src="../examples/option-render.tsx"></code> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable no-console */ | ||
import Select from 'rc-select'; | ||
import '../../assets/index.less'; | ||
|
||
export default () => { | ||
return ( | ||
<Select | ||
options={[ | ||
{ label: 'test1', value: '1' }, | ||
{ label: 'test2', value: '2' }, | ||
]} | ||
optionRender={({ option, index }) => { | ||
return <div>{`${option.label} - ${index}`}</div>; | ||
}} | ||
/> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r | |
direction, | ||
listHeight, | ||
listItemHeight, | ||
optionRender, | ||
} = React.useContext(SelectContext); | ||
|
||
const itemPrefixCls = `${prefixCls}-item`; | ||
|
@@ -366,7 +367,18 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r | |
}} | ||
style={style} | ||
> | ||
<div className={`${optionPrefixCls}-content`}>{content}</div> | ||
<div className={`${optionPrefixCls}-content`}> | ||
{typeof optionRender === 'function' | ||
? optionRender({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 和 SubItem 的 API 对齐吧: type OptionRender = (oriOption, info: { index }) => ReactNode; 早期,给的东西不用太多。index 就够了~ ref https://github.com/ant-design/ant-design/wiki/API-Naming-rules#props There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok 我改下 |
||
option: { ...item.data, label: item.label }, | ||
index: itemIndex, | ||
options: memoFlattenOptions.map((flattenItem) => ({ | ||
...flattenItem.data, | ||
label: flattenItem.label, | ||
})), | ||
}) | ||
: content} | ||
</div> | ||
{React.isValidElement(menuItemSelectedIcon) || selected} | ||
{iconVisible && ( | ||
<TransBtn | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以直接在 prettier ignore 文件中加上过滤