From 0476a294d60a0c688d82af521650166a5fc1ae96 Mon Sep 17 00:00:00 2001 From: Eric NGUYEN Date: Fri, 16 Sep 2022 16:23:17 +0200 Subject: [PATCH] Implement Category list --- src/Components/Category/Category.tsx | 54 +++++++++------------------- src/Components/Editor/Editor.tsx | 1 + src/Components/Sidebar/Sidebar.tsx | 31 ++++++++++------ src/Components/UI/UI.tsx | 3 ++ src/Interfaces/ICategory.ts | 5 +++ src/Interfaces/IConfiguration.ts | 2 ++ test-server/http.js | 16 +++++++-- 7 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 src/Interfaces/ICategory.ts diff --git a/src/Components/Category/Category.tsx b/src/Components/Category/Category.tsx index 2b30935..c8cdafa 100644 --- a/src/Components/Category/Category.tsx +++ b/src/Components/Category/Category.tsx @@ -1,58 +1,38 @@ import { ChevronRightIcon } from '@heroicons/react/outline'; import React, { useState } from 'react'; +import { ICategory } from '../../Interfaces/ICategory'; import { TruncateString } from '../../utils/stringtools'; interface ICategoryProps { children: JSX.Element | JSX.Element[] - category: string + category: ICategory } export function Category(props: ICategoryProps): JSX.Element { const [isOpen, setIsOpen] = useState(false); - if (isOpen) { - return ( -
-
setIsOpen(!isOpen)} - > - - {TruncateString(props.category, 25)} - - - - -
-
- { props.children } -
-
- ); - } + const categoryType: string = props.category.Type; + const categoryDisplayedText: string = props.category.DisplayedText ?? categoryType; return (
setIsOpen(!isOpen)} + className={` transition-all text-center ${isOpen ? 'overflow-hidden h-full' : 'overflow-visible h-16'}`} + key={categoryType} + id={categoryType} + title={categoryDisplayedText} > -
- - {TruncateString(props.category, 25)} +
setIsOpen(!isOpen)} + > + + {TruncateString(categoryDisplayedText, 25)} - - + +
-
+
{ props.children }
diff --git a/src/Components/Editor/Editor.tsx b/src/Components/Editor/Editor.tsx index 6411fa3..239f0ce 100644 --- a/src/Components/Editor/Editor.tsx +++ b/src/Components/Editor/Editor.tsx @@ -210,6 +210,7 @@ export function Editor(props: IEditorProps): JSX.Element { historyCurrentStep={historyCurrentStep} availableContainers={configuration.AvailableContainers} availableSymbols={configuration.AvailableSymbols} + categories={configuration.Categories} selectContainer={(container) => setNewHistory( SelectContainer( container, diff --git a/src/Components/Sidebar/Sidebar.tsx b/src/Components/Sidebar/Sidebar.tsx index cf695de..ac2f7a4 100644 --- a/src/Components/Sidebar/Sidebar.tsx +++ b/src/Components/Sidebar/Sidebar.tsx @@ -1,10 +1,12 @@ import * as React from 'react'; import { IAvailableContainer } from '../../Interfaces/IAvailableContainer'; +import { ICategory } from '../../Interfaces/ICategory'; import { TruncateString } from '../../utils/stringtools'; import { Category } from '../Category/Category'; interface ISidebarProps { componentOptions: IAvailableContainer[] + categories: ICategory[] isOpen: boolean buttonOnClick: (type: string) => void } @@ -15,7 +17,12 @@ function HandleDragStart(event: React.DragEvent): void { export function Sidebar(props: ISidebarProps): JSX.Element { const rootElements: Array = []; - const categories = new Map(); + const categories = new Map(); + const categoriesElements = new Map(props.categories.map(category => { + categories.set(category.Type, category); + return [category.Type, []]; + })); + // build the components props.componentOptions.forEach(componentOption => { if (componentOption.IsHidden === true) { @@ -39,25 +46,27 @@ export function Sidebar(props: ISidebarProps): JSX.Element { return; } - if (categories.get(componentOption.Category) === undefined) { - categories.set(componentOption.Category, []); - } - - const category = categories.get(componentOption.Category); - + const category = categoriesElements.get(componentOption.Category); if (category === undefined) { - // should never go here + console.error(`[Category] Category does not exists in configuration.Categories: ${componentOption.Category}`); return; } - category.push(componentButton); }); // build the categories - categories.forEach((options, category) => { + categoriesElements.forEach((options, categoryName) => { + const category = categories.get(categoryName); + + if (category === undefined) { + // should never go here + console.error(`[Category] Category does not exists in configuration.Categories: ${categoryName}`); + return; + } + rootElements.unshift( { options } diff --git a/src/Components/UI/UI.tsx b/src/Components/UI/UI.tsx index 3561453..702ced8 100644 --- a/src/Components/UI/UI.tsx +++ b/src/Components/UI/UI.tsx @@ -13,6 +13,7 @@ import { Symbols } from '../Symbols/Symbols'; import { SymbolsSidebar } from '../SymbolsSidebar/SymbolsSidebar'; import { PropertyType } from '../../Enums/PropertyType'; import { MessagesSidebar } from '../MessagesSidebar/MessagesSidebar'; +import { ICategory } from '../../Interfaces/ICategory'; interface IUIProps { selectedContainer: IContainerModel | undefined @@ -21,6 +22,7 @@ interface IUIProps { historyCurrentStep: number availableContainers: IAvailableContainer[] availableSymbols: IAvailableSymbol[] + categories: ICategory[] selectContainer: (containerId: string) => void deleteContainer: (containerId: string) => void onPropertyChange: (key: string, value: string | number | boolean, type?: PropertyType) => void @@ -83,6 +85,7 @@ export function UI(props: IUIProps): JSX.Element { AvailableSymbols: IAvailableSymbol[] // TODO: Use a Map + Categories: ICategory[] Patterns: IPattern[] MainContainer: IAvailableContainer } diff --git a/test-server/http.js b/test-server/http.js index a729545..c170204 100644 --- a/test-server/http.js +++ b/test-server/http.js @@ -77,7 +77,7 @@ const GetSVGLayoutConfiguration = () => { }, ShowSelfDimensions: true, IsDimensionBorrower: true, - Category: "Category" + Category: "Stuff" }, { Type: 'Trou', @@ -96,11 +96,11 @@ const GetSVGLayoutConfiguration = () => { stroke: 'green', fill: 'white' }, - Category: "Category" + Category: "Stuff" }, { Type: 'Remplissage', - Category: "Category2", + Category: "Other stuff", CustomSVG: ` @@ -204,6 +204,16 @@ const GetSVGLayoutConfiguration = () => { XPositionReference: 0 } ], + Categories: [ + { + Type: "Stuff", + DisplayedText: "Stuff made here" + }, + { + Type: "Other stuff", + DisplayedText: "Stuff not made here" + } + ], MainContainer: { Height: 200, Width: 800