* wip: add oldmarr config import
* wip: add support for wrong amount of categories / sections with autofix, color mapping, position adjustments of wrappers
* fix: lockfile broken
* feat: add support for form data trpc requests
* wip: improve file upload
* refactor: restructure import, add import configuration
* wip: add configurations for import to modal
* refactor: move oldmarr import to old-import package
* fix: column count not respects screen size for board
* feat: add beta badge for oldmarr config import
* chore: address pull request feedback
* fix: format issues
* fix: inconsistent versions
* fix: deepsource issues
* fix: revert {} to Record<string, never> convertion to prevent typecheck issue
* fix: inconsistent zod version
* fix: format issue
* chore: address pull request feedback
* fix: wrong import
* fix: broken lock file
* fix: inconsistent versions
* fix: format issues
89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useMemo } from "react";
|
|
import type { SelectProps } from "@mantine/core";
|
|
import { Combobox, Input, InputBase, useCombobox } from "@mantine/core";
|
|
import { useUncontrolled } from "@mantine/hooks";
|
|
|
|
interface BaseSelectItem {
|
|
value: string;
|
|
label: string;
|
|
}
|
|
|
|
export interface SelectWithCustomItemsProps<TSelectItem extends BaseSelectItem>
|
|
extends Pick<SelectProps, "label" | "error" | "defaultValue" | "value" | "onChange" | "placeholder"> {
|
|
data: TSelectItem[];
|
|
description?: string;
|
|
withAsterisk?: boolean;
|
|
onBlur?: (event: React.FocusEvent<HTMLButtonElement>) => void;
|
|
onFocus?: (event: React.FocusEvent<HTMLButtonElement>) => void;
|
|
}
|
|
|
|
type Props<TSelectItem extends BaseSelectItem> = SelectWithCustomItemsProps<TSelectItem> & {
|
|
SelectOption: React.ComponentType<TSelectItem>;
|
|
};
|
|
|
|
export const SelectWithCustomItems = <TSelectItem extends BaseSelectItem>({
|
|
data,
|
|
onChange,
|
|
value,
|
|
defaultValue,
|
|
placeholder,
|
|
SelectOption,
|
|
...props
|
|
}: Props<TSelectItem>) => {
|
|
const combobox = useCombobox({
|
|
onDropdownClose: () => combobox.resetSelectedOption(),
|
|
});
|
|
|
|
const [_value, setValue] = useUncontrolled({
|
|
value,
|
|
defaultValue,
|
|
finalValue: null,
|
|
onChange,
|
|
});
|
|
|
|
const selectedOption = useMemo(() => data.find((item) => item.value === _value), [data, _value]);
|
|
|
|
const options = data.map((item) => (
|
|
<Combobox.Option value={item.value} key={item.value}>
|
|
<SelectOption {...item} />
|
|
</Combobox.Option>
|
|
));
|
|
|
|
const toggle = useCallback(() => combobox.toggleDropdown(), [combobox]);
|
|
const onOptionSubmit = useCallback(
|
|
(value: string) => {
|
|
setValue(
|
|
value,
|
|
data.find((item) => item.value === value),
|
|
);
|
|
combobox.closeDropdown();
|
|
},
|
|
[setValue, data, combobox],
|
|
);
|
|
|
|
return (
|
|
<Combobox store={combobox} withinPortal={false} onOptionSubmit={onOptionSubmit}>
|
|
<Combobox.Target>
|
|
<InputBase
|
|
{...props}
|
|
component="button"
|
|
type="button"
|
|
pointer
|
|
rightSection={<Combobox.Chevron />}
|
|
onClick={toggle}
|
|
rightSectionPointerEvents="none"
|
|
multiline
|
|
>
|
|
{selectedOption ? <SelectOption {...selectedOption} /> : <Input.Placeholder>{placeholder}</Input.Placeholder>}
|
|
</InputBase>
|
|
</Combobox.Target>
|
|
|
|
<Combobox.Dropdown>
|
|
<Combobox.Options>{options}</Combobox.Options>
|
|
</Combobox.Dropdown>
|
|
</Combobox>
|
|
);
|
|
};
|