Skip to content

Commit

Permalink
chore: slots components refactor & add condition icons
Browse files Browse the repository at this point in the history
  • Loading branch information
mxerf committed Sep 24, 2024
1 parent 2ce3b47 commit 2809b37
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 623 deletions.
40 changes: 13 additions & 27 deletions frontend/src/UI/Input/DefCombobox.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Popover from "@radix-ui/react-popover"
import classNames from "classnames"
import { CheckIcon } from "lucide-react"
import React, { ReactNode, useEffect, useRef, useState } from "react"
import React, { ReactNode, useCallback, useEffect, useRef, useState } from "react"

interface ComboboxProps {
items: string[]
Expand Down Expand Up @@ -35,11 +35,14 @@ const DefCombobox: React.FC<ComboboxProps> = ({
setIsOpen(true)
}

const handleSelectItem = (item: string) => {
setInputValue(item)
setSelected(item)
setIsOpen(false)
}
const handleSelectItem = useCallback(
(item: string) => {
setInputValue(item)
setSelected(item)
setIsOpen(false)
},
[setSelected]
)

useEffect(() => {
if (isOpen && inputRef.current) {
Expand All @@ -50,17 +53,13 @@ const DefCombobox: React.FC<ComboboxProps> = ({
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (isOpen) {
console.log(e.key, highlightedIndex)
if (e.key === "ArrowDown") {
console.log("arrow down")
setHighlightedIndex((prev) => Math.min(prev + 1, filteredItems.length - 1))
e.preventDefault() // Предотвращаем прокрутку страницы
} else if (e.key === "ArrowUp") {
console.log("arrow up")
setHighlightedIndex((prev) => Math.max(prev - 1, 0))
e.preventDefault() // Предотвращаем прокрутку страницы
} else if (e.key === "Enter" && highlightedIndex >= 0) {
console.log("enter")
handleSelectItem(filteredItems[highlightedIndex])
e.preventDefault() // Предотвращаем отправку формы, если она есть
}
Expand All @@ -71,34 +70,21 @@ const DefCombobox: React.FC<ComboboxProps> = ({
return () => {
window.removeEventListener("keydown", handleKeyDown)
}
}, [isOpen, highlightedIndex, filteredItems])

console.log(isOpen)
}, [isOpen, highlightedIndex, filteredItems, handleSelectItem])

return (
<div className='combobox-container'>
{/* Input field */}
{/* <input
ref={inputRef}
type='text'
value={inputValue}
onChange={handleInputChange}
// onFocus={() => setIsOpen(true)} // Открываем Popover при фокусе на поле ввода
placeholder={placeholder}
className='w-full bg-background p-2 rounded-lg border border-input-border'
/> */}
<div
ref={containerRef}
className='w-full flex items-center justify-between bg-background p-2 rounded-lg border border-input-border'>
className='w-full flex items-center justify-between bg-background p-2 rounded-lg border border-input-border hover:bg-bg-secondary transition-colors'>
{startContent && <span style={{ marginRight: "8px" }}>{startContent}</span>}
<input
ref={inputRef}
type='text'
value={inputValue}
onChange={handleInputChange}
// onFocus={() => setIsOpen(true)}
placeholder={placeholder}
className='w-full bg-transparent outline-none'
className='w-full bg-transparent outline-none placeholder:text-input-border text-sm'
/>
{endContent && <span style={{ marginLeft: "8px" }}>{endContent}</span>}
</div>
Expand All @@ -118,7 +104,7 @@ const DefCombobox: React.FC<ComboboxProps> = ({
style={{
width: containerRef.current?.offsetWidth ?? "320px",
}}
className={`mt-2 bg-background border border-input-border rounded-lg py-1 z-[9999] overflow-x-hidden`}>
className={`mt-2 bg-background border border-input-border rounded-lg py-1 z-[9999] overflow-x-hidden *:text-sm`}>
{filteredItems.length ? (
filteredItems.map((item, index) => (
<div
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/UI/Input/DefSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,16 @@ const DefSelect = ({
scale: 0.95,
transition: { duration: 0.2 },
}}>
<RadixSelect.Viewport className='p-1 grid gap-1 w-full'>
<RadixSelect.Viewport className='py-1 grid gap-1 w-full'>
{items.map((item) => (
<RadixSelect.Item
key={item.key}
value={item.value}
className={classNames(
`flex items-center justify-between rounded-[8px] ${mini ? "px-2 py-0.5" : "p-2"} hover:bg-input-background-disabled data-[highlighted]:bg-input-background-disabled cursor-pointer`,
`flex items-center justify-between ${mini ? "px-2 py-0.5" : "p-2"} hover:bg-input-background-disabled data-[highlighted]:bg-input-background-disabled cursor-pointer`,
{
"bg-input-background-disabled": item.value === selectedValue,
"*:text-sm": mini,
"rounded-[4px]": mini,
}
)}>
<RadixSelect.ItemText>{item.value}</RadixSelect.ItemText>
Expand Down
5 changes: 1 addition & 4 deletions frontend/src/components/nodes/SlotsNode.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from "@nextui-org/react"
import { PlusIcon } from "lucide-react"
import { memo, useContext, useEffect, useState } from "react"
import { memo, useContext, useState } from "react"
import { PopUpContext } from "../../contexts/popUpContext"
import SlotsConditionIcon from "../../icons/nodes/conditions/SlotsConditionIcon"
import EditNodeIcon from "../../icons/nodes/EditNodeIcon"
Expand All @@ -13,9 +13,6 @@ const SlotsNode = memo(({ data }: { data: SlotsNodeDataType }) => {
const { openPopUp } = useContext(PopUpContext)
const [nodeData, setNodeData] = useState(data)

useEffect(() => {
console.log(nodeData)
}, [nodeData])

const onNodeModalOpen = () => {
openPopUp(
Expand Down
133 changes: 133 additions & 0 deletions frontend/src/components/nodes/slots/SlotsGroupsTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { PopUpContext } from "@/contexts/popUpContext"
import DocumentationIcon from "@/icons/DocumentationIcon"
import NewWindowIcon from "@/icons/NewWindowIcon"
import TrashIcon from "@/icons/TrashIcon"
import AlertModal from "@/modals/AlertModal"
import SlotsGroupModal from "@/modals/SlotsModals/SlotsGroupModal"
import { SlotsGroupType } from "@/types/FlowTypes"
import { SlotsNodeDataType } from "@/types/NodeTypes"
import DefTable from "@/UI/Table/DefTable"
import { Button, Divider, TableCell, TableRow } from "@nextui-org/react"
import React, { Fragment, useContext } from "react"

type Props = {
groups: SlotsGroupType[]
setGroups: React.Dispatch<React.SetStateAction<SlotsGroupType[]>>
nodeData: SlotsNodeDataType
setNodeData: React.Dispatch<React.SetStateAction<SlotsNodeDataType>>
onDeleteGroupHandler?: (group: SlotsGroupType, updatedGroups: SlotsGroupType[]) => void
}

const SlotsGroupsTable = ({
groups,
setGroups,
nodeData,
setNodeData,
onDeleteGroupHandler = () => {},
}: Props) => {
const { openPopUp } = useContext(PopUpContext)

const handleDeleteGroup = (group: SlotsGroupType) => {
// Удаление группы
openPopUp(
<AlertModal
size={"lg"}
id={`delete-group-modal-${group.id}`}
title='Delete group'
description={`Are you sure you want to delete "${group.name}" group?`}
onAction={() => {
const updatedGroups = nodeData.groups.filter((g) => g.id !== group.id)
setGroups(updatedGroups)
onDeleteGroupHandler(group, updatedGroups)
}}
actionText='Delete'
/>,
`delete-group-modal-${group.id}`
)
}

const handleGroupModalOpen = (group: SlotsGroupType) => {
openPopUp(
<SlotsGroupModal
id={`slots-group-modal-edit-${group.id}`}
data={nodeData}
setData={setNodeData}
group={group}
is_create={false}
/>,
`slots-group-modal-edit-${group.id}`
)
}

return (
<DefTable headers={[" ", "name", "contains slots", "actions"]}>
{groups.map((group) => (
<TableRow key={group.id}>
<TableCell> </TableCell>
<TableCell>{group.name}</TableCell>
<TableCell>
<div className='flex flex-col'>
<ul className='list-disc'>
{group.slots.slice(0, 3).map((slot, idx) => (
<Fragment key={slot.id}>
{idx === 2 && group.slots.length > 3 ? (
<li className='text-sm text-gray-400'>+ {group.slots.length - 2} more</li>
) : (
idx === 2 && <li>{slot.name}</li>
)}
{idx !== 2 && <li>{slot.name}</li>}
</Fragment>
))}
</ul>
{group.subgroups && group.subgroups.length > 0 && (
<>
<Divider
orientation='horizontal'
className='my-1'
/>
<ul>
{group.subgroups.map((s) => {
const subgroup = nodeData.groups.find((g) => g.id === s)
if (subgroup) {
return (
<li
key={subgroup.id}
className='text-sm flex items-center gap-1'>
<DocumentationIcon />
{subgroup.name}
</li>
)
} else {
return <></>
}
})}
</ul>
</>
)}
</div>
</TableCell>
<TableCell>
<Button
onClick={() => handleDeleteGroup(group)}
size='sm'
isIconOnly
variant='light'
color='danger'>
<TrashIcon />
</Button>
<Button
onClick={() => handleGroupModalOpen(group)}
size='sm'
isIconOnly
variant='light'
color='default'>
<NewWindowIcon />
</Button>
</TableCell>
</TableRow>
))}
</DefTable>
)
}

export default SlotsGroupsTable
56 changes: 0 additions & 56 deletions frontend/src/components/ui/button.tsx

This file was deleted.

Loading

0 comments on commit 2809b37

Please sign in to comment.