Skip to content

Commit c165e35

Browse files
committed
Loop
1 parent be67666 commit c165e35

5 files changed

Lines changed: 49 additions & 48 deletions

File tree

docs/documentation/pseudo-elements.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: pseudo-elements
33
title: Pseudo Elements
44
---
55

6-
When looping through components, spread the `loop` method provided by ReactCSS and call it with the iterator to pass down list-specific props, like: `first-child` `last-child` `even` `odd` `nth-child-*`
6+
When looping through components, spread the `loop` method provided by ReactCSS and call it with the iterator and length of items to pass down list-specific props, like: `first-child` `last-child` `even` `odd` `nth-child-*`
77

88
```
99
import React from 'react'
@@ -13,7 +13,12 @@ const List = () => {
1313
return (
1414
<div>
1515
{ this.props.items((item, i) => {
16-
return <ListItem { ...item } { ...loop(i) } />
16+
return (
17+
<ListItem
18+
{ ...item }
19+
{ ...loop(i, this.props.items.length) }
20+
/>
21+
)
1722
}) }
1823
</div>
1924
)

src/loopable.js renamed to src/loop.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
const loopable = (i, length) => {
44
const props = {}
5-
const setProp = (name, value) => {
6-
props[name] = value != null ? value : true
5+
const setProp = (name, value = true) => {
6+
props[name] = value
77
}
88

9-
i === 0 && setProp('first')
10-
i === length - 1 && setProp('last');
9+
i === 0 && setProp('first-child')
10+
i === length - 1 && setProp('last-child');
1111
(i === 0 || i % 2 === 0) && setProp('even')
1212
Math.abs(i % 2) === 1 && setProp('odd')
13-
setProp('child', i)
13+
setProp('nth-child', i)
1414

1515
return props
1616
}

src/reactcss.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
'use strict'
22

3-
import Component from './deprecated/Component'
4-
export hover from './components/hover.js'
53
import flattenNames from './flattenNames'
64
import mergeClasses from './mergeClasses'
75
import autoprefix from './autoprefix'
86

7+
export Component from './deprecated/Component'
8+
export hover from './components/hover'
9+
export loop from './loop'
10+
911
export const ReactCSS = (classes, ...activations) => {
1012
const activeNames = flattenNames(activations)
1113
const merged = mergeClasses(classes, activeNames)
1214
return autoprefix(merged)
1315
}
1416

15-
ReactCSS.Component = Component
1617
ReactCSS.m = Object.assign
1718

1819
export default ReactCSS

test/loop.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* global describe, it */
2+
/* eslint react/prefer-stateless-function: 0 */
3+
4+
import { React, TestUtils, expect } from './helpers'
5+
import loopable from '../src/loop'
6+
7+
describe('Loopable', () => {
8+
it('should return first-child if first', () => {
9+
expect(loopable(0, 4)).to.eql({ 'first-child': true, 'nth-child': 0, 'even': true })
10+
})
11+
12+
it('should return last-child if last', () => {
13+
expect(loopable(3, 4)).to.eql({ 'last-child': true, 'nth-child': 3, 'odd': true })
14+
})
15+
16+
it('should return even if even', () => {
17+
expect(loopable(2, 4)).to.eql({ 'even': true, 'nth-child': 2 })
18+
})
19+
20+
it('should return odd if odd', () => {
21+
expect(loopable(1, 4)).to.eql({ 'odd': true, 'nth-child': 1 })
22+
})
23+
24+
it('should return simple css', () => {
25+
class Component extends React.Component {
26+
render() {
27+
return <div className="body" />
28+
}
29+
}
30+
const component = TestUtils.renderIntoDocument(<Component { ...loopable(3, 4) } />)
31+
expect(component.props).to.eql({ 'last-child': true, 'nth-child': 3, 'odd': true })
32+
})
33+
})

test/loopable.test.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)