/
index.jsx
299 lines (270 loc) · 8.99 KB
/
index.jsx
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */
/* eslint-disable no-script-url */
// # Filter
// Implements part of the [Panel design pattern](https://www.lightningdesignsystem.com/components/panels) in React.
// Based on SLDS v2.2.0-rc.1
// ## Dependencies
// ### React
import React from 'react';
import createReactClass from 'create-react-class';
import PropTypes from 'prop-types';
// ### assign
import assign from 'lodash.assign';
// ### classNames
import classNames from 'classnames';
// ### shortid
// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid)
// shortid is a short, non-sequential, url-friendly, unique id generator
import shortid from 'shortid';
// This component's `checkProps` which issues warnings to developers about properties when in development mode (similar to React's built in development tools)
import checkProps from './check-props';
import Button from '../button';
import Popover from '../popover';
// ## Constants
import { FILTER } from '../../utilities/constants';
/**
* A Filter is a popover with custom trigger. It can be used by [Panel Filtering](/components/panels/). Menus within a Filter Popover will need to not have "portal mounts" and be inline.
*/
const Filter = createReactClass({
displayName: FILTER,
propTypes: {
/**
* Aligns the popover with the respective side of the trigger. That is `left` will place the `Popover` to the left of the Filter.
*/
align: PropTypes.oneOf(['left', 'right']),
/**
* **Assistive text for accessibility**
* * `removeFilter`: Assistive text for removing a filter. The default is `Remove Filter: this.props.property this.props.predicate`.
* * `editFilter`: Assistive text for changing a filter.
* * `editFilterHeading`: Assistive text for Popover heading.
*/
assistiveText: PropTypes.shape({
editFilter: PropTypes.string,
editFilterHeading: PropTypes.string,
removeFilter: PropTypes.string,
}),
/**
* Contents of popover. That is the dropdowns and inputs that set the filter criteria.
*/
children: PropTypes.node,
/**
* Custom CSS classes for `slds-filters__item` node. Uses `classNames` [API](https://github.com/JedWatson/classnames).
*/
className: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
PropTypes.string,
]),
/**
* Applies error state styling. Per filter error messages are outside this components.
*/
isError: PropTypes.bool,
/**
* A unique ID is needed in order to support keyboard navigation, ARIA support, and connect the dropdown to the triggering button. An `id` will be generated if none is supplied.
*/
id: PropTypes.string,
/**
* If true, the filter will not display an editing popover when clicked.
*/
isLocked: PropTypes.bool,
/**
* Applies new filter styling.
*/
isNew: PropTypes.bool,
/**
* If true, the filter will not include a remove button.
*/
isPermanent: PropTypes.bool,
/**
* Will be triggered when Done within the Popover is clicked. This is the place to update the filter props displayed. Callback will recieve parameters: `clickEvent, { id }`. An index into your store may be a good setting for `id`, so that it will be passed back here.
*/
onChange: PropTypes.func,
/**
* Will be triggered when "Remove Filter" button is clicked. Callback will recieve parameters: `clickEvent, { id }`. An index into your store may be a good setting for `id`, so that it will be passed back here.
*/
onRemove: PropTypes.func,
/**
* Will be triggered when Filter is clicked. This is the place to close/open popover if a custom popover is passed in
*/
onClick: PropTypes.func,
/**
* A `Popover` component. The props from this popover will be merged and override any default props. This also allows a Filter's Popover dialog to be a controlled component. _Tested with Mocha framework._
*/
popover: PropTypes.node,
/**
* The criteria you are filtering for. For instance, if "Hair Color is PURPLE" is your filter, "is PURPLE" is your filter predicate.
*/
predicate: PropTypes.node,
/**
* The property you are filtering. For instance, if "Hair Color is PURPLE" is your filter, "Hair Color" is your filter property.
*/
property: PropTypes.node,
},
getDefaultProps () {
return {
align: 'left',
assistiveText: {
editFilter: 'Edit filter:',
editFilterHeading: 'Choose filter criteria',
},
predicate: 'New Filter',
};
},
getInitialState () {
return {
popoverIsOpen: this.props.popover
? this.props.popover.props.isOpen
: false,
};
},
componentWillMount () {
this.generatedId = shortid.generate();
checkProps(FILTER);
},
getId () {
return this.props.id || this.generatedId;
},
getCustomPopoverProps ({ assistiveText }) {
/*
* Generate the popover props based on passed in popover props. Using the default behavior if not provided by passed in popover
*/
const popoverBody = (
<div>
<h4
className="slds-assistive-text"
id={`${this.getId()}-popover-heading`}
>
{assistiveText.editFilterHeading}
</h4>
{this.props.children}
<div className="slds-m-top--small slds-text-align--right">
<Button
className="slds-col--bump-left"
label="Done"
onClick={this.handleChange}
/>
</div>
</div>
);
const defaultPopoverProps = {
ariaLabelledby: `${this.getId()}-popover-heading`,
align: this.props.align,
body: popoverBody,
heading: '',
id: this.getId(),
isOpen: this.state.popoverIsOpen,
// MAGIC NUMBERS - REMOVE/REDESIGN WHEN DESIGN FOR RIGHT-ALIGNED FILTERS ARE ADDED TO SLDS
offset: this.props.align === 'right' ? '0px -35px' : undefined,
onClose: this.handleClose,
onRequestClose: this.handleClose,
position: 'overflowBoundaryElement',
triggerClassName: 'slds-grow',
};
/* Mixin passed popover's props if there is any to override the default popover props */
const popoverProps = assign(
defaultPopoverProps,
this.props.popover ? this.props.popover.props : {}
);
delete popoverProps.children;
return popoverProps;
},
handleFilterClick () {
this.setState({ popoverIsOpen: true });
if (this.props.onClick) {
this.props.onClick();
}
},
handleClose () {
this.setState({ popoverIsOpen: false });
},
handleChange (event) {
this.setState({ popoverIsOpen: false });
if (this.props.onChange) {
this.props.onChange(event, { id: this.getId() });
}
},
handleRemove (event) {
if (this.props.onRemove) {
this.props.onRemove(event, { id: this.getId() });
}
},
render () {
/* Remove at next breaking change */
const assistiveText = {
editFilter:
this.props.assistiveTextEditFilter || // eslint-disable-line react/prop-types
this.props.assistiveText.editFilter,
editFilterHeading:
this.props.assistiveTextEditFilterHeading || // eslint-disable-line react/prop-types
this.props.assistiveText.editFilterHeading,
removeFilter:
this.props.assistiveTextRemoveFilter || // eslint-disable-line react/prop-types
this.props.assistiveText.removeFilter ||
`Remove Filter: ${this.props.property} ${this.props.predicate}`,
};
/* TODO: Button wrapper for property and predictate should be transitioned to `Button` component. `Button` needs to take custom children first though. */
const popoverProps = this.getCustomPopoverProps({ assistiveText });
return (
<div
className={classNames(
'slds-filters__item',
'slds-grid',
'slds-grid--vertical-align-center',
{
'slds-is-locked': this.props.isLocked,
'slds-is-new': this.props.isNew,
'slds-has-error': this.props.isError,
},
this.props.className
)}
>
{!this.props.isLocked && (this.props.children || this.props.popover) ? (
<Popover {...popoverProps}>
<button
className="slds-button--reset slds-grow slds-has-blur-focus"
onClick={this.handleFilterClick}
aria-describedby={
this.props.isError ? `${this.getId()}-error` : undefined
}
>
<span className="slds-assistive-text">
{assistiveText.editFilter}
</span>
{this.props.property ? (
<p className="slds-text-body--small">{this.props.property}</p>
) : null}
<p>{this.props.predicate}</p>
</button>
</Popover>
) : (
<button
aria-describedby={
this.props.isError ? `${this.getId()}-error` : undefined
}
className="slds-button--reset slds-grow slds-has-blur-focus"
disabled
>
<p className="slds-text-body--small">{this.props.property}</p>
<p>{this.props.predicate}</p>
</button>
)}
{// Remove button
!this.props.isPermanent && !this.props.isLocked ? (
<Button
assistiveText={assistiveText.removeFilter}
hint
iconCategory="utility"
iconName="close"
iconSize="small"
iconVariant="bare"
onClick={this.handleRemove}
title={assistiveText.removeFilter}
variant="icon"
/>
) : null}
</div>
);
},
});
export default Filter;