Dropdowns
The Dropdown
component is used to pass the isOpen
& toggle
props via context to the following components: DropdownToggle
, DropdownMenu
. The DropdownToggle
uses the Button
component internally, meaning it also accepts all the props the Button component accepts.
import React from 'react';
import { DropdownWithIcon, DropdownToggle, DropdownMenu, DropdownItem } from 'seduo-ui';
export default class Example extends React.Component<any, any> {
constructor(props) {
super(props);
const items = [
{
id: 'first',
value: 'First Action',
isDefault: true,
},
{
id: 'second',
value: 'Second Action',
isDefault: false,
},
{
id: 'third',
value: 'Third Action',
isDefault: false,
},
];
this.state = {
isOpen: false,
selected: items[0],
items,
};
}
toggle = () => {
this.setState((prevState) => ({
isOpen: !prevState.isOpen,
}));
};
setFilter(item) {
this.setState({
selected: item,
});
}
render() {
const { isOpen, selected, items } = this.state;
return (
<DropdownWithIcon
isOpen={isOpen}
toggle={this.toggle}
selected={selected.value}
isDefaultSelected={selected.isDefault}
>
{items.map((item) => (
<DropdownItem active={item === selected} key={item.id} onClick={() => this.setFilter(item)}>
{item.value}
</DropdownItem>
))}
</DropdownWithIcon>
);
}
}
Properties
DropdownWithIcon.propTypes = {
children: PropTypes.instanceOf(DropdownItem),
title: PropTypes.string.isRequired,
};
import React from 'react';
import { Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'seduo-ui';
export default class Example extends React.Component<any, any> {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
dropdownOpen: false,
};
}
toggle() {
this.setState({
dropdownOpen: !this.state.dropdownOpen,
});
}
render() {
return (
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
<DropdownToggle>Dropdown</DropdownToggle>
<DropdownMenu>
<DropdownItem header>Header</DropdownItem>
<DropdownItem disabled>Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem divider />
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</Dropdown>
);
}
}
Properties
Dropdown.propTypes = {
disabled: PropTypes.bool,
direction: PropTypes.oneOf(['up', 'down', 'left', 'right']),
group: PropTypes.bool,
isOpen: PropTypes.bool,
// For Dropdown usage inside a Nav
nav: PropTypes.bool,
active: PropTypes.bool,
// For Dropdown usage inside a Navbar (disables popper)
inNavbar: PropTypes.bool,
tag: PropTypes.string, // default: 'div' unless nav=true, then 'li'
toggle: PropTypes.func
};
DropdownToggle.propTypes = {
color: PropTypes.string,
className: PropTypes.string,
disabled: PropTypes.bool,
onClick: PropTypes.func,
'data-toggle': PropTypes.string,
'aria-haspopup': PropTypes.bool,
// For DropdownToggle usage inside a Nav
nav: PropTypes.bool,
// Defaults to Button component
tag: PropTypes.any
};
DropdownMenu.propTypes = {
tag: PropTypes.string,
children: PropTypes.node.isRequired,
right: PropTypes.bool,
flip: PropTypes.bool, // default: true,
className: PropTypes.string,
cssModule: PropTypes.object,
// Custom modifiers that are passed to DropdownMenu.js, see https://popper.js.org/popper-documentation.html#modifiers
modifiers: PropTypes.object
};
DropdownItem.propTypes = {
children: PropTypes.node,
active: PropTypes.bool,
disabled: PropTypes.bool,
divider: PropTypes.bool,
tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
header: PropTypes.bool,
onClick: PropTypes.func,
className: PropTypes.string,
cssModule: PropTypes.object,
toggle: PropTypes.bool // default: true
};
Alignment
To align the DropdownMenu
to the right, add a right
prop to Dropdown
.
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
<DropdownToggle>
This dropdown's menu is right-aligned
</DropdownToggle>
<DropdownMenu right>
<DropdownItem header>Header</DropdownItem>
<DropdownItem disabled>Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem divider/>
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</Dropdown>
Menu Headers
<DropdownItem header>Header</DropdownItem>
Menu Dividers
<DropdownItem divider/>
Menu Items
<DropdownItem>Action</DropdownItem>
Disabled Menu Items
<DropdownItem disabled>Action</DropdownItem>
Sizing
<Dropdown group isOpen={this.state.dropdownOpen} size="lg" toggle={this.toggle}>
<DropdownToggle>
Dropdown
</DropdownToggle>
<DropdownMenu>
...
</DropdownMenu>
</Dropdown>
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
<DropdownToggle>
Dropdown
</DropdownToggle>
<DropdownMenu>
...
</DropdownMenu>
</Dropdown>
<Dropdown group isOpen={this.state.dropdownOpen} size="sm" toggle={this.toggle}>
<DropdownToggle>
Dropdown
</DropdownToggle>
<DropdownMenu>
...
</DropdownMenu>
</Dropdown>
Custom Dropdown
Custom Dropdown Content
import React from 'react';
import { Dropdown, DropdownMenu, DropdownToggle } from 'seduo-ui';
export default class Example extends React.Component<any, any> {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
dropdownOpen: false,
};
}
toggle() {
this.setState({
dropdownOpen: !this.state.dropdownOpen,
});
}
render() {
return (
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
<DropdownToggle
tag="span"
onClick={this.toggle}
data-toggle="dropdown"
aria-expanded={this.state.dropdownOpen}
>
Custom Dropdown Content
</DropdownToggle>
<DropdownMenu>
<div onClick={this.toggle}>Custom dropdown item</div>
<div onClick={this.toggle}>Custom dropdown item</div>
<div onClick={this.toggle}>Custom dropdown item</div>
<div onClick={this.toggle}>Custom dropdown item</div>
</DropdownMenu>
</Dropdown>
);
}
}
Uncontrolled Dropdown
For the most basic use-case an uncontrolled component can provide the functionality wanted without the need to manage/control the state of the component. UncontrolledDropdown
does not require isOpen
nor toggle
props to work.
import React from 'react';
import { UncontrolledDropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'seduo-ui';
export default function Example() {
return (
<UncontrolledDropdown>
<DropdownToggle>Dropdown</DropdownToggle>
<DropdownMenu>
<DropdownItem header>Header</DropdownItem>
<DropdownItem disabled>Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem divider />
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
);
}
Drop direction variations
<Dropdown direction="up" isOpen={isOpen} toggle={toggle}>
<DropdownToggle>
Dropup
</DropdownToggle>
<DropdownMenu>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</Dropdown>
<Dropdown direction="left" isOpen={this.state.btnDropleft} toggle={() => { this.setState({ btnDropleft: !this.state.btnDropleft }); }}>
<DropdownToggle>
Dropleft
</DropdownToggle>
<DropdownMenu>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</Dropdown>
<Dropdown direction="right" isOpen={this.state.btnDropright} toggle={() => { this.setState({ btnDropright: !this.state.btnDropright }); }}>
<DropdownToggle>
Dropright
</DropdownToggle>
<DropdownMenu>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</Dropdown>
Modifiers
<Dropdown isOpen={isOpen} toggle={toggle}>
<DropdownToggle>
Dropdown
</DropdownToggle>
<DropdownMenu
modifiers={{
setMaxHeight: {
enabled: true,
order: 890,
fn: (data) => {
return {
...data,
styles: {
...data.styles,
overflow: 'auto',
maxHeight: 100,
},
};
},
},
}}
>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</Dropdown>