Skip to content

Commit 5eee170

Browse files
author
Jonas Gossens
committed
✨ Add open prop to listItem to control it
1 parent a015b73 commit 5eee170

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

examples/react-chayns-list/Example.jsx

+57-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import React, { Component } from 'react';
22

33
import { ContextMenu, List, ListItem } from '../../src/index';
4+
import Button from '../../src/react-chayns-button/component/Button';
45

56
export default class ListExample extends Component {
7+
constructor(props) {
8+
super(props);
9+
this.state = { open: false };
10+
}
11+
612
render() {
7-
return(
13+
const { open } = this.state;
14+
return (
815
<div>
916
<List>
1017
<ListItem
@@ -65,11 +72,49 @@ export default class ListExample extends Component {
6572
items={[{
6673
icon: 'ts-plus',
6774
onClick: () => chayns.dialog.alert('Create'),
68-
text: 'Create'
75+
text: 'Create',
76+
}, {
77+
icon: 'ts-trash',
78+
onClick: () => chayns.dialog.alert('Delete'),
79+
text: 'Delete',
80+
}]}
81+
/>
82+
</div>
83+
)}
84+
>
85+
{`
86+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
87+
labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores
88+
et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
89+
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
90+
labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores
91+
et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
92+
`}
93+
</ListItem>
94+
<ListItem
95+
onClick={() => { this.setState({ open: !open }); }}
96+
open={open}
97+
title="ListItem (button controlled)"
98+
subtitle="Description"
99+
image="https://chayns.tobit.com/storage/70231-10288/Images/icon-72.png"
100+
right={(
101+
<div
102+
style={{
103+
height: '100%',
104+
display: 'flex',
105+
alignItems: 'center',
106+
}}
107+
onClick={e => e.stopPropagation()}
108+
>
109+
<ContextMenu
110+
items={[{
111+
icon: 'ts-plus',
112+
onClick: () => chayns.dialog.alert('Create'),
113+
text: 'Create',
69114
}, {
70115
icon: 'ts-trash',
71116
onClick: () => chayns.dialog.alert('Delete'),
72-
text: 'Delete'
117+
text: 'Delete',
73118
}]}
74119
/>
75120
</div>
@@ -85,6 +130,15 @@ export default class ListExample extends Component {
85130
`}
86131
</ListItem>
87132
</List>
133+
<Button
134+
onClick={() => {
135+
this.setState({ open: !open });
136+
}}
137+
>
138+
139+
140+
Open/Close
141+
</Button>
88142
</div>
89143
);
90144
}

src/react-chayns-list/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ You can use the List like this:
5151
| subtitle | Second title (subtitle) that should be rendered on the item | String | |
5252
| image | Image that should be rendered to the left of the title | String | |
5353
| className | ClassName that should be set on the ListItem root element | String | |
54-
| onClick | onClick-Listener that should be set on the item (only when no children is specified) | Function | |
54+
| onClick | onClick-Listener that should be set on the item | Function | |
5555
| children | Children that should be shown, when clicked on the item (works only inside Lists that does not have the notExpandable-prop set) | React-Elements | |
5656
| right | React-Elements that should be rendered on the right side of the ListItem | React-Elements | |
5757
| noContentClass | Prevents adding the "list-item__content"-Class to the children wrapper. | bool | false |
58+
| open | Opening state of the item. true=open, false=close, null=uncontrolled | bool | false |
5859

5960
## Example ##
6061

src/react-chayns-list/component/ExpandableList/AbstractExpandableListItem.jsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ExpandableContent from '../../../react-chayns-expandable_content/componen
77
import ExpandableContext from './ExpandableContext';
88

99
let maxId = 1;
10+
1011
function getId() {
1112
const id = maxId;
1213
maxId += 1;
@@ -28,6 +29,7 @@ class AbstractExpandableListItem extends PureComponent {
2829
className: PropTypes.string,
2930
clickable: PropTypes.bool,
3031
noContentClass: PropTypes.bool,
32+
openProp: PropTypes.bool,
3133
};
3234

3335
static defaultProps = {
@@ -36,6 +38,7 @@ class AbstractExpandableListItem extends PureComponent {
3638
className: '',
3739
clickable: false,
3840
noContentClass: false,
41+
openProp: null,
3942
};
4043

4144
constructor(props) {
@@ -89,9 +92,10 @@ class AbstractExpandableListItem extends PureComponent {
8992
clickable,
9093
open: openIds,
9194
noContentClass,
95+
openProp,
9296
} = this.props;
9397

94-
const open = (openIds && openIds.indexOf && openIds.indexOf(this.id) !== -1);
98+
const open = openProp !== null ? openProp : (openIds && openIds.indexOf && openIds.indexOf(this.id) !== -1);
9599

96100
return (
97101
<div

src/react-chayns-list/component/ListItem/ExpandableListItem.jsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import ExpandableListHeader from './ExpandableListHeader';
66
import ExpandableList from '../ExpandableList/ExpandableList';
77

88
const ExpandableListItem = ({ children, ...props }) => {
9-
const { noContentClass } = props;
9+
const {
10+
noContentClass, open,
11+
} = props;
12+
1013
return (
1114
<AbstractExpandableListItem
1215
noContentClass={noContentClass}
@@ -21,6 +24,7 @@ const ExpandableListItem = ({ children, ...props }) => {
2124
</ExpandableList.Context.Consumer>
2225
)}
2326
clickable
27+
openProp={open}
2428
>
2529
{children}
2630
</AbstractExpandableListItem>
@@ -33,10 +37,12 @@ ExpandableListItem.propTypes = {
3337
PropTypes.arrayOf(PropTypes.node),
3438
]).isRequired,
3539
noContentClass: PropTypes.bool,
40+
open: PropTypes.bool,
3641
};
3742

3843
ExpandableListItem.defaultProps = {
3944
noContentClass: false,
45+
open: null,
4046
};
4147

4248
export default ExpandableListItem;

0 commit comments

Comments
 (0)