-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSelectSearchConnected.js
More file actions
149 lines (126 loc) · 3.7 KB
/
SelectSearchConnected.js
File metadata and controls
149 lines (126 loc) · 3.7 KB
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
// @flow
import React, { Component } from 'react';
import { connect } from 'react-redux';
import wrapWithClickout from 'react-clickout';
import SelectSearchBase from './SelectSearchBase';
import {
addSelect,
toggleOpen,
setSelected,
saveSearch,
removeSelect,
} from '../redux/actions';
import type { Options, Selected, Callback } from '../types';
type SelectSearchConnectedProps = {
// config
id: string,
isMultipleSelect: boolean,
// appearance
label: string,
prefix: string,
placeholder: string,
useImages: boolean,
// data
isOpen: boolean,
searchTerm: string,
options: Options,
selected: Selected,
totalPages: number,
currentPage: number,
loading: boolean,
// connected methods
addSelect: Callback,
removeSelect: Callback,
setSelected: Callback,
toggleOpen: Callback,
handleSearch: Callback,
saveSearch: Callback,
handleSelectedClick: Callback,
handleOptionClick: Callback,
};
type SelectSearchConnectedDefaultProps = {
// appearance
label: '',
placeholder: '',
prefix: 'rsm',
// data
isOpen: false,
searchTerm: '',
};
export class SelectSearchConnectedComponent extends Component {
defaultProps: SelectSearchConnectedDefaultProps;
props: SelectSearchConnectedProps;
componentDidMount() {
const { id } = this.props;
this.props.addSelect({ id });
}
componentWillUnmount() {
const { id } = this.props;
this.props.removeSelect({ id });
}
handleOptionClick = (option: Object) => () => {
this.props.handleOptionClick({ selectId: this.props.id, option });
}
handleSelectedClick = (selected: Object) => () => {
this.props.handleSelectedClick({ selectId: this.props.id, selected });
}
handleClickout = () => {
if (!this.props.isOpen) return;
this.props.toggleOpen({ id: this.props.id, isOpen: false });
}
handleSearch = (searchTerm: string) => {
if (!this.props.isOpen) {
this.props.toggleOpen({ id: this.props.id, isOpen: true });
}
this.props.saveSearch({ id: this.props.id, searchTerm });
this.props.handleSearch({ search: searchTerm });
}
handleScroll = (e: Object) => {
const { scrollTop, offsetHeight, scrollHeight } = e.target;
const { currentPage, totalPages, loading } = this.props;
const nextPage = Number(currentPage) + 1;
const loadHeight = (scrollHeight - offsetHeight) - 50;
if (Math.floor(scrollTop) >= (loadHeight)
&& nextPage <= totalPages && !loading) {
this.props.handleSearch({
search: this.props.searchTerm,
pageNo: nextPage,
});
}
}
render() {
return (
<SelectSearchBase
id={this.props.id}
isMultipleSelect={this.props.isMultipleSelect}
label={this.props.label}
prefix={this.props.prefix}
placeholder={this.props.placeholder}
useImages={this.props.useImages}
options={this.props.options}
selected={this.props.selected}
searchTerm={this.props.searchTerm}
isOpen={this.props.isOpen}
handleOptionClick={this.handleOptionClick}
handleSelectedClick={this.handleSelectedClick}
handleSearch={this.handleSearch}
handleScroll={this.handleScroll}
/>);
}
}
const mapStateToProps = (state, ownProps) => ({
isOpen: state.select.getIn([ownProps.id, 'isOpen']),
selected: state.select.getIn([ownProps.id, 'selected']),
options: state.select.getIn([ownProps.id, 'options']),
searchTerm: state.select.getIn([ownProps.id, 'searchTerm']),
});
const mapDispatchToProps = {
addSelect,
toggleOpen,
setSelected,
saveSearch,
removeSelect,
};
const Wrapped = wrapWithClickout(SelectSearchConnectedComponent);
const connected = connect(mapStateToProps, mapDispatchToProps)(Wrapped);
export default connected;