-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathAdvancedSearch.js
107 lines (99 loc) · 2.86 KB
/
AdvancedSearch.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
import PropTypes from 'prop-types';
import { useIntl } from 'react-intl';
import AdvancedSearchModal from './components/AdvancedSearchModal';
import AdvancedSearchRow from './components/AdvancedSearchRow';
import useAdvancedSearch from './hooks/useAdvancedSearch';
import defaultQueryBuilder from './utilities/defaultQueryBuilder';
import filterFilledRows from './utilities/filterFilledRows';
import defaultRowFormatter from './utilities/defaultRowFormatter';
import { ROW_COUNT } from './constants';
const propTypes = {
children: PropTypes.func,
defaultSearchOptionValue: PropTypes.string,
firstRowInitialSearch: PropTypes.shape({
option: PropTypes.string,
query: PropTypes.string.isRequired,
}),
hasMatchSelection: PropTypes.bool,
hasQueryOption: PropTypes.bool,
onCancel: PropTypes.func.isRequired,
onSearch: PropTypes.func.isRequired,
open: PropTypes.bool.isRequired,
queryBuilder: PropTypes.func,
queryToRow: PropTypes.func,
rowFormatter: PropTypes.func,
searchOptions: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.string,
label: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
})).isRequired,
};
const AdvancedSearch = ({
open,
searchOptions,
onSearch,
onCancel,
queryBuilder = defaultQueryBuilder,
rowFormatter = defaultRowFormatter,
hasQueryOption = true,
hasMatchSelection = true,
defaultSearchOptionValue = '',
firstRowInitialSearch = null,
queryToRow,
children,
}) => {
const intl = useIntl();
const {
handleCancel,
handleSearch,
onChange,
resetRows,
rowState,
searchOptionsWithQuery,
showEmptyFirstRowMessage,
activeQueryIndex,
} = useAdvancedSearch({
defaultSearchOptionValue,
firstRowInitialSearch,
onCancel,
onSearch,
queryBuilder,
rowFormatter,
searchOptions,
queryToRow,
hasQueryOption,
open,
});
const renderRows = () => {
return new Array(ROW_COUNT).fill(null).map((_, index) => (
<AdvancedSearchRow
key={`$advanced-search-row-${index}`}
index={index}
rowState={rowState[index]}
isActive={index === activeQueryIndex}
searchOptions={hasQueryOption ? searchOptionsWithQuery : searchOptions}
onChange={onChange}
errorMessage={index === 0 && showEmptyFirstRowMessage
? intl.formatMessage({ id: 'stripes-components.advancedSearch.emptyFirstRowError' })
: null
}
hasMatchSelection={hasMatchSelection}
/>
));
};
return (
<>
<AdvancedSearchModal
open={open}
searchOptions={searchOptionsWithQuery}
onCancel={handleCancel}
onSearch={handleSearch}
>
{renderRows()}
</AdvancedSearchModal>
{ children ? children({ resetRows, rowState: filterFilledRows(rowState) }) : null }
</>
);
};
AdvancedSearch.propTypes = propTypes;
export default AdvancedSearch;