-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: input multiselect adds inicial base
- Loading branch information
Showing
8 changed files
with
299 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,2 @@ | ||
import get from "lodash.get"; | ||
import isEqual from "lodash.isequal"; | ||
import { ChevronsUpDown } from "lucide-react"; | ||
import { useCallback, useMemo, useState } from "react"; | ||
import type { FieldPath } from "react-hook-form"; | ||
import { cn } from "~/lib/utils"; | ||
import { Button } from "../../button"; | ||
import * as Popover from "../../popover"; | ||
import { SelectorContent } from "./content"; | ||
import type { SelectorCommonProps, TOption } from "./model"; | ||
import { useSelectorHelpers } from "./hooks"; | ||
|
||
export interface SelectorProps< | ||
O extends TOption, | ||
VP extends FieldPath<O>, | ||
TV = O[VP], | ||
> extends SelectorCommonProps { | ||
options: O[]; | ||
labelPath: FieldPath<O>; | ||
valuePath: VP; | ||
value: TV; | ||
onChange?(value?: TV): void; | ||
} | ||
|
||
export function Selector<O extends TOption, VP extends FieldPath<O>>( | ||
{ extraActions, onChange = () => {}, value, ...props }: SelectorProps<O, VP>, | ||
// TODO: verificar onde por esse ref dentro do button | ||
ref: React.ForwardedRef<HTMLButtonElement>, | ||
) { | ||
const [width, setWidth] = useState(1); | ||
const { getLabel, getValue } = useSelectorHelpers<O>( | ||
props.labelPath, | ||
props.valuePath, | ||
); | ||
|
||
const item = (option: O) => get(option, props.labelPath); | ||
|
||
const selectedOption = useMemo(() => { | ||
return props.options.find((option) => isEqual(getValue(option), value)); | ||
}, [props.options, value, getValue]); | ||
|
||
const onSelect = useCallback( | ||
(option: O) => { | ||
if (!onChange) return; | ||
|
||
const optionValue = get(option, props.valuePath); | ||
|
||
onChange(isEqual(optionValue, value) ? undefined : optionValue); | ||
}, | ||
[onChange, props.valuePath, value], | ||
); | ||
|
||
const getIsSelected = useCallback( | ||
(option: O) => isEqual(getValue(option), value), | ||
[value, getValue], | ||
); | ||
|
||
return ( | ||
<Popover.Root> | ||
<Popover.Trigger asChild> | ||
<Button | ||
variant="outline" | ||
role="combobox" | ||
className={cn( | ||
"w-full justify-between", | ||
!selectedOption && "text-gray-9 dark:text-graydark-9", | ||
)} | ||
ref={(ref) => setWidth(ref?.getBoundingClientRect().width || 1)} | ||
> | ||
<span> | ||
{selectedOption ? getLabel(selectedOption) : props.placeholder} | ||
</span> | ||
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" /> | ||
</Button> | ||
</Popover.Trigger> | ||
<SelectorContent | ||
getLabel={getLabel} | ||
getValue={getValue} | ||
options={props.options} | ||
onSelect={onSelect} | ||
getIsSelect={getIsSelected} | ||
searchable={props.searchable} | ||
width={width} | ||
/> | ||
</Popover.Root> | ||
); | ||
} | ||
export { SingleSelector, type SingleSelectorProps } from "./single-selector"; | ||
export { MultiSelector, type MultiSelectorProps } from "./multi-selector"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import get from "lodash.get"; | ||
import isEqual from "lodash.isequal"; | ||
import { Check, CheckSquare2, ChevronsUpDown, Square } from "lucide-react"; | ||
import { useCallback, useMemo, useState } from "react"; | ||
import type { FieldPath } from "react-hook-form"; | ||
import { cn } from "~/lib/utils"; | ||
import { Button } from "../../button"; | ||
import * as Popover from "../../popover"; | ||
import { SelectorContent } from "./content"; | ||
import type { SelectorCommonProps, TOption } from "./model"; | ||
|
||
export type MultiSelectorProps< | ||
O extends TOption, | ||
VP extends FieldPath<O>, | ||
TV = O[VP], | ||
> = SelectorCommonProps & { | ||
options: O[]; | ||
labelPath: FieldPath<O>; | ||
valuePath: VP; | ||
value: TV[]; | ||
onChange?(value?: TV[]): void; | ||
}; | ||
|
||
export function MultiSelector<O extends TOption, VP extends FieldPath<O>>( | ||
{ | ||
extraActions, | ||
onChange = () => {}, | ||
value = [], | ||
...props | ||
}: MultiSelectorProps<O, VP>, | ||
// TODO: verificar onde por esse ref dentro do button | ||
ref: React.ForwardedRef<HTMLButtonElement>, | ||
) { | ||
const [width, setWidth] = useState(1); | ||
const getValue = useCallback( | ||
(option: O) => { | ||
return get(option, props.valuePath); | ||
}, | ||
[props.valuePath], | ||
); | ||
const getLabel = useCallback( | ||
(option: O) => { | ||
return get(option, props.labelPath); | ||
}, | ||
[props.labelPath], | ||
); | ||
|
||
const getIsSelected = useCallback( | ||
(option: O) => { | ||
return (value || []).some((it) => isEqual(getValue(option), it)); | ||
}, | ||
[value, getValue], | ||
); | ||
|
||
const selectedOptions = useMemo(() => { | ||
return props.options.filter(getIsSelected); | ||
}, [props.options, getIsSelected]); | ||
|
||
const onSelect = useCallback( | ||
(option: O) => { | ||
if (!onChange) return; | ||
|
||
const v = value || []; | ||
|
||
const optionValue = get(option, props.valuePath); | ||
|
||
if (value.includes(optionValue)) { | ||
onChange(v.filter((it) => it !== optionValue)); | ||
return; | ||
} | ||
|
||
onChange(v.concat(optionValue)); | ||
}, | ||
[onChange, props.valuePath, value], | ||
); | ||
|
||
return ( | ||
<Popover.Root> | ||
<Popover.Trigger asChild> | ||
<Button | ||
variant="outline" | ||
role="combobox" | ||
className={cn( | ||
"w-full justify-between", | ||
!selectedOptions && "text-gray-9 dark:text-graydark-9", | ||
)} | ||
ref={(ref) => setWidth(ref?.getBoundingClientRect().width || 1)} | ||
> | ||
<span> | ||
{selectedOptions | ||
? selectedOptions.map((option) => getLabel(option)).join(", ") | ||
: props.placeholder} | ||
</span> | ||
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" /> | ||
</Button> | ||
</Popover.Trigger> | ||
<SelectorContent | ||
getLabel={getLabel} | ||
getValue={getValue} | ||
options={props.options} | ||
onSelect={onSelect} | ||
getIsSelect={getIsSelected} | ||
searchable={props.searchable} | ||
width={width} | ||
message={{ | ||
...props.messages, | ||
optionSelected: <CheckSquare2 className={cn("mr-2 h-4 w-4")} />, | ||
optionUnselected: <Square className={cn("mr-2 h-4 w-4")} />, | ||
}} | ||
/> | ||
</Popover.Root> | ||
); | ||
} |
Oops, something went wrong.