@visns-studio/visns-components 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/crm/Autocomplete.jsx +158 -156
- package/package.json +1 -1
|
@@ -1,161 +1,163 @@
|
|
|
1
|
-
import React, { useState, useEffect } from
|
|
2
|
-
import axios from
|
|
3
|
-
import AwesomeDebouncePromise from
|
|
4
|
-
import { ToggleOffFill, ToggleOnFill } from
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import AwesomeDebouncePromise from 'awesome-debounce-promise';
|
|
4
|
+
import { ToggleOffFill, ToggleOnFill } from 'akar-icons';
|
|
5
5
|
|
|
6
6
|
const VisnsAutocomplete = (props) => {
|
|
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
|
-
|
|
7
|
+
const { api, country, field, inputValue, onSelect, setFormData, style } =
|
|
8
|
+
props;
|
|
9
|
+
const [search, setSearch] = useState('');
|
|
10
|
+
const [results, setResults] = useState([]);
|
|
11
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
12
|
+
const [overwrite, setOverwrite] = useState(false);
|
|
13
|
+
|
|
14
|
+
const performSearch = (id, value) => {
|
|
15
|
+
if (id > 0) {
|
|
16
|
+
if (value === '') {
|
|
17
|
+
setResults([]);
|
|
18
|
+
setIsLoading(false);
|
|
19
|
+
} else {
|
|
20
|
+
let url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${value}.json?types=address,poi&access_token=${api}`;
|
|
21
|
+
|
|
22
|
+
if (country) {
|
|
23
|
+
url += `&country=${country}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (style.mapbox_proximity) {
|
|
27
|
+
url += `&proximity=${style.mapbox_proximity}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (style.mapbox_bbox) {
|
|
31
|
+
url += `&bbox=${style.mapbox_bbox}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
axios
|
|
35
|
+
.get(url)
|
|
36
|
+
.then((response) => {
|
|
37
|
+
setResults(response.data.features);
|
|
38
|
+
setIsLoading(false);
|
|
39
|
+
})
|
|
40
|
+
.catch(function (error) {
|
|
41
|
+
console.log(error);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const handleSearchChange = async (e) => {
|
|
48
|
+
if (e) {
|
|
49
|
+
setSearch(e.target.value);
|
|
50
|
+
setFormData((prevState) => ({
|
|
51
|
+
...prevState,
|
|
52
|
+
[field]: e.target.value,
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
if (overwrite === false) {
|
|
56
|
+
setIsLoading(true);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (overwrite === false) {
|
|
60
|
+
await performSearchDebounced(1, e.target.value);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const performSearchDebounced = AwesomeDebouncePromise(performSearch, 500, {
|
|
66
|
+
key: (id, value) => id,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const handleItemClicked = (place) => {
|
|
70
|
+
const address =
|
|
71
|
+
place.address !== undefined && place.address !== null
|
|
72
|
+
? place.address
|
|
73
|
+
: '';
|
|
74
|
+
const text =
|
|
75
|
+
place.text !== undefined && place.text !== null ? place.text : '';
|
|
76
|
+
|
|
77
|
+
const search = address && text ? `${address} ${text}` : text;
|
|
78
|
+
setSearch(search);
|
|
79
|
+
setResults([]);
|
|
80
|
+
onSelect(place, field);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const handleOverwrite = () => {
|
|
84
|
+
if (overwrite === false) {
|
|
85
|
+
setOverwrite(true);
|
|
86
|
+
setResults([]);
|
|
87
|
+
} else {
|
|
88
|
+
setOverwrite(false);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
if (overwrite === false) {
|
|
94
|
+
if (search !== '') {
|
|
95
|
+
performSearch();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}, [overwrite]);
|
|
99
|
+
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
setSearch(inputValue);
|
|
102
|
+
}, [inputValue]);
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<>
|
|
106
|
+
<div className="AutocompletePlace">
|
|
107
|
+
<input
|
|
108
|
+
className="AutocompletePlace-input"
|
|
109
|
+
style={{
|
|
110
|
+
height:
|
|
111
|
+
style && style.multi_select_height
|
|
112
|
+
? style.multi_select_height
|
|
113
|
+
: '52px',
|
|
114
|
+
}}
|
|
115
|
+
type="text"
|
|
116
|
+
value={search || ''}
|
|
117
|
+
onChange={handleSearchChange}
|
|
118
|
+
placeholder="Type an address"
|
|
119
|
+
autoComplete="off"
|
|
120
|
+
/>
|
|
121
|
+
{overwrite === false ? (
|
|
122
|
+
<ToggleOffFill
|
|
123
|
+
data-tooltip-id="system-tooltip"
|
|
124
|
+
data-tooltip-content="Disable Autocomplete"
|
|
125
|
+
strokeWidth={2}
|
|
126
|
+
size={24}
|
|
127
|
+
onClick={handleOverwrite}
|
|
128
|
+
/>
|
|
129
|
+
) : (
|
|
130
|
+
<ToggleOnFill
|
|
131
|
+
data-tooltip-id="system-tooltip"
|
|
132
|
+
data-tooltip-content="Enable Autocomplete"
|
|
133
|
+
strokeWidth={2}
|
|
134
|
+
size={24}
|
|
135
|
+
onClick={handleOverwrite}
|
|
136
|
+
className="toggleActive"
|
|
137
|
+
/>
|
|
138
|
+
)}
|
|
139
|
+
{results.length > 0 ? (
|
|
140
|
+
<ul className="AutocompletePlace-results">
|
|
141
|
+
{results.map((place) => (
|
|
142
|
+
<li
|
|
143
|
+
key={place.id}
|
|
144
|
+
className="AutocompletePlace-items"
|
|
145
|
+
onClick={() => handleItemClicked(place)}
|
|
146
|
+
>
|
|
147
|
+
{place.place_name}
|
|
148
|
+
</li>
|
|
149
|
+
))}
|
|
150
|
+
|
|
151
|
+
{isLoading && (
|
|
152
|
+
<li className="AutocompletePlace-items">
|
|
153
|
+
Loading...
|
|
154
|
+
</li>
|
|
155
|
+
)}
|
|
156
|
+
</ul>
|
|
157
|
+
) : null}
|
|
158
|
+
</div>
|
|
159
|
+
</>
|
|
160
|
+
);
|
|
159
161
|
};
|
|
160
162
|
|
|
161
163
|
export default VisnsAutocomplete;
|
package/package.json
CHANGED