Skip to content

Commit

Permalink
feat: support for custom item children for the dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Micko committed Feb 20, 2019
1 parent fdc39be commit d481d5b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
16 changes: 9 additions & 7 deletions packages/react-dropdown/src/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ export default function DropdownCategories({
return (
<Categories key={categoryId}>
<Category twoColumn={twoColumn}>
<Divider
isHighlighted={isHighlighted}
isSelected={isSelected}
twoColumn={twoColumn}
>
<GroupTitle>{category.label}</GroupTitle>
</Divider>
{!category.hidden && (
<Divider
isHighlighted={isHighlighted}
isSelected={isSelected}
twoColumn={twoColumn}
>
<GroupTitle>{category.label}</GroupTitle>
</Divider>
)}
<Divider twoColumn={twoColumn}>
<DropdownItems
categoryId={categoryId}
Expand Down
21 changes: 15 additions & 6 deletions packages/react-dropdown/src/Items.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,21 @@ export default function DropdownItems({
disabled: isDisabled,
})}
>
<HighlightValue
highlight={highlight}
value={item.label}
valueToHighlight={inputValue}
disabled={isDisabled}
/>
{item.children ? (
item.children({
isHighlighted: isHighlighted,
isSelected: isSelected,
isDisabled: isDisabled,
item,
})
) : (
<HighlightValue
highlight={highlight}
value={item.label}
valueToHighlight={inputValue}
disabled={isDisabled}
/>
)}
</Item>
);
})}
Expand Down
7 changes: 7 additions & 0 deletions packages/react-dropdown/src/dropdownTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ export type DropdownItem = {
label: string,
categoryId?: number | string,
disabled?: boolean,
children?: ({
isSelected: boolean,
isDisabled: boolean,
isHighlighted: boolean,
item: DropdownItem,
}) => React.Node,
};

export type DropdownCategory = {
id: number | string,
label: string,
hidden?: boolean,
};

export type ExtendedCategory = DropdownCategory & {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dropdown/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const Dropdown = ({
</Reference>
{isOpen && (
<DropdownList
innerRef={ref}
ref={ref}
style={style}
twoColumn={twoColumn}
items={items}
Expand Down
25 changes: 25 additions & 0 deletions packages/react-dropdown/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,28 @@ it('supports dark theme', () => {
)
).toHaveStyleRule('background-color', 'gray6');
});

it('Children provided trough items should be present', () => {
const wrapper = mount(
<Dropdown
items={[
{
id: 131,
categoryId: 'c',
label: 'Thirteen',
children: ({ isHighlighted, isSelected, isDisabled, item }) => (
<div data-child="hw" data-itemid={item.id}>
Hello world.
</div>
),
},
]}
initialIsOpen={true}
>
{({ getInputProps }) => <Input {...getInputProps()} />}
</Dropdown>
);
const children = wrapper.find('div[data-child="hw"]');
expect(children.prop('data-itemid')).toBe(131);
expect(children.length).toBe(1);
});

0 comments on commit d481d5b

Please sign in to comment.