Skip to content

Commit 8cc2988

Browse files
authored
Merge pull request #398 from mashmatrix/allow-opened-in-dropdown-button
Allow opened/defaultOpened property to render dropdown initially
2 parents 7a49f09 + 74b45ad commit 8cc2988

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/scripts/DropdownButton.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export type Key = string | number;
1919
export type DropdownButtonProps<EventKey extends Key> = {
2020
className?: string;
2121
label?: React.ReactNode;
22+
opened?: boolean;
23+
defaultOpened?: boolean;
2224
menuAlign?: DropdownMenuAlign;
2325
menuSize?: DropdownMenuSize;
2426
menuHeader?: string;
@@ -55,7 +57,11 @@ class DropdownButtonInner<EventKey extends Key> extends Component<
5557

5658
constructor(props: Readonly<DropdownButtonInnerProps<EventKey>>) {
5759
super(props);
58-
this.state = { opened: false };
60+
const opened =
61+
typeof this.props.opened === 'undefined'
62+
? this.props.defaultOpened || false
63+
: this.props.opened;
64+
this.state = { opened };
5965
registerStyle('no-hover-popup', [
6066
[
6167
'.slds-dropdown-trigger:hover .slds-dropdown_menu.react-slds-no-hover-popup',
@@ -80,11 +86,15 @@ class DropdownButtonInner<EventKey extends Key> extends Component<
8086
};
8187

8288
onKeyDown = (e: KeyboardEvent<HTMLButtonElement>) => {
89+
const opened =
90+
typeof this.props.opened === 'undefined'
91+
? this.state.opened
92+
: this.props.opened;
8393
if (e.keyCode === 40) {
8494
// down
8595
e.preventDefault();
8696
e.stopPropagation();
87-
if (!this.state.opened) {
97+
if (!opened) {
8898
this.setState({ opened: true });
8999
if (this.props.onClick) {
90100
this.props.onClick(e);
@@ -205,9 +215,13 @@ class DropdownButtonInner<EventKey extends Key> extends Component<
205215
...props
206216
} = this.props;
207217
let { icon } = this.props;
218+
const opened =
219+
typeof this.props.opened === 'undefined'
220+
? this.state.opened
221+
: this.props.opened;
208222
const dropdownClassNames = classnames(className, 'slds-dropdown-trigger', {
209223
'slds-button-space-left': !props.grouped,
210-
'react-slds-dropdown-opened': this.state.opened,
224+
'react-slds-dropdown-opened': opened,
211225
});
212226
let iconMore = null;
213227
if (!label && !icon) {
@@ -242,7 +256,7 @@ class DropdownButtonInner<EventKey extends Key> extends Component<
242256
ref={(node) => (this.node = node)}
243257
>
244258
{this.renderButton({ type, label, icon, iconMore, ...props })}
245-
{hoverPopup || this.state.opened ? dropdown : undefined}
259+
{hoverPopup || opened ? dropdown : undefined}
246260
</div>
247261
);
248262
}

0 commit comments

Comments
 (0)