/
index.js
executable file
·310 lines (284 loc) · 11.2 KB
/
index.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
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
300
301
302
303
304
305
306
307
308
309
310
import React, {
useState, useRef,
} from 'react';
import PropTypes from 'prop-types';
import { capitalCase } from 'change-case';
import { filtersObject } from '@util/validation';
import { handlePseudoButtonKeyDown } from '@util/a11y-seo';
import { getGlobalThis } from '@util/objects';
import FilterGroup from '@components/FilterGroup';
import FilterLabel from '@components/FilterLabel';
import Checkbox from '@components/Checkbox';
import Range from '@components/Range';
// import Callout from '@components/Callout';
import Icon from '@components/Icon';
// import Inset from '@components/Inset';
import Row from '@components/Row';
import Column from '@components/Column';
import Stack from '@components/Stack';
import ClearFiltersButton from '@components/ClearFiltersButton';
import './FiltersPanel.scss';
const globalThis = getGlobalThis();
function handleDoubleClick( event ) {
// https://stackoverflow.com/a/43321596/214325
if ( event.detail > 1 ) { // Number of clicks
event.preventDefault();
}
}
function FiltersPanel( props ) {
const isDesktop = true; // globalThis.matchMedia( '(min-width: 992px)' ).matches; // TODO: define breakpoints that line up with the CSS in JS somewhere
const attributes = { ...props };
const [isExpanded, setExpanded] = useState( isDesktop );
const [hasInteractedWithFilters, setHasInteractedWithFilters] = useState( false );
const [lastInteractedWithFilters, setLastInteractedWithFilters] = useState( null );
const $self = useRef();
const handleClick = ( event ) => {
const $element = event.target;
let { className, nodeName } = $element;
nodeName = nodeName.toLowerCase();
if ( nodeName === 'use' ) {
className = $element.parentNode.className;
}
if ( className instanceof SVGAnimatedString ) {
className = className.baseVal;
}
const isFiltersPanelClick = /\bml-filters-panel/.test( className );
if ( isFiltersPanelClick ) {
setExpanded( !isExpanded );
} else {
if ( !props.updatingDrawerHeight ) {
props.setUpdatingDrawerHeight( true );
props.drawerRef.current.style.height = '';
props.updateDrawerHeight( props.drawerRef, 250 );
}
}
// updateOwnHeight();
};
// useEffect( props.updateDrawerHeight );
globalThis.addEventListener( 'resize', ( /* event */ ) => {
if ( !props.updatingDrawerHeight ) {
props.setUpdatingDrawerHeight( true );
props.drawerRef.current.style.height = '';
props.updateDrawerHeight( props.drawerRef, 125 );
}
}, false );
if ( props.className ) {
delete attributes.className;
}
if ( props.columnWidth ) {
delete attributes.columnWidth;
attributes['data-column-width'] = props.columnWidth;
}
if ( props.filters ) {
delete attributes.filters;
}
if ( props.handleFilterChange ) {
delete attributes.handleFilterChange;
}
delete attributes.updateDrawerHeight;
delete attributes.updatingDrawerHeight;
delete attributes.setUpdatingDrawerHeight;
delete attributes.listingCounts;
delete attributes.drawerRef;
delete attributes.clearFilters;
delete attributes.undoClearFilters;
delete attributes.showClearFiltersInitially;
const {
offer,
location,
bedrooms,
amiQualification,
rentalPrice,
} = props.filters;
const { listingCounts } = props;
const isExpandedIndicator = ( isExpanded ? '⌃' : '⌄' );
const ariaLabel = `Filter Listings ${isExpandedIndicator}`;
const rentalCount = listingCounts.offer.rent;
// console.log( 'props.filters.rentalPrice.upperBound', props.filters.rentalPrice.upperBound );
// console.log( 'rentalPrice.upperBound', rentalPrice.upperBound );
return (
<section
data-testid="ml-filters-panel"
ref={ $self }
className={
`ml-filters-panel${
props.className
? ` ${props.className}`
: ''
}${
isExpanded ? ' ml-filters-panel--expanded' : ''
}`
}
{ ...attributes }
onClick={ handleClick }
onChange={ ( event ) => {
setHasInteractedWithFilters( true );
setLastInteractedWithFilters( Date.now() );
props.handleFilterChange( event );
} }
>
<div className="ml-filters-panel__menu">
<h3
className="ml-filters-panel__heading"
aria-label={ ariaLabel }
aria-expanded={ isExpanded.toString() }
aria-controls="filters-panel-content"
onMouseDown={ handleDoubleClick }
onKeyDown={ ( event ) => handlePseudoButtonKeyDown( event, true ) }
tabIndex="0"
>
Filter Listings
<Icon className="ml-filters-panel__heading-icon" icon="icon-details-marker" width="19" height="11" alt={ isExpandedIndicator } />
</h3>
<div
id="filters-panel-content"
ref={ props.drawerRef }
className={ `ml-filters-panel__content${isExpanded ? ' ml-filters-panel__content--expanded' : ''}` }
>
<menu className="ml-filters-panel__clear">
<li>
<ClearFiltersButton
clearFilters={ props.clearFilters }
undoClearFilters={ props.undoClearFilters }
showClearFiltersInitially={ props.showClearFiltersInitially }
hasInteractedWithFilters={ hasInteractedWithFilters }
lastInteractedWithFilters={ lastInteractedWithFilters }
/>
</li>
</menu>
<FilterGroup criterion="offer">
<FilterGroup.Label>Offer</FilterGroup.Label>
<Row space="rent-sale" stackAt="large">
<Column width="1/2">
<Checkbox
button
criterion="offer"
value="rent"
checked={ offer.rent }
aria-label={ `For Rent (${rentalCount})` }
>{ `For Rent (${rentalCount})` }</Checkbox>
</Column>
<Column width="1/2">
<Checkbox
button
criterion="offer"
value="sale"
checked={ offer.sale }
>{ `For Sale (${listingCounts.offer.sale})` }</Checkbox>
</Column>
</Row>
</FilterGroup>
<FilterGroup criterion="rentalPrice">
<FilterGroup.Label>Rental Price</FilterGroup.Label>
<Range
criterion="rentalPrice"
min={ 0 }
step={ 100 }
max={ 3000 }
lowerBound={ rentalPrice.lowerBound }
upperBound={ rentalPrice.upperBound }
valueFormat="$"
valueAppend={ () => <> per month</> }
maxValueAppend={ () => <>+</> }
/>
</FilterGroup>
<FilterGroup criterion="location">
<FilterGroup.Label>Location</FilterGroup.Label>
<Stack space="sister-checkboxes">
<Checkbox
criterion="city"
value="boston"
checked={ location.city.boston }
hasSubcategories
>
<FilterLabel>Boston</FilterLabel>
{
Object.keys( listingCounts.location.neighborhood )
.sort()
.sort( ( neighborhoodA, neighborhoodB ) => (
listingCounts.location.neighborhood[neighborhoodB]
- listingCounts.location.neighborhood[neighborhoodA]
) )
.map( ( neighborhood ) => {
const count = listingCounts.location.neighborhood[neighborhood];
return (
<Checkbox
key={ neighborhood }
criterion="neighborhood"
value={ neighborhood }
checked={ location.neighborhood[neighborhood] || false }
>{ `${capitalCase( neighborhood )} (${count || '0'})` }</Checkbox>
);
} )
}
</Checkbox>
<Checkbox
criterion="city"
value="beyondBoston"
checked={ location.city.beyondBoston }
hasSubcategories
>
<FilterLabel>Beyond Boston</FilterLabel>
{
Object.keys( listingCounts.location.cardinalDirection )
.sort( ( cardinalDirectionA, cardinalDirectionB ) => (
listingCounts.location.cardinalDirection[cardinalDirectionB]
- listingCounts.location.cardinalDirection[cardinalDirectionA]
) )
.map( ( cardinalDirection ) => {
const count = listingCounts.location.cardinalDirection[cardinalDirection];
return (
<Checkbox
key={ cardinalDirection }
criterion="cardinalDirection"
value={ cardinalDirection }
checked={ location.cardinalDirection[cardinalDirection] || false }
>{ `${capitalCase( cardinalDirection )} of Boston (${count || '0'})` }</Checkbox>
);
} )
}
</Checkbox>
</Stack>
</FilterGroup>
<FilterGroup criterion="bedrooms" orientation="horizontal">
<FilterGroup.Label>Bedrooms</FilterGroup.Label>
<Checkbox criterion="bedrooms" aria-label="0-bedrooms" checked={ !!bedrooms['0br'] } value="0br">0</Checkbox>
<Checkbox criterion="bedrooms" aria-label="1-bedrooms" checked={ !!bedrooms['1br'] } value="1br">1</Checkbox>
<Checkbox criterion="bedrooms" aria-label="2-bedrooms" checked={ !!bedrooms['2br'] } value="2br">2</Checkbox>
<Checkbox criterion="bedrooms" aria-label="3+-bedrooms" checked={ !!bedrooms['3+br'] } value="3+br">3+</Checkbox>
</FilterGroup>
<FilterGroup criterion="amiQualification">
<FilterGroup.Label>Income Eligibility</FilterGroup.Label>
<div onClick={ ( event ) => event.stopPropagation() }>
<Range
criterion="amiQualification"
min={ 0 }
max={ 200 }
lowerBound={ amiQualification.lowerBound }
upperBound={ amiQualification.upperBound }
valueFormat="%"
valueAppend={ () => <> <abbr className="ml-range__review-unit">AMI</abbr></> }
/>
</div>
</FilterGroup>
</div>
</div>{ /* filters-p anel__menu */ }
</section>
);
}
FiltersPanel.propTypes = {
"className": PropTypes.string,
"columnWidth": PropTypes.string,
"filters": filtersObject,
"clearFilters": PropTypes.func,
"undoClearFilters": PropTypes.func,
"showClearFiltersInitially": PropTypes.bool,
"handleFilterChange": PropTypes.func.isRequired,
"updateDrawerHeight": PropTypes.func.isRequired,
"listingCounts": PropTypes.object,
"drawerRef": PropTypes.object.isRequired,
"updatingDrawerHeight": PropTypes.bool,
"setUpdatingDrawerHeight": PropTypes.func,
"isExpanded": PropTypes.bool,
};
export default FiltersPanel;