Skip to content

Commit

Permalink
UX updates to AdminUI search (#2198)
Browse files Browse the repository at this point in the history
* Remove debounce from useListSearch

* Fix debounce

* Changeset
  • Loading branch information
jordanoverbye authored Jan 8, 2020
1 parent 4744a2d commit 709d44a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-walls-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/app-admin-ui': patch
---

Small UX updates to item/list search input
31 changes: 16 additions & 15 deletions packages/app-admin-ui/client/pages/List/Search.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsx jsx */

import { jsx } from '@emotion/core';
import { useRef, forwardRef, useState } from 'react';
import { useRef, forwardRef, useState, useCallback, useEffect } from 'react';
import debounce from 'lodash.debounce';

import { SearchIcon, XIcon } from '@arch-ui/icons';
Expand All @@ -11,31 +11,35 @@ import { LoadingSpinner } from '@arch-ui/loading';
import { colors } from '@arch-ui/theme';
import { uniformHeight } from '@arch-ui/common';

import { useListSearch } from './dataHooks';
import { useListSearch, useRouter } from './dataHooks';
import { elementOffsetStyles } from './Filters/ActiveFilters';

export default function Search({ isLoading, list }) {
const { searchValue, onChange, onClear, onSubmit } = useListSearch(list.key);
const [value, setValue] = useState(searchValue);
const inputRef = useRef();
const debouncedOnChange = debounce(onChange, 200);
const { history } = useRouter();

const hasValue = searchValue && searchValue.length;
const Icon = hasValue ? XIcon : SearchIcon;
const isFetching = hasValue && isLoading;

const handleChange = event => {
setValue(event.target.value);
debouncedOnChange(event.target.value);
const onChangeDebounced = useCallback(debounce(onChange, 400), []);

const handleChange = ({ target: { value } }) => {
setValue(value);
onChangeDebounced(value);
};

const handleClear = () => {
if (inputRef.current) {
inputRef.current.focus();
}
setValue('');
onClear();
};

useEffect(() => {
if (inputRef.current) inputRef.current.focus();
}, [history.location.search]);

const id = 'ks-list-search-input';

// NOTE: `autoComplete="off"` doesn't behave as expected on `<input />` in
Expand All @@ -60,6 +64,7 @@ export default function Search({ isLoading, list }) {
value={value}
type="text"
ref={inputRef}
disabled={isFetching}
/>
<div
css={{
Expand All @@ -74,17 +79,13 @@ export default function Search({ isLoading, list }) {
right: 0,
top: 0,
width: 40,

':hover': {
color: hasValue ? colors.text : colors.N30,
},
}}
onClick={hasValue && !isFetching ? handleClear : null}
>
{isFetching ? (
<LoadingSpinner size={16} />
) : (
<Icon onClick={hasValue ? handleClear : null} />
)}
{isFetching ? <LoadingSpinner size={16} /> : <Icon />}
</div>
</form>
);
Expand Down
20 changes: 12 additions & 8 deletions packages/app-admin-ui/client/pages/List/dataHooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useContext, useEffect, useState } from 'react';
import { useContext, useEffect, useState, useCallback } from 'react';
import { __RouterContext } from 'react-router-dom';
import debounce from 'lodash.debounce';
import { useQuery } from '@apollo/react-hooks';

import { deconstructErrorsToDataShape } from '../../util';
Expand Down Expand Up @@ -210,14 +209,19 @@ export function useListSearch(listKey) {
const routeProps = useRouter();
const { history, match } = routeProps;

const onChange = debounce(newSearch => {
const addHistoryRecord = !searchValue;
setSearch({ search: newSearch }, addHistoryRecord);
}, 300);
const onClear = () => {
const onChange = useCallback(
newSearch => {
const addHistoryRecord = !searchValue;
setSearch({ search: newSearch }, addHistoryRecord);
},
[searchValue]
);

const onClear = useCallback(() => {
const addHistoryRecord = !!searchValue;
setSearch({ search: '' }, addHistoryRecord);
};
}, [searchValue]);

const onSubmit = event => {
if (event) {
event.preventDefault();
Expand Down

0 comments on commit 709d44a

Please sign in to comment.