From 9184cbdf11b6f596700ee924cebe780ebf2ca024 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Tue, 2 Feb 2021 14:11:54 -0700 Subject: [PATCH] ui(admin): tweaks to LocationSelect.tsx --- .../components/admin/nodes/LocationSelect.tsx | 90 ++++++++++++------- .../scripts/components/elements/Input.tsx | 2 +- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/resources/scripts/components/admin/nodes/LocationSelect.tsx b/resources/scripts/components/admin/nodes/LocationSelect.tsx index 219a99b70..b7974dda1 100644 --- a/resources/scripts/components/admin/nodes/LocationSelect.tsx +++ b/resources/scripts/components/admin/nodes/LocationSelect.tsx @@ -27,27 +27,21 @@ export default ({ defaultLocation }: { defaultLocation: Location }) => { setExpanded(true); }; - const onBlur = () => { - // setInputText(location.short); - // setExpanded(false); - }; - const search = debounce((query: string) => { if (!expanded) { return; } - if (query === '') { + if (query === '' || query.length < 2) { setLocations([]); return; } setLoading(true); searchLocations({ short: query }).then((locations) => { - console.log(locations); setLocations(locations); }).then(() => setLoading(false)); - }, 200); + }, 250); const selectLocation = (location: Location) => { setLocation(location); @@ -58,13 +52,29 @@ export default ({ defaultLocation }: { defaultLocation: Location }) => { setExpanded(false); }, [ location ]); + useEffect(() => { + const handler = (e: KeyboardEvent) => { + if (e.key !== 'Escape') { + return; + } + + setInputText(location.short); + setExpanded(false); + }; + + window.addEventListener('keydown', handler); + return () => { + window.removeEventListener('keydown', handler); + }; + }, [ expanded ]); + return (
- { + { setInputText(e.currentTarget.value); search(e.currentTarget.value); }} @@ -78,32 +88,46 @@ export default ({ defaultLocation }: { defaultLocation: Location }) => {
-
    - {locations.map(l => ( - l.id === location.id ? -
  • selectLocation(l)}> -
    - - {l.short} - -
    + {locations.length < 1 ? +
    +

    Please type 2 or more characters.

    +
    + : +
      + {locations.map(l => ( + l.id === location.id ? +
    • { + e.stopPropagation(); + selectLocation(l); + }} + > +
      + + {l.short} + +
      - - - -
    • - : -
    • selectLocation(l)}> -
      - - {l.short} + + -
      -
    • - ))} -
    +
  • + : +
  • { + e.stopPropagation(); + selectLocation(l); + }} + > +
    + + {l.short} + +
    +
  • + ))} +
+ }
diff --git a/resources/scripts/components/elements/Input.tsx b/resources/scripts/components/elements/Input.tsx index 7e71f6384..d26ea6d77 100644 --- a/resources/scripts/components/elements/Input.tsx +++ b/resources/scripts/components/elements/Input.tsx @@ -59,7 +59,7 @@ const inputStyle = css` ${tw`opacity-75`}; } - &:read-only { + &:not(.ignoreReadOnly):read-only { ${tw`border-neutral-800 bg-neutral-900`}; }