From 86554499141e8d451cace7f73622750891f2f34b Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Sun, 19 Feb 2023 19:57:40 +0300 Subject: [PATCH] Implement combobox for Badge Actions > Queries --- components/badges/queries/Combobox.hooks.ts | 8 ++ components/badges/queries/Combobox.tsx | 105 ++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 components/badges/queries/Combobox.hooks.ts create mode 100644 components/badges/queries/Combobox.tsx diff --git a/components/badges/queries/Combobox.hooks.ts b/components/badges/queries/Combobox.hooks.ts new file mode 100644 index 0000000..b2aac76 --- /dev/null +++ b/components/badges/queries/Combobox.hooks.ts @@ -0,0 +1,8 @@ +import { useState } from 'react' + +import type { QueryListItem } from './query' + +export const useQueryComboboxState = () => { + const [value, setValue] = useState(null) + return { value, onChange: (item: QueryListItem) => setValue(item) } +} diff --git a/components/badges/queries/Combobox.tsx b/components/badges/queries/Combobox.tsx new file mode 100644 index 0000000..4953343 --- /dev/null +++ b/components/badges/queries/Combobox.tsx @@ -0,0 +1,105 @@ +import { Combobox, Transition } from '@headlessui/react' +import clsx from 'clsx' +import { FormControl } from 'components/FormControl' +import { matchSorter } from 'match-sorter' +import { Fragment, useEffect, useState } from 'react' +import { FaChevronDown, FaInfoCircle } from 'react-icons/fa' + +import type { MintRule } from '../creation/ImageUploadDetails' +import type { QueryListItem } from './query' +import { BY_KEY_QUERY_LIST, BY_KEYS_QUERY_LIST, BY_MINTER_QUERY_LIST } from './query' + +export interface QueryComboboxProps { + value: QueryListItem | null + onChange: (item: QueryListItem) => void + mintRule?: MintRule +} + +export const QueryCombobox = ({ value, onChange, mintRule }: QueryComboboxProps) => { + const [search, setSearch] = useState('') + const [QUERY_LIST, SET_QUERY_LIST] = useState(BY_KEY_QUERY_LIST) + + useEffect(() => { + if (mintRule === 'by_keys') { + SET_QUERY_LIST(BY_KEYS_QUERY_LIST) + } else if (mintRule === 'by_minter') { + SET_QUERY_LIST(BY_MINTER_QUERY_LIST) + } else { + SET_QUERY_LIST(BY_KEY_QUERY_LIST) + } + }, [mintRule]) + + const filtered = search === '' ? QUERY_LIST : matchSorter(QUERY_LIST, search, { keys: ['id', 'name', 'description'] }) + + return ( + +
+ val?.name ?? ''} + id="message-type" + onChange={(event) => setSearch(event.target.value)} + placeholder="Select query" + /> + + + {({ open }) => + + setSearch('')} as={Fragment}> + + {filtered.length < 1 && ( + + Query not found + + )} + {filtered.map((entry) => ( + + clsx('flex relative flex-col py-2 px-4 space-y-1 cursor-pointer', { 'bg-stargaze-80': active }) + } + value={entry} + > + {entry.name} + {entry.description} + + ))} + + +
+ + {value && ( +
+
+ +
+ {value.description} +
+ )} +
+ ) +}