Skip to content

Commit

Permalink
Merge pull request #631 from faichuk/dropdown-toggle-event
Browse files Browse the repository at this point in the history
feat(DropDownGroup): add onDropDownToggle callback
  • Loading branch information
dtereshenko authored Dec 11, 2019
2 parents 50e9cc2 + e439d61 commit 8d2ef0b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions catalog/pages/inputs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ rows:
Type: boolean
Default: 'false'
Notes: Defines if width is 100% of container
- Prop: onDropDownToggle
Type: function
Default: 'null'
Notes: Callback invoked when dropdown open/hide event fired
```

## Drop Down Option
Expand Down
35 changes: 24 additions & 11 deletions src/components/Input/DropDown/DropDownGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,18 @@ class DropDownGroup extends React.Component {
};

toggleDropdown = () => {
if (this.state.isOpen) {
const { onDropDownToggle } = this.props;
const { isOpen } = this.state;

if (isOpen) {
this.closeDropdown();
} else {
this.openDropdown();
}

if (onDropDownToggle) {
onDropDownToggle(!isOpen);
}
};

stopInteraction = e => {
Expand Down Expand Up @@ -218,7 +225,7 @@ class DropDownGroup extends React.Component {
className={classNames(props.className, {
"dropdown--open-upward": hasOpenUpwardClass,
"dropdown--disabled": disabled,
"full-width": fullWidth,
"full-width": fullWidth
})}
tabIndex={disabled ? -1 : 0}
aria-haspopup="listbox"
Expand All @@ -228,13 +235,16 @@ class DropDownGroup extends React.Component {
>
<StyledGroup
{...onClickListener}
className={classNames(`dropdown__label dropdown--${size}`, {
"dropdown--active": isOpen,
"dropdown--border": isBorderAround,
"dropdown--no-border": !isBorderAround,
"dropdown__label--disabled": disabled,
"full-width": fullWidth
})}
className={classNames(
`dropdown__label dropdown--${size}`,
{
"dropdown--active": isOpen,
"dropdown--border": isBorderAround,
"dropdown--no-border": !isBorderAround,
"dropdown__label--disabled": disabled,
"full-width": fullWidth
}
)}
>
{/* HiddenLabel is required for correct screen readers
readings when an option is selected */}
Expand All @@ -246,7 +256,8 @@ class DropDownGroup extends React.Component {
"dropdown__text--disabled": disabled
})}
>
{icon}{this.displayLabel(selected)}
{icon}
{this.displayLabel(selected)}
</StyledSelectedText>

{chevronVisible && (
Expand Down Expand Up @@ -313,7 +324,8 @@ DropDownGroup.propTypes = {
fullWidth: PropTypes.bool,
shouldOpenDownward: PropTypes.bool,
icon: PropTypes.node,
chevronVisible: PropTypes.bool
chevronVisible: PropTypes.bool,
onDropDownToggle: PropTypes.func
};

DropDownGroup.defaultProps = {
Expand All @@ -332,6 +344,7 @@ DropDownGroup.defaultProps = {
icon: null,
chevronVisible: true,
fullWidth: false,
onDropDownToggle: null
};

export default DropDownGroup;
11 changes: 11 additions & 0 deletions src/components/Input/__tests__/DropDownGroup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,17 @@ describe("DropDownGroup", () => {

expect(chevron).toBe(null);
});

it("should fire onDropDownToggle if its passed", () => {
const onDropDownToggleEvent = jest.fn();
const { container } = renderTestComponentOne({
onDropDownToggle: onDropDownToggleEvent
});
const labelTag = container.getElementsByTagName("LABEL")[0];

Simulate.click(labelTag);
expect(onDropDownToggleEvent).toBeCalled();
});
});

describe("DropDownOption", () => {
Expand Down

0 comments on commit 8d2ef0b

Please sign in to comment.