diff --git a/assets/css/app.css b/assets/css/app.css index 255a904fc..354cffc1f 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -318,3 +318,71 @@ iframe[hidden] { .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__control { + @apply dark:bg-gray-900 border-gray-300 dark:border-gray-700 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500; +} + +.filter-select__control:hover { + @apply cursor-pointer; + border-color: rgb(156, 163, 175) !important; /* gray-400 */ +} + +.filter-select__indicator-separator { + @apply hidden; +} + +.filter-select__indicator { + @apply text-gray-400; +} + +.filter-select__input { + @apply w-full; +} + +.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; +} + +.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; +} + +.filter-select__menu-list { + @apply dark:bg-gray-900; +} + +.filter-select__option { + @apply dark:text-gray-300; +} + +.filter-select__option--is-focused { + background-color: rgb(165, 180, 252) !important; /* indigo-300 */ +} + +.dark .filter-select__option--is-focused { + background-color: rgb(79, 70, 229) !important; /* indigo-600 */ +} + diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index 2149e1f01..52225788e 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -1,11 +1,13 @@ 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 Modal from './modal' import { parseQuery, formattedFilters, navigateToQuery } from '../../query' import Transition from "../../../transition"; import * as api from '../../api' -import Datamap from 'datamaps' class FilterModal extends React.Component { constructor(props) { @@ -15,32 +17,32 @@ class FilterModal extends React.Component { selectedFilter: "", negated: false, updatedValue: "", - filterSaved: false, - suggestions: [], - fetchSuggestionsTimeout: undefined, - focusedSuggestionIndex: -1, - preventSuggestions: true + inputValue: "", + forceReloadSuggestions: 0, + defaultOptions: [] } 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 }) - - document.addEventListener("keydown", this.handleKeydown); + this.fieldSelectRef.current.focus() + document.addEventListener("keydown", this.handleKeydown) } componentDidUpdate(prevProps, prevState) { - const { query, selectedFilter, updatedValue, preventSuggestions, fetchSuggestionsTimeout } = this.state + const { query, selectedFilter, updatedValue, forceReloadSuggestions } = this.state if (prevState.selectedFilter !== selectedFilter) { - const negated = query.filters[selectedFilter] && query.filters[selectedFilter][0] == '!' && this.negationSupported(selectedFilter) + const negated = !!query.filters[selectedFilter] && query.filters[selectedFilter][0] == '!' && this.negationSupported(selectedFilter) let updatedValue = negated ? query.filters[selectedFilter].slice(1) : (query.filters[selectedFilter] || "") - clearTimeout(fetchSuggestionsTimeout) if (selectedFilter == 'country') { const allCountries = Datamap.prototype.worldTopo.objects.world.geometries; @@ -48,24 +50,8 @@ class FilterModal extends React.Component { updatedValue = selectedCountry.properties.name } - this.setState({ updatedValue, negated, suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: false }, () => { - if (selectedFilter) { - this.fetchSuggestions() - } - }) - } - - if (prevState.updatedValue !== updatedValue) { - if (!preventSuggestions || prevState.preventSuggestions) { - let fetchSuggestionsTimeout; - clearTimeout(this.state.fetchSuggestionsTimeout) - - this.setState({ suggestions: [], preventSuggestions: false, focusedSuggestionIndex: -1, noSuggestions: false }, () => { - this.setState({ fetchSuggestionsTimeout: setTimeout(this.fetchSuggestions, 200) }) - }) - } else { - this.setState({ suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: false }) - } + if (selectedFilter) this.fetchSuggestions('', (defaultOptions) => this.setState({ defaultOptions })) + this.setState({ updatedValue, inputValue: updatedValue, negated, forceReloadSuggestions: forceReloadSuggestions + 1 }) } } @@ -74,152 +60,124 @@ class FilterModal extends React.Component { } handleKeydown(e) { - const { suggestions } = this.state - let { focusedSuggestionIndex, updatedValue, preventSuggestions, selectedFilter } = this.state - const navigationKeys = ['ArrowUp', undefined, 'ArrowDown'] - - if (e.target.id !== 'suggestions_input') return true if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.isComposing || e.keyCode === 229) return - if ((e.key === 'Enter' || e.key === 'Tab') && suggestions[focusedSuggestionIndex]) { - updatedValue = suggestions[focusedSuggestionIndex] - focusedSuggestionIndex = -1 - preventSuggestions = true - e.preventDefault() - } - - if (navigationKeys.includes(e.key)) { - focusedSuggestionIndex += navigationKeys.indexOf(e.key) - 1 - if (focusedSuggestionIndex < 0) { - focusedSuggestionIndex = suggestions.length - 1 - } else if (focusedSuggestionIndex >= suggestions.length) { - focusedSuggestionIndex = 0 - } - e.preventDefault() + if (e.target.tagName == 'BODY' && e.key == 'Enter') { + this.handleSubmit() } - - this.setState({ focusedSuggestionIndex, updatedValue, preventSuggestions }) } negationSupported(filter) { return ['page', 'entry_page', 'exit_page'].includes(filter) } - fetchSuggestions() { - const { query, updatedValue, selectedFilter } = this.state + fetchSuggestions(searchQuery = "", callback) { + 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(updatedValue.trim())) + const matchedCountries = Datamap.prototype.worldTopo.objects.world.geometries.filter(c => c.properties.name.includes(searchQuery.trim())) const matches = matchedCountries.map(c => c.id) api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/country`, updatedQuery, { q: matches }) .then((res) => { - const suggestions = res.map(code => matchedCountries.filter(c => c.id == code)[0].properties.name) - - this.setState({ suggestions, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: res.length == 0 }) + const result = res.map(code => matchedCountries.filter(c => c.id == code)[0].properties.name).map(r => ({ label: r, value: r })) + callback(result) }) } else { - api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, updatedQuery, { q: updatedValue.trim() }) + api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, updatedQuery, { q: searchQuery.trim() }) .then((res) => { - this.setState({ suggestions: res, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: res.length == 0 }) + const result = res.map(r => ({ label: r, value: r })) + callback(result) }) } } renderSelector(filter) { - const { updatedValue, suggestions, inputFocused, focusedSuggestionIndex, selectedFilter } = this.state; + const { updatedValue, selectedFilter, inputValue, forceReloadSuggestions, defaultOptions } = this.state; if (!filter) return null - return