diff --git a/assets/css/react-select.css b/assets/css/react-select.css
deleted file mode 100644
index 27a598d61..000000000
--- a/assets/css/react-select.css
+++ /dev/null
@@ -1,97 +0,0 @@
-.filter-list-text:hover ~ .filter-list-edit {
- display: flex;
-}
-
-.filter-list-text:hover ~ .filter-list-remove {
- display: none;
-}
-
-.filter-select__control:focus-within {
- box-shadow: rgb(255, 255, 255) 0px 0px 0px 0px, rgb(99, 102, 241) 0px 0px 0px 1px, rgba(0, 0, 0, 0) 0px 0px 0px 0px; /* indigo-500 */
-}
-
-.filter-select__value-container {
- padding-left: 1rem !important;
-}
-
-.filter-select__control {
- @apply dark:bg-gray-900 border-gray-300 dark:border-gray-700 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 pr-8;
- border-radius: 0.375rem !important;
- background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
- background-position: right 0.5rem center;
- background-repeat: no-repeat;
- background-size: 1.35rem 1.35rem;
-}
-
-.filter-select__control:hover {
- @apply cursor-pointer;
- border-color: rgb(156, 163, 175) !important; /* gray-400 */
-}
-
-.filter-select__indicator-separator {
- @apply hidden;
-}
-
-.filter-select__dropdown-indicator {
- display: none !important;
-}
-
-.filter-select__input {
- @apply w-full text-base sm:text-sm;
- margin: 0 !important;
-}
-
-.filter-select__input input {
- width: 100% !important;
- box-shadow: none !important;
-}
-
-.dark .filter-select__input input {
- color: rgb(209, 213, 219) !important; /* gray-300 */
-}
-
-.filter-select__value-container > :not(.filter-select__single-value) {
- @apply w-full;
- padding-top: 0 !important;
-}
-
-.filter-select__loading-indicator {
- color: rgb(79, 70, 229) !important; /* indigo-600 */
-}
-
-.filter-select__loading-indicator > span {
- height: 1.5em;
- width: 1.5em;
-}
-
-.filter-select__single-value {
- @apply dark:text-gray-400 text-base sm:text-sm;
-}
-
-.filter-select__menu-list {
- @apply dark:bg-gray-900;
-}
-
-.filter-select__option {
- @apply dark:text-gray-300;
- font-size: 1rem !important;
- line-height: 1.5rem !important;
- cursor: pointer !important;
-}
-
-@media (min-width: 640px) {
- .filter-select__option {
- font-size: 0.875rem !important;
- line-height: 1.25rem !important;
- }
-}
-
-.filter-select__option--is-focused {
- background-color: rgb(99, 102, 241) !important; /* indigo-500 */
- color: white !important;
-}
-
-.dark .filter-select__option--is-focused {
- background-color: rgb(79, 70, 229) !important; /* indigo-600 */
-}
-
diff --git a/assets/js/app.js b/assets/js/app.js
index 81e2debee..9a2ea9ac1 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -1,5 +1,4 @@
import "../css/app.css"
-import "../css/react-select.css"
import "flatpickr/dist/flatpickr.min.css"
import "./polyfills/closest"
import 'abortcontroller-polyfill/dist/polyfill-patch-fetch'
diff --git a/assets/js/dashboard/components/search-select.js b/assets/js/dashboard/components/search-select.js
new file mode 100644
index 000000000..5dcc6f6bb
--- /dev/null
+++ b/assets/js/dashboard/components/search-select.js
@@ -0,0 +1,115 @@
+import React, {useState, useCallback} from 'react'
+import {useCombobox} from 'downshift'
+import classNames from 'classnames'
+import debounce from 'debounce-promise'
+
+function selectInputText(e) {
+ e.target.select()
+}
+
+function ChevronDown() {
+ return (
+
+ )
+}
+
+function Spinner() {
+ return (
+
+ )
+}
+
+export default function SearchSelect(props) {
+ const [items, setItems] = useState([])
+ const [loading, setLoading] = useState(false)
+ const [initialLoadComplete, setInitialLoadComplete] = useState(false)
+
+ function fetchOptions({inputValue, isOpen}) {
+ setLoading(isOpen)
+
+ return props.fetchOptions(inputValue).then((loadedItems) => {
+ setLoading(false)
+ setItems(loadedItems)
+ })
+ }
+
+ const debouncedFetchOptions = useCallback(debounce(fetchOptions, 200), [])
+
+ const {
+ isOpen,
+ inputValue,
+ getToggleButtonProps,
+ getLabelProps,
+ getMenuProps,
+ getInputProps,
+ getComboboxProps,
+ highlightedIndex,
+ getItemProps,
+ selectItem,
+ closeMenu,
+ setInputValue
+ } = useCombobox({
+ items: items,
+ onInputValueChange: (changes) => {
+ debouncedFetchOptions(changes)
+ props.onInput(changes.inputValue)
+ },
+ initialSelectedItem: props.initialSelectedItem,
+ onIsOpenChange: ({inputValue}) => {
+ if (!initialLoadComplete) {
+ fetchOptions({inputValue: inputValue, isOpen: true}).then(() => {
+ setInitialLoadComplete(true)
+ })
+ }
+ }
+ })
+
+ function keydown(e) {
+ if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.isComposing || e.keyCode === 229) return
+
+ if (e.key == 'Enter' && isOpen && highlightedIndex === -1) {
+ closeMenu()
+ e.preventDefault()
+ }
+ }
+
+ return (
+
+
+
+
+ { !loading && }
+ { loading && }
+
+
+
+ { isOpen &&
+
+ { !loading && items.length == 0 &&
+ - No results found
+ }
+ { loading && items.length == 0 &&
+ - Loading options...
+ }
+
+ {
+ items.map((item, index) => (
+ -
+ {item}
+
+ ))
+ }
+
+ }
+
+
+ )
+}
diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js
index da362424c..ea05cb29e 100644
--- a/assets/js/dashboard/stats/modals/filter.js
+++ b/assets/js/dashboard/stats/modals/filter.js
@@ -2,8 +2,7 @@ import React from "react";
import { withRouter, Redirect } from 'react-router-dom'
import Datamap from 'datamaps'
-import AsyncSelect from 'react-select/async'
-import debounce from 'lodash.debounce'
+import SearchSelect from '../../components/search-select'
import Modal from './modal'
import { parseQuery, formattedFilters, navigateToQuery } from '../../query'
import Transition from "../../../transition";
@@ -14,44 +13,34 @@ class FilterModal extends React.Component {
super(props)
this.state = {
query: parseQuery(props.location.search, props.site),
- selectedFilter: "",
+ selectedFilter: this.props.match.params.field || 'page',
negated: false,
- updatedValue: "",
- inputValue: "",
- forceReloadSuggestions: 0,
- defaultOptions: []
+ filterValue: "",
}
this.editableGoals = Object.keys(this.state.query.filters).filter(filter => !['props'].includes(filter))
- this.renderSelector = this.renderSelector.bind(this)
- this.fetchSuggestions = this.fetchSuggestions.bind(this)
this.handleKeydown = this.handleKeydown.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
- this.fieldSelectRef = React.createRef()
- this.debouncedFetchSuggestions = debounce((q, callback) => this.fetchSuggestions(q, callback), 150)
}
componentDidMount() {
- this.setState({ selectedFilter: this.props.match.params.field })
- this.fieldSelectRef.current.focus()
document.addEventListener("keydown", this.handleKeydown)
}
componentDidUpdate(prevProps, prevState) {
- const { query, selectedFilter, updatedValue, forceReloadSuggestions } = this.state
+ const { query, selectedFilter, filterValue } = this.state
if (prevState.selectedFilter !== selectedFilter) {
const negated = !!query.filters[selectedFilter] && query.filters[selectedFilter][0] == '!' && this.negationSupported(selectedFilter)
- let updatedValue = negated ? query.filters[selectedFilter].slice(1) : (query.filters[selectedFilter] || "")
+ let filterValue = negated ? query.filters[selectedFilter].slice(1) : (query.filters[selectedFilter] || "")
if (selectedFilter == 'country') {
const allCountries = Datamap.prototype.worldTopo.objects.world.geometries;
- const selectedCountry = allCountries.find((c) => c.id === updatedValue) || { properties: { name: updatedValue } };
- updatedValue = selectedCountry.properties.name
+ const selectedCountry = allCountries.find((c) => c.id === filterValue) || { properties: { name: filterValue } };
+ filterValue = selectedCountry.properties.name
}
- if (selectedFilter) this.fetchSuggestions('', (defaultOptions) => this.setState({ defaultOptions }))
- this.setState({ updatedValue, inputValue: updatedValue, negated, forceReloadSuggestions: forceReloadSuggestions + 1 })
+ this.setState({ filterValue, negated })
}
}
@@ -71,73 +60,43 @@ class FilterModal extends React.Component {
return ['page', 'entry_page', 'exit_page'].includes(filter)
}
- fetchSuggestions(searchQuery = "", callback) {
- const { query, selectedFilter } = this.state
+ fetchOptions(input) {
+ const {query, selectedFilter} = this.state
const updatedQuery = { ...query, filters: { ...query.filters, [selectedFilter]: null } }
- if (selectedFilter == 'country') {
- const matchedCountries = Datamap.prototype.worldTopo.objects.world.geometries.filter(c => c.properties.name.includes(searchQuery.trim()))
+ if (selectedFilter === 'country') {
+ const matchedCountries = Datamap.prototype.worldTopo.objects.world.geometries.filter(c => c.properties.name.includes(input.trim()))
const matches = matchedCountries.map(c => c.id)
- api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/country`, updatedQuery, { q: matches })
+ return api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/country`, updatedQuery, { q: matches })
.then((res) => {
- const result = res.map(code => matchedCountries.filter(c => c.id == code)[0].properties.name).map(r => ({ label: r, value: r }))
- callback(result)
+ return res.map(code => matchedCountries.filter(c => c.id == code)[0].properties.name)
})
} else {
- api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, updatedQuery, { q: searchQuery.trim() })
- .then((res) => {
- const result = res.map(r => ({ label: r, value: r }))
- callback(result)
- })
+ return api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, updatedQuery, { q: input.trim() })
}
}
- renderSelector(filter) {
- const { updatedValue, selectedFilter, inputValue, forceReloadSuggestions, defaultOptions } = this.state;
-
- if (!filter) return null
+ onInput(val) {
+ this.setState({filterValue: val})
+ }
+ renderSearchSelector(filter) {
return (
- this.debouncedFetchSuggestions(q, callback)}
- placeholder='Filter value'
- maxMenuHeight={250}
- tabSelectsValue={false}
- classNamePrefix='filter-select'
- blurInputOnSelect
- noOptionsMessage={() => (
-
-
- No results found in current dashboard, try removing some filters, checking your input and ensuring this {formattedFilters[selectedFilter].toLowerCase()} exists
-
- )}
- loadingMessage={() => (
-
- Loading filter suggestions
-
- )}
- value={inputValue == '' ? { label: 'Enter a filter value', value: '' } : { label: updatedValue, value: `${updatedValue } ` }}
- onChange={({ value }, a) => {
- this.setState({ updatedValue: value, inputValue: value, forceReloadSuggestions: forceReloadSuggestions + 1 })
- }}
- onInputChange={(value, { action }) => {
- if (action == 'input-change') this.setState({ inputValue: value })
- if (action == 'menu-close' || action == 'input-blur') this.setState(({inputValue}) => ({ updatedValue: inputValue }))
- }}
- inputValue={inputValue}
- onBlur={() => this.setState({ forceReloadSuggestions: forceReloadSuggestions + 1 })}
- />)
+
+ )
}
handleSubmit() {
- const { selectedFilter, negated, updatedValue } = this.state;
- const validFilter = this.editableGoals.includes(selectedFilter) && updatedValue.trim()
+ const { selectedFilter, negated, filterValue } = this.state;
+ const validFilter = this.editableGoals.includes(selectedFilter) && filterValue.trim()
- let finalFilterValue = (this.negationSupported(selectedFilter) && negated ? '!' : '') + updatedValue.trim()
+ let finalFilterValue = (this.negationSupported(selectedFilter) && negated ? '!' : '') + filterValue.trim()
if (selectedFilter == 'country') {
const allCountries = Datamap.prototype.worldTopo.objects.world.geometries;
const selectedCountry = allCountries.find((c) => c.properties.name === finalFilterValue) || { id: finalFilterValue };
@@ -152,8 +111,8 @@ class FilterModal extends React.Component {
}
renderBody() {
- const { selectedFilter, negated, updatedValue, query } = this.state;
- const validFilter = this.editableGoals.includes(selectedFilter) && updatedValue.trim()
+ const { selectedFilter, negated, filterValue, query } = this.state;
+ const validFilter = this.editableGoals.includes(selectedFilter) && filterValue.trim()
return (
<>
@@ -167,7 +126,6 @@ class FilterModal extends React.Component {
className="my-2 block w-full py-2 pl-3 pr-10 text-base border-gray-300 dark:border-gray-700 hover:border-gray-400 dark:hover:border-gray-200 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:bg-gray-900 dark:text-gray-300 cursor-pointer"
placeholder="Select a Filter"
onChange={(e) => this.setState({ selectedFilter: e.target.value })}
- ref={this.fieldSelectRef}
>
{this.editableGoals.map(filter => )}
@@ -188,7 +146,7 @@ class FilterModal extends React.Component {
)}
- {this.renderSelector(selectedFilter)}
+ {this.renderSearchSelector(selectedFilter)}