Skip to content

Commit

Permalink
Fix: Slot onClick passes correct ID (#972)
Browse files Browse the repository at this point in the history
Fixes #971
  • Loading branch information
kidkuro authored and sapegin committed May 15, 2018
1 parent 7f5137e commit f5b0057
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/rsg-components/Slot/Slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ export default function Slot({ name, active, onlyActive, className, props = {} }
const rendered = fills.map((Fill, index) => {
// { id: 'pizza', render: ({ foo }) => <div>{foo}</div> }
const { id, render } = Fill;
let fillProps = props;
if (id && render) {
// Render only specified fill
if (onlyActive && id !== active) {
return null;
}

const { onClick } = props;
props = {
fillProps = {
...props,
name: id,
// Set active prop to active fill
Expand All @@ -32,7 +33,7 @@ export default function Slot({ name, active, onlyActive, className, props = {} }
Fill = render;
}

return <Fill key={index} {...props} />;
return <Fill key={index} {...fillProps} />;
});

const filtered = rendered.filter(Boolean);
Expand Down
17 changes: 10 additions & 7 deletions src/rsg-components/Slot/Slot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import Slot from './Slot';

/* eslint-disable react/prop-types */

const Button = ({ onClick }) => <button onClick={onClick}>1</button>;
const Button2 = () => <button>2</button>;
const Button = ({ name, onClick, children }) => (
<button name={name} onClick={onClick}>
{children}
</button>
);

const Button2 = props => <Button {...props}>2</Button>;

const fillsWithIds = [
{
id: 'one',
Expand Down Expand Up @@ -74,12 +80,9 @@ it('should pass slot ID to onClick handler', () => {
},
});

actual
.find('button')
.first()
.simulate('click');
actual.find('button[name="two"]').simulate('click');

expect(onClick).toBeCalledWith('one', expect.any(Object));
expect(onClick).toBeCalledWith('two', expect.any(Object));
});

it('should return null if all slots render null', () => {
Expand Down

0 comments on commit f5b0057

Please sign in to comment.