From 44d98063d61ee5722744879cd8d9645d4b71a6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=A2=E6=9E=9C=E6=B1=81?= Date: Wed, 25 Oct 2023 15:24:23 +0800 Subject: [PATCH] feat: Implement `optionRender` API (#987) * feat: implement optionRender api * chore: clean code * docs: update md * refactor: modify api * refactor: fix type definition --- README.md | 3 +++ docs/demo/option-render.md | 8 ++++++++ docs/examples/option-render.tsx | 17 +++++++++++++++++ src/OptionList.tsx | 13 +++++++++---- src/Select.tsx | 16 ++++++++++++---- src/SelectContext.ts | 11 +++++++++-- tests/Select.test.tsx | 31 +++++++++++++++++++++++++------ 7 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 docs/demo/option-render.md create mode 100644 docs/examples/option-render.tsx diff --git a/README.md b/README.md index 71d69aea..4e318b24 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ React Select + [![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 + | 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 | (oriOption: FlattenOptionData\ , info: { index: number }) => React.ReactNode | - | ### Methods diff --git a/docs/demo/option-render.md b/docs/demo/option-render.md new file mode 100644 index 00000000..d203f935 --- /dev/null +++ b/docs/demo/option-render.md @@ -0,0 +1,8 @@ +--- +title: optionRender +nav: + title: Demo + path: /demo +--- + + diff --git a/docs/examples/option-render.tsx b/docs/examples/option-render.tsx new file mode 100644 index 00000000..216fd110 --- /dev/null +++ b/docs/examples/option-render.tsx @@ -0,0 +1,17 @@ +/* eslint-disable no-console */ +import Select from 'rc-select'; +import '../../assets/index.less'; + +export default () => { + return ( + x }} value="1"> + , @@ -277,17 +277,16 @@ describe('Select.Basic', () => { expect(wrapper3.find('.custom-clear-icon').text()).toBe('x'); const wrapper4 = mount( - x }}> , ); expect(wrapper4.find('.custom-clear-icon').length).toBeFalsy(); - resetWarned(); const wrapper5 = mount( - x} value="1"> , @@ -296,7 +295,7 @@ describe('Select.Basic', () => { expect(wrapper5.find('.custom-clear-icon').text()).toBe('x'); const wrapper6 = mount( - x}> , @@ -1361,7 +1360,7 @@ describe('Select.Basic', () => { beforeAll(() => { domHook = spyElementPrototypes(HTMLElement, { getBoundingClientRect: () => ({ - width: 1000 + width: 1000, }), }); }); @@ -2095,4 +2094,24 @@ describe('Select.Basic', () => { const { container } = testingRender( { + return `${option.label} - ${index}`; + }} + />, + ); + expect(container.querySelector('.rc-select-item-option-content').innerHTML).toEqual( + 'test1 - 0', + ); + }); });