Better radio dropdown type

This commit is contained in:
Gregory Schier
2023-03-20 16:54:26 -07:00
parent f855d8ab16
commit 4b0793ebef
3 changed files with 17 additions and 22 deletions

View File

@@ -27,7 +27,7 @@ export function RequestPane({ fullHeight, className }: Props) {
defaultValue: 'body', defaultValue: 'body',
}); });
const tabs: TabItem[] = useMemo( const tabs: TabItem<string | null>[] = useMemo(
() => [ () => [
{ {
value: 'body', value: 'body',

View File

@@ -1,26 +1,21 @@
import { memo, useMemo } from 'react'; import { useMemo } from 'react';
import type { DropdownProps } from './Dropdown'; import type { DropdownProps } from './Dropdown';
import { Dropdown } from './Dropdown'; import { Dropdown } from './Dropdown';
import { Icon } from './Icon'; import { Icon } from './Icon';
export interface RadioDropdownItem { export interface RadioDropdownItem<T> {
label: string; label: string;
value: string | null; value: T;
} }
export interface RadioDropdownProps { export interface RadioDropdownProps<T> {
value: string | null; value: T;
onChange: (value: string | null) => void; onChange: (value: T) => void;
items: RadioDropdownItem[]; items: RadioDropdownItem<T>[];
children: DropdownProps['children']; children: DropdownProps['children'];
} }
export const RadioDropdown = memo(function RadioDropdown({ export function RadioDropdown<T>({ value, items, onChange, children }: RadioDropdownProps<T>) {
value,
items,
onChange,
children,
}: RadioDropdownProps) {
const dropdownItems = useMemo( const dropdownItems = useMemo(
() => () =>
items.map(({ label, value: v }) => ({ items.map(({ label, value: v }) => ({
@@ -32,4 +27,4 @@ export const RadioDropdown = memo(function RadioDropdown({
); );
return <Dropdown items={dropdownItems}>{children}</Dropdown>; return <Dropdown items={dropdownItems}>{children}</Dropdown>;
}); }

View File

@@ -9,23 +9,23 @@ import { HStack } from '../Stacks';
import './Tabs.css'; import './Tabs.css';
export type TabItem = { export type TabItem<T> = {
value: string; value: string;
label: string; label: string;
options?: Omit<RadioDropdownProps, 'children'>; options?: Omit<RadioDropdownProps<T>, 'children'>;
}; };
interface Props { interface Props<T = unknown> {
label: string; label: string;
value?: string; value?: string;
onChangeValue: (value: string) => void; onChangeValue: (value: string) => void;
tabs: TabItem[]; tabs: TabItem<T>[];
tabListClassName?: string; tabListClassName?: string;
className?: string; className?: string;
children: ReactNode; children: ReactNode;
} }
export const Tabs = memo(function Tabs({ export function Tabs<T>({
value, value,
onChangeValue, onChangeValue,
label, label,
@@ -33,7 +33,7 @@ export const Tabs = memo(function Tabs({
tabs, tabs,
className, className,
tabListClassName, tabListClassName,
}: Props) { }: Props<T>) {
const ref = useRef<HTMLDivElement | null>(null); const ref = useRef<HTMLDivElement | null>(null);
const handleTabChange = (value: string) => { const handleTabChange = (value: string) => {
@@ -109,7 +109,7 @@ export const Tabs = memo(function Tabs({
{children} {children}
</div> </div>
); );
}); }
interface TabContentProps { interface TabContentProps {
value: string; value: string;