Skip to content

Commit

Permalink
fix: Apply suggestions from code review
Browse files Browse the repository at this point in the history
- Add a displayName property to each function component
- Revert DropdownItemSpec changes
- Fix rendering issues caused by callback functions via useCallback
- Reduce imperative code in Toast via useTimeout hook
- Hoist function out of Tabs render.
  • Loading branch information
bpas247 committed Aug 6, 2019
1 parent 44d5e28 commit c3fab88
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 46 deletions.
14 changes: 9 additions & 5 deletions src/Fade.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import React from 'react';
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import Transition, {
ENTERED,
Expand Down Expand Up @@ -77,10 +77,13 @@ const fadeStyles = {
};

const Fade = React.forwardRef(({ className, children, ...props }, ref) => {
const handleEnter = node => {
triggerBrowserReflow(node);
if (props.onEnter) props.onEnter(node);
};
const handleEnter = useCallback(
node => {
triggerBrowserReflow(node);
if (props.onEnter) props.onEnter(node);
},
[props],
);

return (
<Transition
Expand All @@ -106,5 +109,6 @@ const Fade = React.forwardRef(({ className, children, ...props }, ref) => {

Fade.propTypes = propTypes;
Fade.defaultProps = defaultProps;
Fade.displayName = 'Fade';

export default Fade;
1 change: 1 addition & 0 deletions src/InputGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const InputGroupRadio = props => (
);

InputGroup.propTypes = propTypes;
InputGroup.displayName = 'InputGroup';

InputGroup.Text = InputGroupText;
InputGroup.Radio = InputGroupRadio;
Expand Down
1 change: 1 addition & 0 deletions src/Jumbotron.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ const Jumbotron = React.forwardRef(

Jumbotron.propTypes = propTypes;
Jumbotron.defaultProps = defaultProps;
Jumbotron.displayName = 'Jumbotron';

export default Jumbotron;
1 change: 1 addition & 0 deletions src/ListGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const ListGroup = React.forwardRef((props, ref) => {

ListGroup.propTypes = propTypes;
ListGroup.defaultProps = defaultProps;
ListGroup.displayName = 'ListGroup';

ListGroup.Item = ListGroupItem;

Expand Down
22 changes: 13 additions & 9 deletions src/ListGroupItem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classNames from 'classnames';
import React from 'react';
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';

import AbstractNavItem from './AbstractNavItem';
Expand Down Expand Up @@ -71,15 +71,18 @@ const ListGroupItem = React.forwardRef(
) => {
bsPrefix = useBootstrapPrefix(bsPrefix, 'list-group-item');

const handleClick = event => {
if (disabled) {
event.preventDefault();
event.stopPropagation();
return;
}
const handleClick = useCallback(
event => {
if (disabled) {
event.preventDefault();
event.stopPropagation();
return;
}

if (onClick) onClick(event);
};
if (onClick) onClick(event);
},
[disabled, onClick],
);

return (
<AbstractNavItem
Expand All @@ -104,5 +107,6 @@ const ListGroupItem = React.forwardRef(

ListGroupItem.propTypes = propTypes;
ListGroupItem.defaultProps = defaultProps;
ListGroupItem.displayName = 'ListGroupItem';

export default ListGroupItem;
1 change: 1 addition & 0 deletions src/SafeAnchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ const SafeAnchor = React.forwardRef(
);

SafeAnchor.propTypes = propTypes;
SafeAnchor.displayName = 'SafeAnchor';

export default SafeAnchor;
1 change: 1 addition & 0 deletions src/Spinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ const Spinner = React.forwardRef(
);

Spinner.propTypes = propTypes;
Spinner.displayName = 'Spinner';

export default Spinner;
1 change: 1 addition & 0 deletions src/SplitButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ const SplitButton = React.forwardRef(

SplitButton.propTypes = propTypes;
SplitButton.defaultProps = defaultProps;
SplitButton.displayName = 'SplitButton';

export default SplitButton;
35 changes: 18 additions & 17 deletions src/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,25 @@ function getDefaultActiveKey(children) {
return defaultActiveKey;
}

const Tabs = React.forwardRef((props, ref) => {
const renderTab = child => {
const { title, eventKey, disabled, tabClassName } = child.props;
if (title == null) {
return null;
}
function renderTab(child) {
const { title, eventKey, disabled, tabClassName } = child.props;
if (title == null) {
return null;
}

return (
<NavItem
as={NavLink}
eventKey={eventKey}
disabled={disabled}
className={tabClassName}
>
{title}
</NavItem>
);
};
return (
<NavItem
as={NavLink}
eventKey={eventKey}
disabled={disabled}
className={tabClassName}
>
{title}
</NavItem>
);
}

const Tabs = React.forwardRef((props, ref) => {
const {
id,
onSelect,
Expand Down Expand Up @@ -156,5 +156,6 @@ const Tabs = React.forwardRef((props, ref) => {

Tabs.propTypes = propTypes;
Tabs.defaultProps = defaultProps;
Tabs.displayName = 'Tabs';

export default Tabs;
34 changes: 19 additions & 15 deletions src/Toast.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useMemo, useCallback } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

import useTimeout from '@restart/hooks/useTimeout';
import Fade from './Fade';
import Header from './ToastHeader';
import Body from './ToastBody';
Expand Down Expand Up @@ -76,26 +77,28 @@ const Toast = React.forwardRef(
const delayRef = useRef(delay);
const onCloseRef = useRef(onClose);

// We use refs for these, because we don't want to restart the autohide
// timer in case these values change.
delayRef.current = delay;
onCloseRef.current = onClose;

useEffect(() => {
// We use refs for these, because we don't want to restart the autohide
// timer in case these values change.
delayRef.current = delay;
onCloseRef.current = onClose;
}, [delay, onClose]);

const autohideTimeout = useTimeout();
const autohideFunc = useCallback(() => {
if (!(autohide && show)) {
return undefined;
return;
}
onCloseRef.current();
}, [autohide, show]);

const autohideHandle = setTimeout(() => {
onCloseRef.current();
}, delayRef.current);
autohideTimeout.set(autohideFunc, delayRef.current);

return () => {
clearTimeout(autohideHandle);
};
}, [autohide, show]);
const useAnimation = useMemo(() => Transition && animation, [
Transition,
animation,
]);

const useAnimation = Transition && animation;
const toast = (
<div
{...props}
Expand Down Expand Up @@ -127,6 +130,7 @@ const Toast = React.forwardRef(

Toast.propTypes = propTypes;
Toast.defaultProps = defaultProps;
Toast.displayName = 'Toast';

Toast.Body = Body;
Toast.Header = Header;
Expand Down
1 change: 1 addition & 0 deletions src/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@ const Tooltip = React.forwardRef(

Tooltip.propTypes = propTypes;
Tooltip.defaultProps = defaultProps;
Tooltip.displayName = 'Tooltip';

export default Tooltip;
7 changes: 7 additions & 0 deletions test/DropdownItemSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ describe('<Dropdown.Item>', () => {
.should.not.have.property('onSelect');
});

it('does not pass onSelect to children', () => {
mount(<Dropdown.Item onSelect={() => {}}>Item</Dropdown.Item>)
.find('SafeAnchor')
.props()
.should.not.have.property('onSelect');
});

it('disabled link', () => {
const handleSelect = () => {
throw new Error('Should not invoke onSelect event');
Expand Down

0 comments on commit c3fab88

Please sign in to comment.