From 962ed35352c67d813091d48cc09ad50369b2b185 Mon Sep 17 00:00:00 2001 From: Chika Date: Wed, 18 May 2022 21:54:29 +0300 Subject: [PATCH] Components & hooks: optimize --- .eslintrc.json | 13 +++- .../Calculation/builders/build-action.tsx | 3 +- .../Calculation/builders/build-computed.tsx | 4 +- .../Calculation/builders/build-options.tsx | 9 +-- .../Calculation/builders/build-readonly.tsx | 4 +- .../Calculation/builders/build-value.tsx | 15 ++--- Elements/Button.tsx | 8 +-- Elements/Checkbox.tsx | 13 ++-- Elements/Input.tsx | 16 ++--- Elements/InputNumber.tsx | 18 ++---- Elements/Link.tsx | 6 +- Elements/Radio.tsx | 14 +++-- Elements/Select.tsx | 27 +++++---- Elements/Switch.tsx | 11 +--- Elements/Text.tsx | 6 +- stores/calculation/options/hooks.js | 12 +--- stores/calculation/statuses/hooks.js | 12 +--- stores/calculation/validation/hooks.js | 10 +--- stores/calculation/values/hooks.js | 60 +++++-------------- 19 files changed, 108 insertions(+), 153 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 3410de2..7d8d609 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -36,6 +36,17 @@ "lines-between-class-members": "off", "@typescript-eslint/lines-between-class-members": ["off"], "indent": "off", - "@typescript-eslint/indent": ["off"] + "@typescript-eslint/indent": ["off"], + "react/jsx-no-bind": [ + "error", + { + "ignoreDOMComponents": false, + "ignoreRefs": false, + "allowArrowFunctions": false, + "allowFunctions": true, + "allowBind": false + } + ], + "newline-before-return": "warn" } } diff --git a/Components/Calculation/builders/build-action.tsx b/Components/Calculation/builders/build-action.tsx index 1460172..9419338 100644 --- a/Components/Calculation/builders/build-action.tsx +++ b/Components/Calculation/builders/build-action.tsx @@ -1,3 +1,4 @@ +/* eslint-disable react/jsx-no-bind */ import { observer } from 'mobx-react-lite'; import { useStatus } from 'stores/calculation/statuses/hooks'; import { getAction } from '../config/map-actions'; @@ -7,7 +8,7 @@ export default function buildAction({ elementName, Component, ...props }: Builde const actionName = getAction(elementName); return observer(() => { - const { status } = useStatus(elementName); + const status = useStatus(elementName); return ( { - const { computedValue } = useComputedValue(computedValueName); - const { status } = useStatus(elementName); + const computedValue = useComputedValue(computedValueName); + const status = useStatus(elementName); return ; }); diff --git a/Components/Calculation/builders/build-options.tsx b/Components/Calculation/builders/build-options.tsx index 2c6a199..7a4e245 100644 --- a/Components/Calculation/builders/build-options.tsx +++ b/Components/Calculation/builders/build-options.tsx @@ -2,7 +2,7 @@ import { observer } from 'mobx-react-lite'; import { useOptions } from 'stores/calculation/options/hooks'; import { useStatus } from 'stores/calculation/statuses/hooks'; import { useValidation } from 'stores/calculation/validation/hooks'; -import { useValue } from 'stores/calculation/values/hooks'; +import { useSetValue, useValue } from 'stores/calculation/values/hooks'; import { getValueName } from '../config/map-values'; import type { BuilderProps } from './types'; @@ -10,10 +10,11 @@ export default function buildOptions({ elementName, Component, ...props }: Build const valueName = getValueName(elementName); return observer(() => { - const { value, setValue } = useValue(valueName); - const { status } = useStatus(elementName); + const value = useValue(valueName); + const setValue = useSetValue(valueName); + const status = useStatus(elementName); const { isValid, help } = useValidation(elementName); - const { options } = useOptions(elementName); + const options = useOptions(elementName); return ( { - const { value } = useValue(valueName); + const value = useValue(valueName); - return ; + return ; }); } diff --git a/Components/Calculation/builders/build-value.tsx b/Components/Calculation/builders/build-value.tsx index 97878c9..5e006e6 100644 --- a/Components/Calculation/builders/build-value.tsx +++ b/Components/Calculation/builders/build-value.tsx @@ -1,7 +1,7 @@ import { observer } from 'mobx-react-lite'; import { useStatus } from 'stores/calculation/statuses/hooks'; import { useValidation } from 'stores/calculation/validation/hooks'; -import { useValue } from 'stores/calculation/values/hooks'; +import { useSetValue } from 'stores/calculation/values/hooks'; import { getValueName } from '../config/map-values'; import type { BuilderProps } from './types'; @@ -9,19 +9,12 @@ export default function buildValue({ elementName, Component, ...props }: Builder const valueName = getValueName(elementName); return observer(() => { - const { value, setValue } = useValue(valueName); - const { status } = useStatus(elementName); + const setValue = useSetValue(valueName); + const status = useStatus(elementName); const { isValid, help } = useValidation(elementName); return ( - + ); }); } diff --git a/Elements/Button.tsx b/Elements/Button.tsx index f944163..4aca367 100644 --- a/Elements/Button.tsx +++ b/Elements/Button.tsx @@ -1,5 +1,5 @@ import type { ButtonProps } from 'antd'; -import { Button } from 'antd'; +import { Button as AntButton } from 'antd'; import { throttle } from 'lodash'; import type { Status } from './types'; @@ -9,19 +9,19 @@ type ElementProps = { text: string; }; -export default function Element({ status, action, text }: ElementProps) { +export default function Button({ status, action, text }: ElementProps) { const throttledAction = throttle(action, 1200, { trailing: false, }); return ( - + ); } diff --git a/Elements/Checkbox.tsx b/Elements/Checkbox.tsx index 6e4c72c..e9026b4 100644 --- a/Elements/Checkbox.tsx +++ b/Elements/Checkbox.tsx @@ -1,10 +1,11 @@ import type { CheckboxProps } from 'antd'; -import { Checkbox, Form } from 'antd'; +import { Checkbox as AntCheckbox, Form } from 'antd'; +import type { CheckboxChangeEvent } from 'antd/lib/checkbox'; import type { BaseElementProps } from './types'; const { Item: FormItem } = Form; -export default function Element({ +export default function Checkbox({ value, setValue, status, @@ -12,11 +13,15 @@ export default function Element({ help, ...props }: BaseElementProps) { + function handleChange(e: CheckboxChangeEvent) { + setValue(e.target.checked); + } + return ( - setValue(e.target.value)} + onChange={handleChange} disabled={status === 'Disabled'} {...props} /> diff --git a/Elements/Input.tsx b/Elements/Input.tsx index 256efec..470f84a 100644 --- a/Elements/Input.tsx +++ b/Elements/Input.tsx @@ -1,10 +1,11 @@ import type { InputProps } from 'antd'; -import { Form, Input } from 'antd'; +import { Form, Input as AntInput } from 'antd'; +import type { ChangeEvent } from 'react'; import type { BaseElementProps } from './types'; const { Item: FormItem } = Form; -export default function Element({ +export default function Input({ value, setValue, status, @@ -12,14 +13,13 @@ export default function Element({ help, ...props }: BaseElementProps) { + function handleChange(e: ChangeEvent) { + setValue(e.target.value); + } + return ( - setValue(e.target.value)} - disabled={status === 'Disabled'} - {...props} - /> + ); } diff --git a/Elements/InputNumber.tsx b/Elements/InputNumber.tsx index 39b7ce5..a0c4169 100644 --- a/Elements/InputNumber.tsx +++ b/Elements/InputNumber.tsx @@ -1,17 +1,10 @@ import type { InputNumberProps } from 'antd'; -import { Form, InputNumber } from 'antd'; +import { Form, InputNumber as AntInputNumber } from 'antd'; import type { BaseElementProps } from './types'; const { Item: FormItem } = Form; -const parser = (val?: string): number => { - const res = val?.replace(/[^0-9.,]+/, ''); - if (!res || res === '') return 0; - return parseFloat(res); -}; - -export default function Element({ - value = 0, +export default function InputNumber({ setValue, status, isValid, @@ -20,11 +13,10 @@ export default function Element({ }: BaseElementProps) { return ( - setValue(val)} + diff --git a/Elements/Link.tsx b/Elements/Link.tsx index 4525c9b..b44abf7 100644 --- a/Elements/Link.tsx +++ b/Elements/Link.tsx @@ -1,11 +1,11 @@ import DownloadOutlined from '@ant-design/icons/lib/icons/DownloadOutlined'; import type { ButtonProps } from 'antd'; -import { Button } from 'antd'; +import { Button as AntButton } from 'antd'; import type { BaseElementProps } from './types'; -export default function Element({ value, setValue, status, ...props }: BaseElementProps) { +export default function Link({ value, setValue, status, ...props }: BaseElementProps) { return ( -