Skip to content

Commit 9aafabd

Browse files
ibolton336redallen
authored andcommitted
fix(navExpand): remove defaultExpand in favor of just using isExpanded (#1490)
1 parent 86824d9 commit 9aafabd

File tree

6 files changed

+56
-67
lines changed

6 files changed

+56
-67
lines changed

packages/patternfly-4/react-core/src/components/Nav/NavExpandable.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { FunctionComponent, HTMLProps, ReactNode } from 'react';
33
export interface NavExpandableProps extends HTMLProps<HTMLDivElement> {
44
title: string;
55
srText?: string;
6-
defaultExpanded?: boolean;
76
isExpanded?: boolean;
87
children?: ReactNode;
98
className?: string;
109
groupId?: string | number;
1110
isActive?: boolean;
1211
id?: string;
12+
onExpand?(e: Event, val: boolean): void;
1313
}
1414

1515
declare const NavExpandable: FunctionComponent<NavExpandableProps>;

packages/patternfly-4/react-core/src/components/Nav/NavExpandable.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ const propTypes = {
1313
title: PropTypes.string.isRequired,
1414
/** If defined, screen readers will read this text instead of the list title */
1515
srText: PropTypes.string,
16-
/** If true will default the list to be expanded */
17-
defaultExpanded: PropTypes.bool,
1816
/** Boolean to programatically expand or collapse section */
1917
isExpanded: PropTypes.bool,
2018
/** Anything that can be rendered inside of the expandable list */
@@ -28,45 +26,54 @@ const propTypes = {
2826
/** Identifier to use for the section aria label */
2927
id: PropTypes.string,
3028
/** Additional props are spread to the container <li> */
31-
'': PropTypes.any
29+
'': PropTypes.any,
30+
/** allow consumer to optionally override this callback and manage expand state externally */
31+
onExpand: PropTypes.func
3232
};
3333

3434
const defaultProps = {
3535
srText: '',
36-
defaultExpanded: false,
37-
isExpanded: null,
36+
isExpanded: false,
3837
children: null,
3938
className: '',
4039
groupId: null,
4140
isActive: false,
42-
id: ''
41+
id: '',
42+
onExpand: undefined
4343
};
4444

4545
class NavExpandable extends React.Component {
4646
id = this.props.id || getUniqueId();
47+
state = {
48+
expandedState: false
49+
};
50+
51+
componentDidMount() {
52+
this.setState({ expandedState: this.props.isExpanded });
53+
}
54+
55+
componentDidUpdate(prevProps) {
56+
if (this.props.isExpanded !== prevProps.isExpanded) {
57+
this.setState({ expandedState: this.props.isExpanded });
58+
}
59+
}
60+
61+
onExpand = (e, val) => {
62+
if (this.props.onExpand) {
63+
this.props.onExpand(e, val);
64+
} else {
65+
this.setState({ expandedState: val });
66+
}
67+
};
68+
4769
render() {
48-
const {
49-
id,
50-
title,
51-
srText,
52-
isExpanded,
53-
defaultExpanded,
54-
children,
55-
className,
56-
groupId,
57-
isActive,
58-
...props
59-
} = this.props;
70+
const { id, title, srText, isExpanded, children, className, groupId, isActive, ...props } = this.props;
71+
const { expandedState } = this.state;
6072

6173
return (
6274
<NavContext.Consumer>
6375
{context => (
64-
<NavToggle
65-
defaultValue={defaultExpanded}
66-
isExpanded={isExpanded}
67-
groupId={groupId}
68-
onToggle={context.onToggle}
69-
>
76+
<NavToggle groupId={groupId} onToggle={context.onToggle} onExpand={this.onExpand} isExpanded={expandedState}>
7077
{({ toggleValue, toggle }) => (
7178
<li
7279
className={css(

packages/patternfly-4/react-core/src/components/Nav/NavToggle.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,36 @@ import PropTypes from 'prop-types';
33

44
const propTypes = {
55
children: PropTypes.func.isRequired,
6-
defaultValue: PropTypes.bool,
6+
isExpanded: PropTypes.bool,
77
groupId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
8-
onToggle: PropTypes.func
8+
onToggle: PropTypes.func,
9+
onExpand: PropTypes.func
910
};
1011

1112
const defaultProps = {
12-
defaultValue: false,
13+
isExpanded: false,
1314
groupId: 0,
14-
onToggle: () => undefined
15+
onToggle: () => undefined,
16+
onExpand: () => undefined
1517
};
1618

1719
class NavToggle extends React.Component {
1820
static propTypes = propTypes;
1921
static defaultProps = defaultProps;
20-
state = {
21-
toggleValue: this.props.defaultValue
22-
};
23-
componentDidUpdate(prevProps) {
24-
if (this.props.isExpanded !== prevProps.isExpanded) {
25-
this.setState({ toggleValue: this.props.isExpanded });
26-
}
27-
}
2822

2923
handleToggle = e => {
3024
// Item events can bubble up, ignore those
3125
if (e.target.getAttribute('data-component') !== 'pf-nav-expandable') {
3226
return;
3327
}
34-
const { toggleValue } = this.state;
35-
const { groupId, onToggle } = this.props;
36-
this.setState({
37-
toggleValue: !toggleValue
38-
});
39-
onToggle(e, groupId, !toggleValue);
28+
const { groupId, onToggle, onExpand, isExpanded } = this.props;
29+
onToggle(e, groupId, !isExpanded);
30+
onExpand(e, !isExpanded);
4031
};
4132

4233
render() {
4334
return this.props.children({
44-
toggleValue: this.state.toggleValue,
35+
toggleValue: this.props.isExpanded,
4536
toggle: this.handleToggle
4637
});
4738
}

packages/patternfly-4/react-core/src/components/Nav/__snapshots__/Nav.test.js.snap

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ exports[`Expandable Nav List - Trigger toggle 1`] = `
356356
overflow: hidden;
357357
opacity: 0;
358358
}
359-
.pf-c-nav__item.expandable-group {
359+
.pf-c-nav__item.pf-m-expanded.expandable-group {
360360
display: block;
361361
}
362362
.pf-c-nav__list {
@@ -388,7 +388,6 @@ exports[`Expandable Nav List - Trigger toggle 1`] = `
388388
>
389389
<NavExpandable
390390
className="expandable-group"
391-
defaultExpanded={false}
392391
groupId={null}
393392
id="grp-1"
394393
isActive={false}
@@ -397,17 +396,17 @@ exports[`Expandable Nav List - Trigger toggle 1`] = `
397396
title="Section 1"
398397
>
399398
<NavToggle
400-
defaultValue={false}
401399
groupId={null}
402400
isExpanded={true}
401+
onExpand={[Function]}
403402
onToggle={[Function]}
404403
>
405404
<li
406-
className="pf-c-nav__item expandable-group"
405+
className="pf-c-nav__item pf-m-expanded expandable-group"
407406
onClick={[Function]}
408407
>
409408
<a
410-
aria-expanded={false}
409+
aria-expanded={true}
411410
className="pf-c-nav__link"
412411
data-component="pf-nav-expandable"
413412
href="#"
@@ -450,7 +449,7 @@ exports[`Expandable Nav List - Trigger toggle 1`] = `
450449
<section
451450
aria-labelledby="grp-1"
452451
className="pf-c-nav__subnav"
453-
hidden={true}
452+
hidden={null}
454453
>
455454
<ul
456455
className="pf-c-nav__simple-list"
@@ -642,18 +641,17 @@ exports[`Expandable Nav List 1`] = `
642641
>
643642
<NavExpandable
644643
className=""
645-
defaultExpanded={false}
646644
groupId={null}
647645
id="grp-1"
648646
isActive={false}
649-
isExpanded={null}
647+
isExpanded={false}
650648
srText=""
651649
title="Section 1"
652650
>
653651
<NavToggle
654-
defaultValue={false}
655652
groupId={null}
656-
isExpanded={null}
653+
isExpanded={false}
654+
onExpand={[Function]}
657655
onToggle={[Function]}
658656
>
659657
<li
@@ -904,18 +902,17 @@ exports[`Expandable Nav List with aria label 1`] = `
904902
>
905903
<NavExpandable
906904
className=""
907-
defaultExpanded={false}
908905
groupId={null}
909906
id="grp-1"
910907
isActive={false}
911-
isExpanded={null}
908+
isExpanded={false}
912909
srText="Section 1 - Example sub-navigation"
913910
title="Section 1"
914911
>
915912
<NavToggle
916-
defaultValue={false}
917913
groupId={null}
918-
isExpanded={null}
914+
isExpanded={false}
915+
onExpand={[Function]}
919916
onToggle={[Function]}
920917
>
921918
<li

packages/patternfly-4/react-core/src/components/Nav/examples/NavExpandableList.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class NavExpandableList extends React.Component {
2929
return (
3030
<Nav onSelect={this.onSelect} onToggle={this.onToggle}>
3131
<NavList>
32-
<NavExpandable title="Link 1" groupId="grp-1" isActive={activeGroup === 'grp-1'} defaultExpanded>
32+
<NavExpandable title="Link 1" groupId="grp-1" isActive={activeGroup === 'grp-1'} isExpanded>
3333
<NavItem
3434
preventDefault
3535
to="#expandable-1"
@@ -52,7 +52,7 @@ class NavExpandableList extends React.Component {
5252
Subnav Link 3
5353
</NavItem>
5454
</NavExpandable>
55-
<NavExpandable title="Link 2" groupId="grp-2" isActive={activeGroup === 'grp-2'}>
55+
<NavExpandable title="Link 2" groupId="grp-2" isActive={activeGroup === 'grp-2'} isExpanded>
5656
<NavItem
5757
preventDefault
5858
onClick={this.handleItemOnclick}

packages/patternfly-4/react-core/src/components/Nav/examples/NavExpandableTitlesList.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ class NavExpandableTitlesList extends React.Component {
1919
return (
2020
<Nav onSelect={this.onSelect}>
2121
<NavList>
22-
<NavExpandable
23-
title="Link 1"
24-
srText="SR Link"
25-
groupId="grp-1"
26-
isActive={activeGroup === 'grp-1'}
27-
defaultExpanded
28-
>
22+
<NavExpandable title="Link 1" srText="SR Link" groupId="grp-1" isActive={activeGroup === 'grp-1'} isExpanded>
2923
<NavItem
3024
preventDefault
3125
to="#sr-expandable-1"

0 commit comments

Comments
 (0)