From 98b92bbd78736dbedc83cfadf84e5467c81db732 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Thu, 25 Oct 2018 09:45:40 +0200 Subject: [PATCH] refactor: Use the same mechanism for autogenerated component id To make the code more consistent, I added a method getUniqueId that is used by all components that need a unique id. I put this id recovery in the defaultProps object. Add also some tests for util file. --- .../src/components/Nav/NavExpandable.js | 30 ++++++++-------- .../react-core/src/components/Nav/NavGroup.js | 18 +++------- .../Nav/__snapshots__/Nav.test.js.snap | 5 --- .../src/components/Progress/Progress.js | 34 +++++++++++-------- .../react-core/src/internal/util.js | 9 +++++ .../react-core/src/internal/util.test.js | 14 ++++++++ 6 files changed, 63 insertions(+), 47 deletions(-) create mode 100644 packages/patternfly-4/react-core/src/internal/util.test.js diff --git a/packages/patternfly-4/react-core/src/components/Nav/NavExpandable.js b/packages/patternfly-4/react-core/src/components/Nav/NavExpandable.js index 10434db77ef..98d5c34502a 100644 --- a/packages/patternfly-4/react-core/src/components/Nav/NavExpandable.js +++ b/packages/patternfly-4/react-core/src/components/Nav/NavExpandable.js @@ -6,6 +6,7 @@ import PropTypes from 'prop-types'; import NavToggle from './NavToggle'; import { AngleRightIcon } from '@patternfly/react-icons'; import { NavContext } from './Nav'; +import { getUniqueId } from '../../internal/util'; const propTypes = { /** Title shown for the expandable list */ @@ -37,19 +38,20 @@ const defaultProps = { }; class NavExpandable extends React.Component { - constructor(props) { - super(props); - - this.uniqueId = - props.id || - new Date().getTime() + - Math.random() - .toString(36) - .slice(2); - } + id = this.props.id || getUniqueId(); render() { - const { title, srText, isExpanded: defaultExpanded, children, className, groupId, isActive, ...props } = this.props; + const { + id, + title, + srText, + isExpanded: defaultExpanded, + children, + className, + groupId, + isActive, + ...props + } = this.props; return ( @@ -68,7 +70,7 @@ class NavExpandable extends React.Component { > e.preventDefault()} onMouseDown={e => e.preventDefault()} @@ -81,11 +83,11 @@ class NavExpandable extends React.Component {