Fix misuse of Hooks with useRef #24

Merged
Siklos merged 1 commit from dev.fixhooks into dev 2022-08-11 09:10:06 -04:00
9 changed files with 76 additions and 27 deletions

View file

@ -19,6 +19,7 @@ module.exports = {
plugins: [ plugins: [
'only-warn', 'only-warn',
'react', 'react',
'react-hooks',
'@typescript-eslint' '@typescript-eslint'
], ],
rules: { rules: {
@ -29,5 +30,7 @@ module.exports = {
'@typescript-eslint/semi': ['warn', 'always'], '@typescript-eslint/semi': ['warn', 'always'],
'no-unused-vars': 'off', 'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error', '@typescript-eslint/no-unused-vars': 'error',
'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
'react-hooks/exhaustive-deps': 'warn' // Checks effect dependencies
} }
}; };

49
package-lock.json generated
View file

@ -15,6 +15,7 @@
"react-svg-pan-zoom": "^3.11.0" "react-svg-pan-zoom": "^3.11.0"
}, },
"devDependencies": { "devDependencies": {
"@testing-library/dom": "^8.16.1",
"@testing-library/jest-dom": "^5.16.4", "@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0", "@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^14.4.1", "@testing-library/user-event": "^14.4.1",
@ -31,8 +32,10 @@
"eslint-config-standard-with-typescript": "^22.0.0", "eslint-config-standard-with-typescript": "^22.0.0",
"eslint-plugin-import": "^2.26.0", "eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.2.4", "eslint-plugin-n": "^15.2.4",
"eslint-plugin-only-warn": "^1.0.3",
"eslint-plugin-promise": "^6.0.0", "eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.30.1", "eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"jsdom": "^20.0.0", "jsdom": "^20.0.0",
"postcss": "^8.4.14", "postcss": "^8.4.14",
"sass": "^1.54.0", "sass": "^1.54.0",
@ -745,9 +748,9 @@
"dev": true "dev": true
}, },
"node_modules/@testing-library/dom": { "node_modules/@testing-library/dom": {
"version": "8.16.0", "version": "8.17.1",
"resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-8.16.0.tgz", "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-8.17.1.tgz",
"integrity": "sha512-uxF4zmnLHHDlmW4l+0WDjcgLVwCvH+OVLpD8Dfp+Bjfz85prwxWGbwXgJdLtkgjD0qfOzkJF9SmA6YZPsMYX4w==", "integrity": "sha512-KnH2MnJUzmFNPW6RIKfd+zf2Wue8mEKX0M3cpX6aKl5ZXrJM1/c/Pc8c2xDNYQCnJO48Sm5ITbMXgqTr3h4jxQ==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"@babel/code-frame": "^7.10.4", "@babel/code-frame": "^7.10.4",
@ -2879,6 +2882,15 @@
"eslint": ">=7.0.0" "eslint": ">=7.0.0"
} }
}, },
"node_modules/eslint-plugin-only-warn": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/eslint-plugin-only-warn/-/eslint-plugin-only-warn-1.0.3.tgz",
"integrity": "sha512-XQOX/TfLoLw6h8ky51d29uUjXRTQHqBGXPylDEmy5fe/w7LIOnp8MA24b1OSMEn9BQoKow1q3g1kLe5/9uBTvw==",
"dev": true,
"engines": {
"node": ">=6"
}
},
"node_modules/eslint-plugin-promise": { "node_modules/eslint-plugin-promise": {
"version": "6.0.0", "version": "6.0.0",
"resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.0.0.tgz", "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.0.0.tgz",
@ -2919,6 +2931,18 @@
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8" "eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8"
} }
}, },
"node_modules/eslint-plugin-react-hooks": {
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.0.tgz",
"integrity": "sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==",
"dev": true,
"engines": {
"node": ">=10"
},
"peerDependencies": {
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0"
}
},
"node_modules/eslint-plugin-react/node_modules/doctrine": { "node_modules/eslint-plugin-react/node_modules/doctrine": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/doctrine/-/doctrine-2.1.0.tgz", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-2.1.0.tgz",
@ -6708,9 +6732,9 @@
"dev": true "dev": true
}, },
"@testing-library/dom": { "@testing-library/dom": {
"version": "8.16.0", "version": "8.17.1",
"resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-8.16.0.tgz", "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-8.17.1.tgz",
"integrity": "sha512-uxF4zmnLHHDlmW4l+0WDjcgLVwCvH+OVLpD8Dfp+Bjfz85prwxWGbwXgJdLtkgjD0qfOzkJF9SmA6YZPsMYX4w==", "integrity": "sha512-KnH2MnJUzmFNPW6RIKfd+zf2Wue8mEKX0M3cpX6aKl5ZXrJM1/c/Pc8c2xDNYQCnJO48Sm5ITbMXgqTr3h4jxQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@babel/code-frame": "^7.10.4", "@babel/code-frame": "^7.10.4",
@ -8254,6 +8278,12 @@
"semver": "^7.3.7" "semver": "^7.3.7"
} }
}, },
"eslint-plugin-only-warn": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/eslint-plugin-only-warn/-/eslint-plugin-only-warn-1.0.3.tgz",
"integrity": "sha512-XQOX/TfLoLw6h8ky51d29uUjXRTQHqBGXPylDEmy5fe/w7LIOnp8MA24b1OSMEn9BQoKow1q3g1kLe5/9uBTvw==",
"dev": true
},
"eslint-plugin-promise": { "eslint-plugin-promise": {
"version": "6.0.0", "version": "6.0.0",
"resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.0.0.tgz", "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.0.0.tgz",
@ -8311,6 +8341,13 @@
} }
} }
}, },
"eslint-plugin-react-hooks": {
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.0.tgz",
"integrity": "sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==",
"dev": true,
"requires": {}
},
"eslint-scope": { "eslint-scope": {
"version": "5.1.1", "version": "5.1.1",
"resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-5.1.1.tgz", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-5.1.1.tgz",

View file

@ -40,6 +40,7 @@
"eslint-plugin-only-warn": "^1.0.3", "eslint-plugin-only-warn": "^1.0.3",
"eslint-plugin-promise": "^6.0.0", "eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.30.1", "eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"jsdom": "^20.0.0", "jsdom": "^20.0.0",
"postcss": "^8.4.14", "postcss": "^8.4.14",
"sass": "^1.54.0", "sass": "^1.54.0",

11
pnpm-lock.yaml generated
View file

@ -22,6 +22,7 @@ specifiers:
eslint-plugin-only-warn: ^1.0.3 eslint-plugin-only-warn: ^1.0.3
eslint-plugin-promise: ^6.0.0 eslint-plugin-promise: ^6.0.0
eslint-plugin-react: ^7.30.1 eslint-plugin-react: ^7.30.1
eslint-plugin-react-hooks: ^4.6.0
framer-motion: ^6.5.1 framer-motion: ^6.5.1
jsdom: ^20.0.0 jsdom: ^20.0.0
postcss: ^8.4.14 postcss: ^8.4.14
@ -62,6 +63,7 @@ devDependencies:
eslint-plugin-only-warn: 1.0.3 eslint-plugin-only-warn: 1.0.3
eslint-plugin-promise: 6.0.0_eslint@8.21.0 eslint-plugin-promise: 6.0.0_eslint@8.21.0
eslint-plugin-react: 7.30.1_eslint@8.21.0 eslint-plugin-react: 7.30.1_eslint@8.21.0
eslint-plugin-react-hooks: 4.6.0_eslint@8.21.0
jsdom: 20.0.0 jsdom: 20.0.0
postcss: 8.4.16 postcss: 8.4.16
sass: 1.54.3 sass: 1.54.3
@ -1740,6 +1742,15 @@ packages:
eslint: 8.21.0 eslint: 8.21.0
dev: true dev: true
/eslint-plugin-react-hooks/4.6.0_eslint@8.21.0:
resolution: {integrity: sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==}
engines: {node: '>=10'}
peerDependencies:
eslint: ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0
dependencies:
eslint: 8.21.0
dev: true
/eslint-plugin-react/7.30.1_eslint@8.21.0: /eslint-plugin-react/7.30.1_eslint@8.21.0:
resolution: {integrity: sha512-NbEvI9jtqO46yJA3wcRF9Mo0lF9T/jhdHqhCHXiXtD+Zcb98812wvokjWpU7Q4QH5edo6dmqrukxVvWWXHlsUg==} resolution: {integrity: sha512-NbEvI9jtqO46yJA3wcRF9Mo0lF9T/jhdHqhCHXiXtD+Zcb98812wvokjWpU7Q4QH5edo6dmqrukxVvWWXHlsUg==}
engines: {node: '>=4'} engines: {node: '>=4'}

View file

@ -5,6 +5,7 @@ import { ContainerModel, IContainerModel } from '../../Interfaces/ContainerModel
import { findContainerById } from '../../utils/itertools'; import { findContainerById } from '../../utils/itertools';
import { getCurrentHistory } from './Editor'; import { getCurrentHistory } from './Editor';
import { SizePointer } from '../../Interfaces/SizePointer'; import { SizePointer } from '../../Interfaces/SizePointer';
import Properties from '../../Interfaces/Properties';
/** /**
* Select a container * Select a container
@ -274,7 +275,7 @@ export function OnPropertyChange(
*/ */
export function OnPropertiesSubmit( export function OnPropertiesSubmit(
event: React.SyntheticEvent<HTMLFormElement>, event: React.SyntheticEvent<HTMLFormElement>,
refs: Array<React.RefObject<HTMLInputElement>>, properties: Properties,
fullHistory: HistoryState[], fullHistory: HistoryState[],
historyCurrentStep: number, historyCurrentStep: number,
setHistory: Dispatch<SetStateAction<HistoryState[]>>, setHistory: Dispatch<SetStateAction<HistoryState[]>>,
@ -291,10 +292,10 @@ export function OnPropertiesSubmit(
if (parent === null) { if (parent === null) {
const selectedContainerClone: IContainerModel = structuredClone(current.SelectedContainer); const selectedContainerClone: IContainerModel = structuredClone(current.SelectedContainer);
for (const ref of refs) { for (const property in properties) {
const input = ref.current; const input = (event.target as HTMLFormElement).querySelector(`#${property}`);
if (input instanceof HTMLInputElement) { if (input instanceof HTMLInputElement) {
(selectedContainerClone.properties as any)[input.id] = input.value; (selectedContainerClone.properties as any)[property] = input.value;
} }
} }
setHistory(history.concat([{ setHistory(history.concat([{
@ -315,10 +316,10 @@ export function OnPropertiesSubmit(
throw new Error('[OnPropertyChange] Container model was not found among children of the main container!'); throw new Error('[OnPropertyChange] Container model was not found among children of the main container!');
} }
for (const ref of refs) { for (const property in properties) {
const input = ref.current; const input = (event.target as HTMLFormElement).querySelector(`#${property}`);
if (input instanceof HTMLInputElement) { if (input instanceof HTMLInputElement) {
(container.properties as any)[input.id] = input.value; (container.properties as any)[property] = input.value;
} }
} }

View file

@ -72,9 +72,9 @@ const Editor: React.FunctionComponent<IEditorProps> = (props) => {
setHistory, setHistory,
setHistoryCurrentStep setHistoryCurrentStep
)} )}
OnPropertiesSubmit={(event, refs) => OnPropertiesSubmit( OnPropertiesSubmit={(event, properties) => OnPropertiesSubmit(
event, event,
refs, properties,
history, history,
historyCurrentStep, historyCurrentStep,
setHistory, setHistory,

View file

@ -1,6 +1,7 @@
import * as React from 'react'; import * as React from 'react';
import { motion } from 'framer-motion'; import { motion } from 'framer-motion';
import { Properties } from '../Properties/Properties'; import { Properties } from '../Properties/Properties';
import ContainerProperties from '../../Interfaces/Properties';
import { IContainerModel } from '../../Interfaces/ContainerModel'; import { IContainerModel } from '../../Interfaces/ContainerModel';
import { getDepth, MakeIterator } from '../../utils/itertools'; import { getDepth, MakeIterator } from '../../utils/itertools';
import { Menu } from '../Menu/Menu'; import { Menu } from '../Menu/Menu';
@ -8,13 +9,14 @@ import { MenuItem } from '../Menu/MenuItem';
import { handleDragLeave, handleDragOver, handleLeftClick, handleOnDrop, handleRightClick } from './MouseEventHandlers'; import { handleDragLeave, handleDragOver, handleLeftClick, handleOnDrop, handleRightClick } from './MouseEventHandlers';
import { Point } from '../../Interfaces/Point'; import { Point } from '../../Interfaces/Point';
interface IElementsSidebarProps { interface IElementsSidebarProps {
MainContainer: IContainerModel MainContainer: IContainerModel
isOpen: boolean isOpen: boolean
isHistoryOpen: boolean isHistoryOpen: boolean
SelectedContainer: IContainerModel | null SelectedContainer: IContainerModel | null
OnPropertyChange: (key: string, value: string | number | boolean) => void OnPropertyChange: (key: string, value: string | number | boolean) => void
OnPropertiesSubmit: (event: React.FormEvent<HTMLFormElement>, refs: Array<React.RefObject<HTMLInputElement>>) => void OnPropertiesSubmit: (event: React.FormEvent<HTMLFormElement>, properties: ContainerProperties) => void
SelectContainer: (container: IContainerModel) => void SelectContainer: (container: IContainerModel) => void
DeleteContainer: (containerid: string) => void DeleteContainer: (containerid: string) => void
AddContainer: (index: number, type: string, parent: string) => void AddContainer: (index: number, type: string, parent: string) => void

View file

@ -6,7 +6,7 @@ import { INPUT_TYPES } from './PropertiesInputTypes';
interface IPropertiesProps { interface IPropertiesProps {
properties?: ContainerProperties properties?: ContainerProperties
onChange: (key: string, value: string | number | boolean) => void onChange: (key: string, value: string | number | boolean) => void
onSubmit: (event: React.FormEvent<HTMLFormElement>, refs: Array<React.RefObject<HTMLInputElement>>) => void onSubmit: (event: React.FormEvent<HTMLFormElement>, properties: ContainerProperties) => void
} }
export const Properties: React.FC<IPropertiesProps> = (props: IPropertiesProps) => { export const Properties: React.FC<IPropertiesProps> = (props: IPropertiesProps) => {
@ -17,10 +17,9 @@ export const Properties: React.FC<IPropertiesProps> = (props: IPropertiesProps)
} }
const groupInput: React.ReactNode[] = []; const groupInput: React.ReactNode[] = [];
const refs: Array<React.RefObject<HTMLInputElement>> = [];
Object Object
.entries(props.properties) .entries(props.properties)
.forEach((pair) => handleProperties(pair, groupInput, refs, isDynamicInput, props.onChange)); .forEach((pair) => handleProperties(pair, groupInput, isDynamicInput, props.onChange));
const form = isDynamicInput const form = isDynamicInput
? <div> ? <div>
@ -28,7 +27,7 @@ export const Properties: React.FC<IPropertiesProps> = (props: IPropertiesProps)
</div> </div>
: <form : <form
key={props.properties.id} key={props.properties.id}
onSubmit={(event) => props.onSubmit(event, refs)} onSubmit={(event) => props.onSubmit(event, props.properties as ContainerProperties)}
> >
<input type='submit' className='normal-btn block mx-auto' value='Submit'/> <input type='submit' className='normal-btn block mx-auto' value='Submit'/>
{ groupInput } { groupInput }
@ -52,7 +51,6 @@ export const Properties: React.FC<IPropertiesProps> = (props: IPropertiesProps)
const handleProperties = ( const handleProperties = (
[key, value]: [string, string | number], [key, value]: [string, string | number],
groupInput: React.ReactNode[], groupInput: React.ReactNode[],
refs: Array<React.RefObject<HTMLInputElement>>,
isDynamicInput: boolean, isDynamicInput: boolean,
onChange: (key: string, value: string | number | boolean) => void onChange: (key: string, value: string | number | boolean) => void
): void => { ): void => {
@ -69,9 +67,6 @@ const handleProperties = (
type = INPUT_TYPES[key]; type = INPUT_TYPES[key];
} }
const ref: React.RefObject<HTMLInputElement> = React.useRef<HTMLInputElement>(null);
refs.push(ref);
const isDisabled = ['id', 'parentId'].includes(key); const isDisabled = ['id', 'parentId'].includes(key);
const input = isDynamicInput const input = isDynamicInput
? <input ? <input
@ -82,7 +77,6 @@ const handleProperties = (
' '
type={type} type={type}
id={key} id={key}
ref={ref}
value={value} value={value}
checked={checked} checked={checked}
onChange={(event) => { onChange={(event) => {
@ -102,7 +96,6 @@ const handleProperties = (
' '
type={type} type={type}
id={key} id={key}
ref={ref}
defaultValue={value} defaultValue={value}
defaultChecked={checked} defaultChecked={checked}
disabled={isDisabled} disabled={isDisabled}

View file

@ -8,6 +8,7 @@ import { HistoryState } from '../../Interfaces/HistoryState';
import { PhotographIcon, UploadIcon } from '@heroicons/react/outline'; import { PhotographIcon, UploadIcon } from '@heroicons/react/outline';
import { FloatingButton } from '../FloatingButton/FloatingButton'; import { FloatingButton } from '../FloatingButton/FloatingButton';
import { Bar } from '../Bar/Bar'; import { Bar } from '../Bar/Bar';
import Properties from '../../Interfaces/Properties';
interface IUIProps { interface IUIProps {
current: HistoryState current: HistoryState
@ -17,7 +18,7 @@ interface IUIProps {
SelectContainer: (container: ContainerModel) => void SelectContainer: (container: ContainerModel) => void
DeleteContainer: (containerId: string) => void DeleteContainer: (containerId: string) => void
OnPropertyChange: (key: string, value: string | number | boolean) => void OnPropertyChange: (key: string, value: string | number | boolean) => void
OnPropertiesSubmit: (event: React.FormEvent<HTMLFormElement>, refs: Array<React.RefObject<HTMLInputElement>>) => void OnPropertiesSubmit: (event: React.FormEvent<HTMLFormElement>, properties: Properties) => void
AddContainerToSelectedContainer: (type: string) => void AddContainerToSelectedContainer: (type: string) => void
AddContainer: (index: number, type: string, parentId: string) => void AddContainer: (index: number, type: string, parentId: string) => void
SaveEditorAsJSON: () => void SaveEditorAsJSON: () => void