Skip to content

Commit 0a41a32

Browse files
committed
feat(component): accept any type value
1 parent 383e1e1 commit 0a41a32

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@ Install with [npm](https://www.npmjs.com/):
1010

1111
## Usage
1212

13+
Put `<YourComponent />` into `<TogglePattern />`.
14+
15+
```js
16+
class ToggleButton extends React.Component {
17+
render(){
18+
return (
19+
<TogglePattern isEditing={this.props.isEditing}>
20+
<LeaveEditingButton isEditing={true} />
21+
<EnterEditingButton isEditing={false} />
22+
</TogglePattern>
23+
);
24+
}
25+
}
26+
```
27+
28+
It means that
29+
30+
- if `this.props.isEditing` is `true`, show `<LeaveEditingButton />`
31+
- if `this.props.isEditing` is `false`, show `<EnterEditingButton />`
32+
- In the other case, show `null`
33+
34+
### `<TogglePattern />` Interface
35+
36+
- `<TogglePattern anyAttribute=anyValue />`
37+
- `anyAttribute` is any name.
38+
- `anyValue` is any type.
39+
40+
### Example
41+
1342
Show component that has truly attribute with `<TogglePattern attribute />`
1443

1544
```js
@@ -36,6 +65,17 @@ Show component that match attribute and value with `<TogglePattern attribute=val
3665

3766
Result to `<ComponentY />`
3867

68+
Also, it is ok that `value` it `string` type.
69+
70+
```js
71+
<TogglePattern pattern="one">
72+
<ComponentX pattern="one"/>
73+
<ComponentY pattern="two"/>
74+
</TogglePattern>
75+
```
76+
77+
Result to `<ComponentY />`
78+
3979
-----
4080

4181
Show component**s** that match attribute and value with `<TogglePattern attribute=value />`.
@@ -62,6 +102,8 @@ Not show when not match
62102

63103
Result to `null`.
64104

105+
106+
65107
## Changelog
66108

67109
See [Releases page](https://github.com/azu/react-toggle-pattern/releases).

src/react-toggle-pattern.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import React, {Component, PropTypes} from 'react';
44

55
export default class TogglePattern extends Component {
66
getFlagNames() {
7-
const keys = Object.keys(this.props);
8-
return keys.filter(key => {
9-
const flag = this.props[key];
10-
return typeof flag === "boolean";
11-
});
7+
return Object.keys(this.props);
128
}
139

1410
getUnMatchedComponent() {

test/react-toggle-pattern-test.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,20 @@ describe('<TogglePattern />', () => {
5050
const result = wrapper.find(ComponentX);
5151
assert(result.length === 0);
5252
});
53-
});
53+
54+
it('match any type value', () => {
55+
const wrapper = shallow(<TogglePattern pattern="one">
56+
<ComponentX pattern="one"/>
57+
<ComponentY pattern="two"/>
58+
</TogglePattern>);
59+
assert(wrapper.is(ComponentX));
60+
61+
const symbol = {};
62+
const wrapper1 = shallow(<TogglePattern pattern={symbol}>
63+
<ComponentX pattern={symbol}/>
64+
<ComponentY pattern="two"/>
65+
</TogglePattern>);
66+
assert(wrapper1.is(ComponentX));
67+
68+
});
69+
});

0 commit comments

Comments
 (0)