Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Add proper Typescript generics

Blue Mouse300fd5364836
diff --git a/app/ui/Dropdown.tsx b/app/ui/Dropdown.tsx
index a0f05c062..c2f47f90e 100644
--- a/app/ui/Dropdown.tsx
+++ b/app/ui/Dropdown.tsx
@@ -1,24 +1,20 @@
-import React, { memo, Fragment } from 'react'
-import { Menu, Transition } from '@headlessui/react'
+import React, { memo, Fragment, Key } from 'react'
+import { Menu, MenuButton, MenuItems, MenuItem, Transition } from '@headlessui/react'
import { ChevronDownIcon } from '@heroicons/react/24/solid'
import { ChevronDownIcon as ChevronDownIconMini } from '@heroicons/react/20/solid'
import cx from 'clsx'
import _map from 'lodash/map'
import _isEmpty from 'lodash/isEmpty'
+import Spin from './icons/Spin'
-// Define the prop types for the component
-interface IDropdown {
+interface IDropdown<T> {
title: string | number | React.ReactNode
desc?: string | number | React.ReactNode
className?: string
- /* (array): An array of items to be displayed in the dropdown menu. */
- items: any[]
- /* (function): A function that returns the label for each item in the dropdown menu. */
- labelExtractor?: (item: any) => string | number | React.ReactNode
- /* (function): A function that returns the key for each item in the dropdown menu. */
- keyExtractor?: (item: any) => string | number | React.ReactNode
- /* (function): A function that is called when an item is selected. */
- onSelect: (item: any, e?: React.MouseEvent<HTMLElement>) => void | null
+ items: T[]
+ labelExtractor?: (item: T, close: () => void) => string | number | React.ReactNode
+ keyExtractor?: (item: T, close: () => void) => Key
+ onSelect: (item: T, e: React.MouseEvent<HTMLElement>, close: () => void) => void | null
aside?: boolean
buttonClassName?: string
selectItemClassName?: string
@@ -27,9 +23,11 @@ interface IDropdown {
chevron?: 'regular' | 'mini'
headless?: boolean
disabled?: boolean
+ onClick?: React.MouseEventHandler<HTMLButtonElement>
+ loading?: boolean
}
-const Dropdown = ({
+function Dropdown<T>({
title,
desc,
className,
@@ -45,87 +43,99 @@ const Dropdown = ({
chevron = 'regular',
headless,
disabled,
-}: IDropdown): JSX.Element => (
- <Menu as='div' className={cx('relative inline-block text-left', className)}>
- {({ open }) => (
- <>
- {!_isEmpty(desc) && <p className='mb-2 text-sm text-gray-900'>{desc}</p>}
- <div>
- <Menu.Button
- disabled={disabled}
- className={cx(buttonClassName, {
- 'justify-between': aside,
- 'justify-center': !aside,
- 'inline-flex w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm font-medium shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:ring-offset-gray-100 dark:border-gray-800 dark:bg-slate-800 dark:hover:bg-slate-700 md:px-4':
- !headless,
- 'group inline-flex w-full px-3 py-2 text-sm font-medium outline-none md:px-4': headless,
- 'text-gray-700 dark:text-gray-50': !disabled,
- 'cursor-not-allowed text-gray-500 dark:text-gray-400': disabled,
- })}
- >
- {title}
- {chevron === 'regular' && (
- <ChevronDownIcon
- className={cx('-mr-1 ml-2 h-5 w-5 transform-gpu transition-transform', {
- 'group-hover:text-gray-500': headless,
- 'rotate-180': open,
- })}
- aria-hidden='true'
- />
- )}
- {chevron === 'mini' && (
- <ChevronDownIconMini
- className={cx('-mr-1 ml-1 h-5 w-5 transform-gpu transition-transform', {
- 'group-hover:text-gray-500': headless,
- 'rotate-180': open,
- })}
- aria-hidden='true'
- />
- )}
- </Menu.Button>
- </div>
+ onClick,
+ loading,
+}: IDropdown<T>): JSX.Element {
+ return (
+ <Menu as='div' className={cx('relative inline-block text-left', className)}>
+ {({ open, close }) => (
+ <>
+ {!_isEmpty(desc) && <p className='mb-2 text-sm text-gray-900'>{desc}</p>}
+ <div>
+ <MenuButton
+ onClick={onClick}
+ disabled={disabled}
+ className={cx(buttonClassName, {
+ 'justify-between': aside,
+ 'justify-center': !aside,
+ 'inline-flex w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm font-medium shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:ring-offset-gray-100 dark:border-gray-800 dark:bg-slate-800 dark:hover:bg-slate-700 md:px-4':
+ !headless,
+ 'group inline-flex w-full px-3 py-2 text-sm font-medium outline-none md:px-4': headless,
+ 'text-gray-700 dark:text-gray-50': !disabled,
+ 'cursor-not-allowed text-gray-500 dark:text-gray-400': disabled,
+ })}
+ >
+ {title}
+ {chevron === 'regular' && (
+ <ChevronDownIcon
+ className={cx('-mr-1 ml-2 h-5 w-5 transform-gpu transition-transform', {
+ 'group-hover:text-gray-500': headless,
+ 'rotate-180': open,
+ })}
+ aria-hidden='true'
+ />
+ )}
+ {chevron === 'mini' && (
+ <ChevronDownIconMini
+ className={cx('-mr-1 ml-1 h-5 w-5 transform-gpu transition-transform', {
+ 'group-hover:text-gray-500': headless,
+ 'rotate-180': open,
+ })}
+ aria-hidden='true'
+ />
+ )}
+ </MenuButton>
+ </div>
- <Transition
- show={open}
- as={Fragment}
- enter='transition ease-out duration-100'
- enterFrom='transform opacity-0 scale-95'
- enterTo='transform opacity-100 scale-100'
- leave='transition ease-in duration-75'
- leaveFrom='transform opacity-100 scale-100'
- leaveTo='transform opacity-0 scale-95'
- >
- <Menu.Items
- static
- className={cx(
- 'absolute right-0 z-50 mt-2 w-40 min-w-max origin-top-right rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none dark:bg-slate-800',
- menuItemsClassName,
- {
- 'divide-y divide-gray-100 dark:divide-gray-600': header,
- },
- )}
+ <Transition
+ show={open}
+ as={Fragment}
+ enter='transition ease-out duration-100'
+ enterFrom='transform opacity-0 scale-95'
+ enterTo='transform opacity-100 scale-100'
+ leave='transition ease-in duration-75'
+ leaveFrom='transform opacity-100 scale-100'
+ leaveTo='transform opacity-0 scale-95'
>
- {header && <p className='px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-50'>{header}</p>}
- {_map(items, (item) => (
- <Menu.Item key={keyExtractor ? keyExtractor(item) : item}>
- <span
- className={
- selectItemClassName ||
- 'block cursor-pointer px-4 py-2 text-sm text-gray-700 hover:bg-gray-200 dark:border-gray-800 dark:bg-slate-800 dark:text-gray-50 dark:hover:bg-slate-700'
- }
- role='menuitem'
- tabIndex={0}
- onClick={(e: React.MouseEvent<HTMLElement>) => onSelect(item, e)}
- >
- {labelExtractor ? labelExtractor(item) : item}
- </span>
- </Menu.Item>
- ))}
- </Menu.Items>
- </Transition>
- </>
- )}
- </Menu>
-)
+ <MenuItems
+ static
+ className={cx(
+ 'absolute right-0 z-50 mt-2 w-40 min-w-max origin-top-right rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none dark:bg-slate-800',
+ menuItemsClassName,
+ {
+ 'divide-y divide-gray-100 dark:divide-gray-600': header,
+ },
+ )}
+ >
+ {header && <p className='px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-50'>{header}</p>}
+ {loading ? (
+ <div className='px-4 py-2'>
+ <Spin className='!ml-0' />
+ <span className='sr-only'>Loading...</span>
+ </div>
+ ) : (
+ _map(items, (item) => (
+ <MenuItem key={keyExtractor ? keyExtractor(item, close) : (item as Key)}>
+ <span
+ className={
+ selectItemClassName ||
+ 'block cursor-pointer px-4 py-2 text-sm text-gray-700 hover:bg-gray-200 dark:border-gray-800 dark:bg-slate-800 dark:text-gray-50 dark:hover:bg-slate-700'
+ }
+ role='menuitem'
+ tabIndex={0}
+ onClick={(e: React.MouseEvent<HTMLElement>) => onSelect(item, e, close)}
+ >
+ {labelExtractor ? labelExtractor(item, close) : (item as React.ReactNode)}
+ </span>
+ </MenuItem>
+ ))
+ )}
+ </MenuItems>
+ </Transition>
+ </>
+ )}
+ </Menu>
+ )
+}
-export default memo(Dropdown)
+export default memo(Dropdown) as typeof Dropdown
diff --git a/app/ui/Input.tsx b/app/ui/Input.tsx
index 5492669d0..2f3aa72c2 100644
--- a/app/ui/Input.tsx
+++ b/app/ui/Input.tsx
@@ -17,6 +17,7 @@ interface IInput {
disabled?: boolean
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void
isBeta?: boolean
+ maxLength?: number
}
const Input = ({
@@ -32,6 +33,7 @@ const Input = ({
disabled,
onKeyDown,
isBeta,
+ maxLength,
}: IInput): JSX.Element => {
const isError = !_isEmpty(error)
@@ -48,6 +50,7 @@ const Input = ({
<HeadlessInput
type={type}
value={value}
+ maxLength={maxLength}
name={name}
onChange={onChange}
onKeyDown={onKeyDown}
diff --git a/app/ui/Select.tsx b/app/ui/Select.tsx
index bf8dfe32d..3dd9547bb 100644
--- a/app/ui/Select.tsx
+++ b/app/ui/Select.tsx
@@ -1,25 +1,25 @@
-import React, { Fragment, memo } from 'react'
+import React, { Fragment, Key, memo } from 'react'
import cx from 'clsx'
import { Listbox, Transition } from '@headlessui/react'
import { CheckIcon, ChevronUpDownIcon } from '@heroicons/react/24/solid'
import _map from 'lodash/map'
-interface ISelect {
+interface ISelect<T> {
title?: string
label?: string
className?: string
labelClassName?: string
buttonClassName?: string
capitalise?: boolean
- items: any[]
+ items: T[]
id?: string
- labelExtractor?: (item: any, index: number) => string
- keyExtractor?: (item: any, index: number) => string
- iconExtractor?: (item: any, index: number) => JSX.Element | null
- onSelect: (item: any) => void
+ labelExtractor?: (item: T, index: number) => string
+ keyExtractor?: (item: T, index: number) => string
+ iconExtractor?: (item: T, index: number) => React.ReactNode | null
+ onSelect: (item: string) => void
}
-const Select = ({
+function Select<T>({
title,
label,
className,
@@ -32,97 +32,99 @@ const Select = ({
buttonClassName,
capitalise,
labelClassName,
-}: ISelect): JSX.Element => (
- // @ts-ignore
- <Listbox id={id || ''} value={title} onChange={onSelect}>
- {({ open }) => (
- <>
- <Listbox.Label className='block whitespace-pre-line text-sm font-medium text-gray-700 dark:text-gray-100'>
- {label}
- </Listbox.Label>
- <div className={cx('relative mt-1', className)}>
- <Listbox.Button
- className={cx(
- 'relative w-full cursor-default rounded-md border border-gray-300 bg-white py-2 pl-3 pr-10 text-left shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:border-gray-800 dark:bg-slate-800 dark:text-gray-50 sm:text-sm',
- buttonClassName,
- )}
- >
- <span
- className={cx('block truncate', {
- 'first-letter:capitalize': capitalise,
- })}
+}: ISelect<T>): React.ReactNode {
+ return (
+ // @ts-ignore
+ <Listbox id={id || ''} value={title} onChange={onSelect}>
+ {({ open }) => (
+ <>
+ <Listbox.Label className='block whitespace-pre-line text-sm font-medium text-gray-700 dark:text-gray-100'>
+ {label}
+ </Listbox.Label>
+ <div className={cx('relative mt-1', className)}>
+ <Listbox.Button
+ className={cx(
+ 'relative w-full cursor-default rounded-md border border-gray-300 bg-white py-2 pl-3 pr-10 text-left shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:border-gray-800 dark:bg-slate-800 dark:text-gray-50 sm:text-sm',
+ buttonClassName,
+ )}
>
- {title}
- </span>
- <span className='pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2'>
- <ChevronUpDownIcon className='h-5 w-5 text-gray-400' aria-hidden='true' />
- </span>
- </Listbox.Button>
+ <span
+ className={cx('block truncate', {
+ 'first-letter:capitalize': capitalise,
+ })}
+ >
+ {title}
+ </span>
+ <span className='pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2'>
+ <ChevronUpDownIcon className='h-5 w-5 text-gray-400' aria-hidden='true' />
+ </span>
+ </Listbox.Button>
- <Transition
- show={open}
- as={Fragment}
- leave='transition ease-in duration-100'
- leaveFrom='opacity-100'
- leaveTo='opacity-0'
- >
- <Listbox.Options
- static
- className='absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none dark:bg-slate-800 sm:text-sm'
+ <Transition
+ show={open}
+ as={Fragment}
+ leave='transition ease-in duration-100'
+ leaveFrom='opacity-100'
+ leaveTo='opacity-0'
>
- {_map(items, (item, index) => (
- <Listbox.Option
- key={keyExtractor ? keyExtractor(item, index) : item}
- className={({ active }) =>
- cx('relative cursor-default select-none py-2 pl-8 pr-4 dark:text-white', {
- 'bg-indigo-600 text-white': active,
- 'text-gray-900': !active,
- })
- }
- value={labelExtractor ? labelExtractor(item, index) : item}
- >
- {({ selected, active }) => (
- <>
- <span
- className={cx(
- 'block truncate',
- {
- 'font-semibold': selected,
- 'font-normal': !selected,
- 'first-letter:capitalize': capitalise,
- },
- labelClassName,
- )}
- >
- {labelExtractor ? labelExtractor(item, index) : item}
- </span>
-
- {iconExtractor && (
- <span className={cx('absolute inset-y-0 left-0 flex items-center pl-1.5')}>
- {iconExtractor(item, index)}
- </span>
- )}
-
- {selected && (
+ <Listbox.Options
+ static
+ className='absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none dark:bg-slate-800 sm:text-sm'
+ >
+ {_map(items, (item, index) => (
+ <Listbox.Option
+ key={keyExtractor ? keyExtractor(item, index) : (item as Key)}
+ className={({ active }) =>
+ cx('relative cursor-default select-none py-2 pl-8 pr-4 dark:text-white', {
+ 'bg-indigo-600 text-white': active,
+ 'text-gray-900': !active,
+ })
+ }
+ value={labelExtractor ? labelExtractor(item, index) : item}
+ >
+ {({ selected, active }) => (
+ <>
<span
- className={cx('absolute inset-y-0 left-0 flex items-center pl-1.5', {
- 'text-white': active,
- 'text-indigo-600': !active,
- })}
+ className={cx(
+ 'block truncate',
+ {
+ 'font-semibold': selected,
+ 'font-normal': !selected,
+ 'first-letter:capitalize': capitalise,
+ },
+ labelClassName,
+ )}
>
- <CheckIcon className='h-5 w-5' aria-hidden='true' />
+ {labelExtractor ? labelExtractor(item, index) : (item as React.ReactNode)}
</span>
- )}
- </>
- )}
- </Listbox.Option>
- ))}
- </Listbox.Options>
- </Transition>
- </div>
- </>
- )}
- </Listbox>
-)
-export default memo(Select)
+ {iconExtractor && (
+ <span className={cx('absolute inset-y-0 left-0 flex items-center pl-1.5')}>
+ {iconExtractor(item, index)}
+ </span>
+ )}
+
+ {selected && (
+ <span
+ className={cx('absolute inset-y-0 left-0 flex items-center pl-1.5', {
+ 'text-white': active,
+ 'text-indigo-600': !active,
+ })}
+ >
+ <CheckIcon className='h-5 w-5' aria-hidden='true' />
+ </span>
+ )}
+ </>
+ )}
+ </Listbox.Option>
+ ))}
+ </Listbox.Options>
+ </Transition>
+ </div>
+ </>
+ )}
+ </Listbox>
+ )
+}
+
+export default memo(Select) as typeof Select