Skip to content

Commit

Permalink
refactor: modify api
Browse files Browse the repository at this point in the history
  • Loading branch information
RedJue committed Oct 24, 2023
1 parent d231afa commit ba330ec
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +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 | - |
| optionRender | Custom rendering options | (oriOption: OptionType, info: { index: number }) => React.ReactNode | - |

### Methods

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/option-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default () => {
{ label: 'test1', value: '1' },
{ label: 'test2', value: '2' },
]}
optionRender={({ option, index }) => {
return <div>{`${option.label} - ${index}`}</div>;
optionRender={(option, { index }) => {
return `${option.label} - ${index}`;
}}
/>
);
Expand Down
9 changes: 1 addition & 8 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
>
<div className={`${optionPrefixCls}-content`}>
{typeof optionRender === 'function'
? optionRender({
option: { ...item.data, label: item.label },
index: itemIndex,
options: memoFlattenOptions.map((flattenItem) => ({
...flattenItem.data,
label: flattenItem.label,
})),
})
? optionRender({ ...item.data, label: item.label }, { index: itemIndex })
: content}
</div>
{React.isValidElement(menuItemSelectedIcon) || selected}
Expand Down
8 changes: 1 addition & 7 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,6 @@ export type SelectHandler<ValueType, OptionType extends BaseOptionType = Default
option: OptionType,
) => void;

export type OptionRenderParams = {
option: DefaultOptionType;
index: number;
options: DefaultOptionType[];
};

type ArrayElementType<T> = T extends (infer E)[] ? E : T;

export interface SelectProps<ValueType = any, OptionType extends BaseOptionType = DefaultOptionType>
Expand Down Expand Up @@ -144,7 +138,7 @@ export interface SelectProps<ValueType = any, OptionType extends BaseOptionType
optionLabelProp?: string;
children?: React.ReactNode;
options?: OptionType[];
optionRender?: (params: OptionRenderParams) => React.ReactNode;
optionRender?: (oriOption: OptionType, info: { index: number }) => React.ReactNode;
defaultActiveFirstOption?: boolean;
virtual?: boolean;
direction?: 'ltr' | 'rtl';
Expand Down
4 changes: 2 additions & 2 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2105,8 +2105,8 @@ describe('Select.Basic', () => {
<Select
open
options={options}
optionRender={({ option, index }) => {
return `${option.label} - ${index}`
optionRender={(option, {index}) => {
return `${option.label} - ${index}`;
}}
/>,
);
Expand Down

0 comments on commit ba330ec

Please sign in to comment.