Skip to content

Commit

Permalink
[cleanup] Make sirius-components-trees use the strict version of our …
Browse files Browse the repository at this point in the history
…TypeScript configuration

Signed-off-by: Stéphane Bégaudeau <[email protected]>
  • Loading branch information
sbegaudeau committed Jan 6, 2025
1 parent 7276000 commit ee08015
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Specifiers can contribute dedicated AQL services for this feature using implemen

- https://github.com/eclipse-sirius/sirius-web/issues/4368[#4368] [sirius-web] Add GraphQL subscription exception handler
- [charts] Make the npm package `sirius-components-charts` use the strict version of our TypeScript configuration
- [trees] Make the npm package `sirius-components-trees` use the strict version of our TypeScript configuration


== v2025.1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023, 2024 Obeo.
* Copyright (c) 2023, 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -46,11 +46,6 @@ export const TreeToolBar = ({
}: TreeToolBarProps) => {
const { classes } = useTreeToolbarStyles();

let treeFiltersMenu: JSX.Element;
if (treeFilters.length > 0) {
treeFiltersMenu = <TreeFiltersMenu filters={treeFilters} onTreeFilterMenuItemClick={onTreeFilterMenuItemClick} />;
}

const preferenceButtonSynchronizeTitle = synchronized
? 'Disable synchronization with representation'
: 'Enable synchronization with representation';
Expand All @@ -66,7 +61,9 @@ export const TreeToolBar = ({
const element = React.createElement(component, props);
return element;
})}
{treeFiltersMenu}
{treeFilters.length > 0 ? (
<TreeFiltersMenu filters={treeFilters} onTreeFilterMenuItemClick={onTreeFilterMenuItemClick} />
) : null}
{children}
<IconButton
color="inherit"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2024 Obeo.
* Copyright (c) 2019, 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -123,19 +123,15 @@ export const TreeItem = ({
markedItemIds,
treeItemActionRender,
}: TreeItemProps) => {
const { classes } = useTreeItemStyle({ depth });

const initialState: TreeItemState = {
const [state, setState] = useState<TreeItemState>({
editingMode: false,
editingKey: null,
partHovered: null,
};

const [state, setState] = useState<TreeItemState>(initialState);
const { editingMode } = state;
});

const refDom = useRef() as any;

const { classes } = useTreeItemStyle({ depth });
const { selection, setSelection } = useSelection();
const { onDropTreeItem } = useDropTreeItem(editingContextId, treeId);

Expand All @@ -161,7 +157,7 @@ export const TreeItem = ({
}));
};

