Skip to content

Commit 2d476c4

Browse files
committed
new List api, tests, docs
1 parent 3b44927 commit 2d476c4

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed

.babelrc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"@babel/stage-3"
88
],
99
"plugins": [
10-
["@babel/transform-runtime", { "polyfill": false, "useBuiltIns": true, "useESModules": true }]
10+
["@babel/plugin-transform-runtime", { "polyfill": false, "useBuiltIns": true, "useESModules": true }]
1111
]
1212
},
1313
"commonjs": {
@@ -17,7 +17,7 @@
1717
"@babel/stage-3"
1818
],
1919
"plugins": [
20-
["@babel/transform-runtime", { "polyfill": false, "useBuiltIns": true }]
20+
["@babel/plugin-transform-runtime", { "polyfill": false, "useBuiltIns": true }]
2121
]
2222
},
2323
"test": {
@@ -26,8 +26,8 @@
2626
"@babel/stage-3"
2727
],
2828
"plugins": [
29-
["@babel/transform-runtime", { "polyfill": false, "useBuiltIns": true }],
30-
"@babel/transform-modules-commonjs"
29+
["@babel/plugin-transform-runtime", { "polyfill": false, "useBuiltIns": true }],
30+
"@babel/plugin-transform-modules-commonjs"
3131
]
3232
}
3333
}

docs/components/List.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,25 @@ import { List } from 'react-powerplug'
2727
Specifies the initial list state, must be an array.
2828
By default, the initial list state is an empty array.
2929

30+
**onChange** _(optional)_
31+
Callback that fires when state changes.
32+
3033
## List Children Props
3134

32-
TL;DR: `{ list, push, pull, sort, setList }`
35+
TL;DR: `{ list, first, last, push, pull, sort, set }`
3336

3437
**list**
3538
`boolean`
3639
Your list state
3740

41+
**first**
42+
`() => any`
43+
Get first element of your list array
44+
45+
**last**
46+
`() => any`
47+
Get last element of your list array
48+
3849
**push**
3950
`(item: any) => void`
4051
Add an item to your list array
@@ -54,17 +65,21 @@ pull(item => item.name === 'Renato')
5465
`(comparator: (a: item, b: item) => number)) => void`
5566
Use [Array.prototype.sort](https://www.w3schools.com/jsref/jsref_sort.asp) to sort your list state.
5667

57-
**setList**
58-
`(list: array) => void`
68+
**set**
69+
`(list: array | (list: array) => array) => void`
5970
Set a new full list to your state
6071
Note: use it to handle array in the way you prefer
6172

6273
```js
6374
import R from 'ramda'
6475

76+
set([1, 2, 3, 4])
77+
set(list => [...list, 5, 6])
6578
// list = [1, 2, 3, 4, 5, 6]
66-
setList(R.drop(2, list)) // remove two first items
79+
80+
set(R.drop(2)) // remove two first items
6781
// list = [3, 4, 5, 6]
68-
setList(R.dropLast(2, list)) // remove two last items
82+
83+
set(R.dropLast(2)) // remove two last items
6984
// list = [3, 4]
7085
```

docs/components/Toggle.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Toggle } from 'react-powerplug'
1818
Specifies the initial on state, must be an boolean.
1919
By default, the initial on state is false.
2020

21-
**onChange** _(optional)_
21+
**onChange** _(optional)_
2222
Callback that fires when state changes.
2323

2424
## Toggle Children Props

src/components/List.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react'
22
import State from './State'
33
import renderProps from '../utils/renderProps'
4+
import set from '../utils/set'
45

56
const complement = fn => (...args) => !fn(...args)
67

@@ -11,14 +12,12 @@ const List = ({ initial = [], onChange, ...props }) => (
1112
list: state.list,
1213
first: () => state.list[0],
1314
last: () => state.list[Math.max(0, state.list.length - 1)],
14-
setList: list => setState({ list }),
15-
push: value => setState(({ list }) => ({ list: [...list, value] })),
15+
set: list => setState(s => ({ list: set(list, s.list) })),
16+
push: value => setState(s => ({ list: [...s.list, value] })),
1617
pull: predicate =>
17-
setState(({ list }) => ({
18-
list: list.filter(complement(predicate)),
19-
})),
18+
setState(s => ({ list: s.list.filter(complement(predicate)) })),
2019
sort: compareFn =>
21-
setState(({ list }) => ({ list: [...list].sort(compareFn) })),
20+
setState(s => ({ list: [...s.list].sort(compareFn) })),
2221
})
2322
}
2423
</State>

tests/components/List.test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ test('<List />', () => {
1414
lastCalled().push(8)
1515
expect(lastCalled().list).toEqual([1, 8])
1616

17-
lastCalled().setList([5, 4, 1, 6, 9])
18-
expect(lastCalled().list).toEqual([5, 4, 1, 6, 9])
17+
lastCalled().set([9, 2, 3, 4])
18+
expect(lastCalled().list).toEqual([9, 2, 3, 4])
19+
20+
lastCalled().set(list => [...list, 5])
21+
expect(lastCalled().list).toEqual([9, 2, 3, 4, 5])
1922

2023
const listBeforeSort = lastCalled().list
2124
lastCalled().sort()
22-
expect(lastCalled().list).toEqual([1, 4, 5, 6, 9])
23-
expect(listBeforeSort).toEqual([5, 4, 1, 6, 9])
25+
expect(lastCalled().list).toEqual([2, 3, 4, 5, 9])
26+
expect(listBeforeSort).toEqual([9, 2, 3, 4, 5])
2427

2528
lastCalled().pull(d => d % 2)
26-
expect(lastCalled().list).toEqual([4, 6])
29+
expect(lastCalled().list).toEqual([2, 4])
30+
31+
expect(lastCalled().first()).toEqual(2)
32+
expect(lastCalled().last()).toEqual(4)
2733

28-
expect(lastCalled().first()).toEqual(4)
29-
expect(lastCalled().last()).toEqual(6)
30-
lastCalled().setList([])
34+
lastCalled().set([])
3135
expect(lastCalled().first()).toEqual(undefined)
3236
expect(lastCalled().last()).toEqual(undefined)
3337
})

0 commit comments

Comments
 (0)