Skip to content

Commit 3b44927

Browse files
gnapserenatorib
authored andcommitted
Change compose to pass list of props instead of object (#51)
* Change compose to pass list of props instead of object * Restore markdown trailing whitespace * Restore markdown trailing whitespace * linebreak
1 parent 36e746f commit 3b44927

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ import { compose } from 'react-powerplug'
308308
const ToggleCounter = compose(Toggle, Counter)
309309

310310
<ToggleCounter>
311-
{({ toggle: { toggle, on }, counter: { count, inc, dec } }) => (
311+
{({ toggle, on }, { count, inc, dec }) => (
312312
<ProductCard
313313
{...productInfo}
314314
isFavorited={on}
@@ -339,7 +339,7 @@ Also, you can use a built-in Compose component and pass components on `states` p
339339
import { Compose } from 'react-powerplug'
340340

341341
<Compose states={[Toggle, Counter]}>
342-
{({ toggle, counter }) => (
342+
{(toggle, counter) => (
343343
<ProductCard {...} />
344344
)}
345345
</Compose>

docs/components/Compose.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { Compose, Toggle, Counter } from 'react-powerplug'
88

99
```jsx
1010
<Compose states={[<Counter />, <Toggle initial={false} />]}>
11-
{({ count, inc, dec, on, toggle }) => (
11+
{(counter, toggle) => (
1212
<ProductCard
1313
{...productInfo}
14-
isFavorited={on}
15-
onFavorite={toggle}
16-
count={count}
17-
onAdd={inc}
18-
onRemove={dec}
14+
isFavorited={toggle.on}
15+
onFavorite={toggle.toggle}
16+
count={counter.count}
17+
onAdd={counter.inc}
18+
onRemove={counter.dec}
1919
/>
2020
)}
2121
</Compose>
@@ -34,7 +34,7 @@ const ToggleCounter = compose(
3434

3535
```jsx
3636
<ToggleCounter>
37-
{({ count, inc, dec, on, toggle }) => (...)}
37+
{(counter, toggle) => (...)}
3838
</ToggleCounter>
3939
```
4040

@@ -48,10 +48,8 @@ need to pass an array, just pass by arguments: `compose(<Foo />, <Bar />, <Baz /
4848

4949
## Compose Children Props
5050

51-
Depends on your choices.
52-
Will be all children props merged from your passed components.
51+
The render props function provided will receive n arguments, each of them being
52+
the arguments provided by the corresponding component in the list.
53+
5354

54-
## Known Issues
5555

56-
React PowerPlug still does not deal with name conflicts.
57-
Sorry fot that, but PRs are welcome!

src/utils/compose.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const compose = (...elements) => {
1010
// Stack children arguments recursively and pass
1111
// it down until the last component that render children
1212
// with these stacked arguments
13-
function stackProps(i, elements, stacked = {}) {
13+
function stackProps(i, elements, propsList = []) {
1414
const element = elements[i]
1515
const isTheLast = i === 0
1616

@@ -19,8 +19,8 @@ const compose = (...elements) => {
1919
// Otherwise continue stacking arguments
2020
const renderFn = props =>
2121
isTheLast
22-
? renderProps(composedProps, { [name(element)]: props, ...stacked })
23-
: stackProps(i - 1, elements, { [name(element)]: props, ...stacked })
22+
? renderProps(composedProps, ...propsList.concat(props))
23+
: stackProps(i - 1, elements, propsList.concat(props))
2424

2525
// Clone a element if it's passed created as <Element initial={} />
2626
// Or create it if passed as just Element

src/utils/renderProps.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import warn from './warn'
22

3-
const isFn = (prop) => typeof prop === 'function'
3+
const isFn = prop => typeof prop === 'function'
44

55
/**
66
* renderProps
@@ -9,19 +9,18 @@ const isFn = (prop) => typeof prop === 'function'
99
* or children if both are used
1010
*/
1111

12-
const renderProps = ({ children, render }, props) => {
12+
const renderProps = ({ children, render }, ...props) => {
1313
if (process.env.NODE_ENV !== 'production') {
14-
warn(isFn(children) && isFn(render),
14+
warn(
15+
isFn(children) && isFn(render),
1516
'You are using the children and render props together.\n' +
16-
'This is impossible, therefore, only the children will be used.'
17+
'This is impossible, therefore, only the children will be used.'
1718
)
1819
}
1920

20-
const fn = isFn(children)
21-
? children
22-
: render
21+
const fn = isFn(children) ? children : render
2322

24-
return fn ? fn(props) : null
23+
return fn ? fn(...props) : null
2524
}
2625

2726
export default renderProps

0 commit comments

Comments
 (0)