let content = null;
let content: JSX.Element | null = null;
if (item.expanded && item.children) {
content = (
<ul className={classes.ul}>
Expand Down Expand Up @@ -191,7 +187,7 @@ export const TreeItem = ({
}

let className = classes.treeItem;
let dataTestid = undefined;
let dataTestid: string | undefined = undefined;

const selected = selection.entries.find((entry) => entry.id === item.id);
if (selected) {
Expand Down Expand Up @@ -225,7 +221,7 @@ export const TreeItem = ({
};

const marked: boolean = markedItemIds.some((id) => id === item.id);
if (editingMode) {
if (state.editingMode) {
text = (
<TreeItemDirectEditInput
editingContextId={editingContextId}
Expand All @@ -238,7 +234,7 @@ export const TreeItem = ({
const styledLabelProps = {
styledString: item.label,
selected: false,
textToHighlight: textToHighlight,
textToHighlight: textToHighlight ?? '',
marked: marked,
};
text = <StyledLabel {...styledLabelProps}></StyledLabel>;
Expand Down Expand Up @@ -277,7 +273,7 @@ export const TreeItem = ({
};

const onBeginEditing = (event) => {
if (!item.editable || editingMode || readOnly || !event.currentTarget.contains(event.target as HTMLElement)) {
if (!item.editable || state.editingMode || readOnly || !event.currentTarget.contains(event.target as HTMLElement)) {
return;
}
const { key } = event;
Expand Down Expand Up @@ -335,7 +331,7 @@ export const TreeItem = ({
const query = item.kind.substring(item.kind.indexOf('?') + 1, item.kind.length);
const params = new URLSearchParams(query);
if (params.has('type')) {
tooltipText = params.get('type');
tooltipText = params.get('type') ?? 'representation';
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* Copyright (c) 2024, 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -10,10 +10,10 @@
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import IconButton from '@mui/material/IconButton';
import { makeStyles } from 'tss-react/mui';
import MoreVertIcon from '@mui/icons-material/MoreVert';
import IconButton from '@mui/material/IconButton';
import { useState } from 'react';
import { makeStyles } from 'tss-react/mui';
import { getString } from './TreeItem';
import { TreeItemActionProps, TreeItemActionState } from './TreeItemAction.types';
import { TreeItemContextMenu } from './TreeItemContextMenu';
Expand Down Expand Up @@ -56,8 +56,8 @@ export const TreeItemAction = ({
}
};

let contextMenu = null;
if (state.showContextMenu) {
let contextMenu: JSX.Element | null = null;
if (state.showContextMenu && state.menuAnchor) {
const closeContextMenu = () => {
setState((prevState) => ({
...prevState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023, 2024 Obeo.
* Copyright (c) 2023, 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -85,24 +85,26 @@ export const TreeItemDirectEditInput = ({
});

useEffect(() => {
let cleanup = undefined;
if (initialLabelTreeItemItemError) {
addErrorMessage('An unexpected error has occurred, please refresh the page');
}
if (initialLabelTreeItemItemData?.viewer.editingContext.representation.description.initialDirectEditTreeItemLabel) {
const initialLabel =
initialLabelTreeItemItemData?.viewer.editingContext.representation.description.initialDirectEditTreeItemLabel;
const initialLabel =
initialLabelTreeItemItemData?.viewer.editingContext.representation.description.initialDirectEditTreeItemLabel;
if (initialLabel) {
if (!editingKey) {
setState((prevState) => {
return { ...prevState, newLabel: initialLabel };
});
const timeOutId = setTimeout(() => {
textInput.current.select();
if (textInput.current) {
textInput.current.select();
}
}, 0);
cleanup = () => clearTimeout(timeOutId);

return () => clearTimeout(timeOutId);
}
}
return cleanup;
return () => {};
}, [initialLabelTreeItemItemError, initialLabelTreeItemItemData]);

const [renameTreeItem, { data: renameTreeItemData, error: renameTreeItemError }] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023, 2024 Obeo.
* Copyright (c) 2023, 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -15,7 +15,7 @@ export interface TreeItemDirectEditInputProps {
editingContextId: string;
treeId: string;
treeItemId: string;
editingKey: string;
editingKey: string | null;
onClose: () => void;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023, 2024 Obeo.
* Copyright (c) 2023, 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -22,12 +22,12 @@ export const isFilterCandidate = (treeItem: GQLTreeItem, textToFilter: string |
filter = false;
} else if (
!treeItem.hasChildren &&
splitLabelWithTextToHighlight.length === 1 &&
splitLabelWithTextToHighlight[0] &&
splitLabelWithTextToHighlight[0].toLocaleLowerCase() !== textToFilter.toLocaleLowerCase()
) {
filter = true;
} else if (
splitLabelWithTextToHighlight.length === 1 &&
splitLabelWithTextToHighlight[0] &&
splitLabelWithTextToHighlight[0].toLocaleLowerCase() === textToFilter.toLocaleLowerCase()
) {
filter = false;
Expand Down
30 changes: 16 additions & 14 deletions packages/trees/frontend/sirius-components-trees/src/trees/Tree.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2024 Obeo.
* Copyright (c) 2019, 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -36,7 +36,7 @@ export const Tree = ({
treeItemActionRender,
}: TreeProps) => {
const { classes } = useTreeStyle();
const treeElement = useRef(null);
const treeElement = useRef<HTMLDivElement | null>(null);

useEffect(() => {
const downHandler = (event) => {
Expand All @@ -50,42 +50,44 @@ export const Tree = ({
event.preventDefault();

const previousItem = document.activeElement as HTMLElement;
const dataset = (previousItem as any).dataset;
const dataset = previousItem.dataset;
if (dataset.treeitemid) {
const treeItemDomElements = document.querySelectorAll<HTMLElement>('[data-treeitemid]');
const index = Array.from(treeItemDomElements).indexOf(previousItem);
const id = dataset.treeitemid;
const hasChildren = dataset.haschildren === 'true';
const isExpanded = dataset.expanded === 'true';
const depth: number = parseInt(dataset.depth ?? '0');

switch (event.key) {
case 'ArrowLeft':
if (hasChildren && isExpanded) {
onExpand(id, dataset.depth);
onExpand(id, depth);
} else if (index > 0) {
const parentDepth = (dataset.depth - 1).toString();
let positionFromParent = 0;
while (!(treeItemDomElements[index - positionFromParent].dataset.depth === parentDepth)) {
const parentDepth = (depth - 1).toString();

let positionFromParent: number = 0;
while (!(treeItemDomElements[index - positionFromParent]?.dataset.depth === parentDepth)) {
positionFromParent++;
}
treeItemDomElements[index - positionFromParent].click();
treeItemDomElements[index - positionFromParent]?.click();
}
break;
case 'ArrowRight':
if (hasChildren && !isExpanded) {
onExpand(id, dataset.depth);
onExpand(id, depth);
} else if (index < treeItemDomElements.length - 1) {
treeItemDomElements[index + 1].click();
treeItemDomElements[index + 1]?.click();
}
break;
case 'ArrowUp':
if (index > 0) {
treeItemDomElements[index - 1].click();
treeItemDomElements[index - 1]?.click();
}
break;
case 'ArrowDown':
if (index < treeItemDomElements.length - 1) {
treeItemDomElements[index + 1].click();
treeItemDomElements[index + 1]?.click();
}
break;
default:
Expand All @@ -94,15 +96,15 @@ export const Tree = ({
}
};

const element = treeElement?.current;
const element = treeElement.current;
if (element) {
element.addEventListener('keydown', downHandler);

return () => {
element.removeEventListener('keydown', downHandler);
};
}
return null;
return () => {};
}, [treeElement, onExpand]);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@eclipse-sirius/sirius-components-tsconfig/react-library.json",
"extends": "@eclipse-sirius/sirius-components-tsconfig/strict-react-library.json",
"compilerOptions": {
"emitDeclarationOnly": true,
"outDir": "./dist"
Expand Down

0 comments on commit ee08015

Please sign in to comment.