forked from folio-org/stripes-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntrySelector.js
179 lines (160 loc) · 4.38 KB
/
EntrySelector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import Switch from 'react-router-dom/Switch';
import Route from 'react-router-dom/Route';
import { FormattedMessage, intlShape } from 'react-intl';
import injectIntl from '../InjectIntl';
import IconButton from '../IconButton';
import Paneset from '../Paneset';
import Pane from '../Pane';
import PaneMenu from '../PaneMenu';
import Button from '../Button';
import NavList from '../NavList';
import NavListSection from '../NavListSection';
import NavListItem from '../NavListItem';
class EntrySelector extends React.Component {
static propTypes = {
addButtonTitle: PropTypes.string,
addMenu: PropTypes.node,
children: PropTypes.node,
contentData: PropTypes.arrayOf(
PropTypes.object,
).isRequired,
detailComponent: PropTypes.func.isRequired,
detailPaneTitle: PropTypes.string,
editable: PropTypes.bool,
history: PropTypes.shape({
push: PropTypes.func.isRequired,
}).isRequired,
intl: intlShape.isRequired,
location: PropTypes.object.isRequired,
match: PropTypes.shape({
path: PropTypes.string.isRequired,
}).isRequired,
nameKey: PropTypes.string,
onAdd: PropTypes.func,
onClick: PropTypes.func,
onEdit: PropTypes.func,
paneSetWidth: PropTypes.string,
paneTitle: PropTypes.string,
paneWidth: PropTypes.string.isRequired,
parentMutator: PropTypes.object.isRequired,
resources: PropTypes.object.isRequired,
stripes: PropTypes.object,
};
static defaultProps = {
editable: true,
nameKey: 'name',
paneSetWidth: 'fill',
};
constructor(props) {
super(props);
this.activeLink = this.activeLink.bind(this);
this.linkPath = this.linkPath.bind(this);
this.onClose = this.onClose.bind(this);
this.onClick = this.onClick.bind(this);
}
linkPath(id) {
return `${this.props.match.path}/${id}`;
}
activeLink(links) {
return this.props.location.pathname || this.linkPath(links[0].key);
}
onClose() {
this.props.history.push(`${this.props.match.path}`);
}
onClick(item) {
if (this.props.onClick) {
this.props.onClick(item);
}
}
renderDetail(item) {
const {
detailPaneTitle,
stripes,
intl,
paneWidth,
detailComponent,
parentMutator,
editable,
resources
} = this.props;
const ComponentToRender = detailComponent;
const lastMenu = (
<IconButton
icon="edit"
onClick={() => this.props.onEdit(item)}
id="clickable-edit-item"
ariaLabel="edit"
title={intl.formatMessage({ id: 'stripes-components.button.edit' })}
size="medium"
/>
);
return (
<Pane
paneTitle={detailPaneTitle}
{...editable ? { lastMenu } : {}}
defaultWidth={paneWidth}
dismissible
onClose={this.onClose}
>
<ComponentToRender
stripes={stripes}
parentResources={resources}
initialValues={item}
parentMutator={parentMutator}
/>
</Pane>
);
}
render() {
const {
addButtonTitle,
contentData,
paneTitle,
nameKey,
addMenu,
paneSetWidth
} = this.props;
const links = _.sortBy(contentData, [nameKey]).map(item => (
<NavListItem
key={item.id}
to={this.linkPath(item.id)}
onClick={() => this.props.onClick(item)}
>
{item[nameKey] || '[unnamed]'}
</NavListItem>
));
const routes = contentData.map(item => (
<Route
key={item.id}
path={this.linkPath(item.id)}
render={() => this.renderDetail(item)}
/>
));
const LastMenu = addMenu || (
<PaneMenu>
<Button title={addButtonTitle} onClick={this.props.onAdd} buttonStyle="primary paneHeaderNewButton">
<FormattedMessage id="stripes-components.button.new" />
</Button>
</PaneMenu>
);
return (
<Paneset defaultWidth={paneSetWidth}>
<Pane defaultWidth="fill" lastMenu={LastMenu} paneTitle={paneTitle} noOverflow>
<NavList>
<NavListSection activeLink={this.activeLink(links)}>
{links}
</NavListSection>
</NavList>
</Pane>
<Switch>
{routes}
</Switch>
{this.props.children}
</Paneset>
);
}
}
export default injectIntl(EntrySelector);