Skip to content

Commit f9f6265

Browse files
Merge pull request #161 from quid/feat/dropdown-support-customize-dropdownlist
feat: make Dropdown supports customize dropdown list
2 parents de65377 + 8f44688 commit f9f6265

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

packages/react-dropdown/src/List.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
type HighlightedIndex,
2020
} from './dropdownTypes.js';
2121

22-
type Props = {
22+
export type ListProps = {
2323
items: Array<DropdownItem>,
2424
categories: Array<DropdownCategory>,
2525
inputValue: ?string,
@@ -53,7 +53,7 @@ export const List = styled.div`
5353
min-width: 27.86em;
5454
`;
5555

56-
const DropdownList: React.ComponentType<Props> = styled(
56+
const DropdownList: React.ComponentType<ListProps> = styled(
5757
React.forwardRef(
5858
(
5959
{
@@ -69,7 +69,7 @@ const DropdownList: React.ComponentType<Props> = styled(
6969
highlight,
7070
multiselect,
7171
...props
72-
}: Props,
72+
}: ListProps,
7373
ref: React.ElementRef<any>
7474
) => {
7575
const filteredItems = useFilter ? filterFn(items, inputValue) : items;

packages/react-dropdown/src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as React from 'react';
99
import { type GetInputPropsReturn } from 'downshift';
1010
import { filterItems, callAll } from './utils';
11-
import DropdownList from './List';
11+
import DropdownList, { type ListProps } from './List';
1212
import InputCreator from './InputCreator';
1313
import styled from '@emotion/styled/macro';
1414
import MultiDownshift, {
@@ -60,6 +60,7 @@ type Props = {
6060
MultiControllerStateAndHelpers
6161
) => void,
6262
renderDropdown?: ({ dropdown: React.Node }) => React.Node,
63+
customizeDropdownList?: React.ComponentType<ListProps>,
6364
};
6465

6566
const DropdownContainer = styled.div`
@@ -88,6 +89,7 @@ const Dropdown = ({
8889
onSelect,
8990
highlight = false,
9091
renderDropdown = defaultRenderDropdown,
92+
customizeDropdownList,
9193
...props
9294
}: Props) => {
9395
if (defaultSelectedItems != null && selectedItems != null) {
@@ -101,6 +103,8 @@ const Dropdown = ({
101103
'Warning: Failed prop type: You provided a `selectedItems` prop to a form field without an `onChange` handler. If the field should be mutable use `defaultSelectedItems`. Otherwise, set `onChange`.'
102104
);
103105
}
106+
const List = customizeDropdownList || DropdownList;
107+
104108
return (
105109
<Manager>
106110
<MultiDownshift
@@ -162,7 +166,7 @@ const Dropdown = ({
162166
{isOpen &&
163167
renderDropdown({
164168
dropdown: (
165-
<DropdownList
169+
<List
166170
// $FlowFixMe(fzivolo): react-popper has wrong types?
167171
ref={ref}
168172
style={style}

packages/react-dropdown/src/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,20 @@ it('custom renderDropdown function should render', () => {
772772
expect(wrapper.find('ul li').hostNodes().length > 0).toBe(true);
773773
});
774774

775+
it('customize DropdownList should be rendered', () => {
776+
const customizeDropdownList = () => <div id="customizeList"></div>;
777+
const wrapper = mount(
778+
<Dropdown
779+
items={items.slice(0, 2)}
780+
defaultIsOpen={true}
781+
customizeDropdownList={customizeDropdownList}
782+
>
783+
{({ getInputProps }) => <input {...getInputProps()} />}
784+
</Dropdown>
785+
);
786+
expect(wrapper.find('#customizeList')).toHaveLength(1);
787+
});
788+
775789
it('DropdownList could be styled', () => {
776790
const StyledDropdown = styled(Dropdown)`
777791
${DL} {

0 commit comments

Comments
 (0)