From 1fe6ec611c0cb55636b446270b74645326241c66 Mon Sep 17 00:00:00 2001 From: Azat Gataullin Date: Thu, 16 Apr 2020 17:23:05 +0300 Subject: [PATCH] redesign-topics-adding-custom-params-design --- .../New/CustomParams/CustomParamAction.tsx | 22 ++--- .../New/CustomParams/CustomParamButton.tsx | 15 +++- .../New/CustomParams/CustomParamSelect.tsx | 32 +++---- .../New/CustomParams/CustomParamValue.tsx | 9 +- .../Topics/New/CustomParams/CustomParams.tsx | 90 +++++++++---------- 5 files changed, 76 insertions(+), 92 deletions(-) diff --git a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamAction.tsx b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamAction.tsx index f634e2a65f..c97c3f194d 100644 --- a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamAction.tsx +++ b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamAction.tsx @@ -1,26 +1,20 @@ import React from 'react'; import CustomParamButton, { CustomParamButtonType } from './CustomParamButton'; -import { isFirstParam } from './CustomParams'; interface Props { index: string; - onAdd: (event: React.MouseEvent) => void; onRemove: (index: string) => void; } -const CustomParamAction: React.FC = ({ - index, - onAdd, - onRemove, -}) => ( +const CustomParamAction: React.FC = ({ index, onRemove }) => ( <> - - { - isFirstParam(index) - ? - : onRemove(index)} /> - } + + onRemove(index)} + /> -) +); export default CustomParamAction; diff --git a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamButton.tsx b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamButton.tsx index c9fe3d8cd0..ea5c731df5 100644 --- a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamButton.tsx +++ b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamButton.tsx @@ -6,21 +6,28 @@ export enum CustomParamButtonType { } interface Props { - onClick: (event: React.MouseEvent) => void, + onClick: (event: React.MouseEvent) => void; className: string; type: CustomParamButtonType; + btnText?: string; } const CustomParamButton: React.FC = ({ onClick, className, type, + btnText, }) => ( - -) +); export default CustomParamButton; diff --git a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamSelect.tsx b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamSelect.tsx index 70177be727..2d751d200e 100644 --- a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamSelect.tsx +++ b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamSelect.tsx @@ -1,8 +1,8 @@ import React from 'react'; import { useFormContext, ErrorMessage } from 'react-hook-form'; -import CustomParamOptions from './CustomParamOptions'; -import { isFirstParam, INDEX_PREFIX } from './CustomParams'; import { TopicFormCustomParam } from 'redux/interfaces'; +import CustomParamOptions from './CustomParamOptions'; +import { INDEX_PREFIX } from './CustomParams'; interface Props { isDisabled: boolean; @@ -10,19 +10,10 @@ interface Props { name: string; } -const CustomParamSelect: React.FC = ({ - isDisabled, - index, - name, -}) => { - - const { register, unregister, errors, getValues, triggerValidation } = useFormContext(); +const CustomParamSelect: React.FC = ({ isDisabled, index, name }) => { + const { register, errors, getValues, triggerValidation } = useFormContext(); const optInputName = `${index}[name]`; - React.useEffect( - () => { if (isFirstParam(index)) { unregister(optInputName) } }, - ); - const selectedMustBeUniq = (selected: string) => { const values = getValues({ nest: true }); const customParamsValues: TopicFormCustomParam = values.customParams; @@ -30,11 +21,12 @@ const CustomParamSelect: React.FC = ({ let valid = true; for (const [key, customParam] of Object.entries(customParamsValues)) { - if (`${INDEX_PREFIX}.${key}` === index) { continue; } - if (selected === customParam.name) { - valid = false; - break; - }; + if (`${INDEX_PREFIX}.${key}` !== index) { + if (selected === customParam.name) { + valid = false; + break; + } + } } return valid ? true : 'Custom Parameter must be unique'; @@ -48,7 +40,7 @@ const CustomParamSelect: React.FC = ({ name={optInputName} ref={register({ required: 'Custom Parameter is required.', - validate: { unique: selected => selectedMustBeUniq(selected) }, + validate: { unique: (selected) => selectedMustBeUniq(selected) }, })} onChange={() => triggerValidation(optInputName)} disabled={isDisabled} @@ -57,7 +49,7 @@ const CustomParamSelect: React.FC = ({

- +

diff --git a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamValue.tsx b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamValue.tsx index 7445708842..244fb7d95a 100644 --- a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamValue.tsx +++ b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParamValue.tsx @@ -1,7 +1,6 @@ import React from 'react'; import { useFormContext, ErrorMessage } from 'react-hook-form'; import { CUSTOM_PARAMS_OPTIONS } from './customParamsOptions'; -import { isFirstParam } from './CustomParams'; interface Props { isDisabled: boolean; @@ -16,7 +15,7 @@ const CustomParamValue: React.FC = ({ name, defaultValue, }) => { - const { register, unregister, errors, watch, setValue } = useFormContext(); + const { register, errors, watch, setValue } = useFormContext(); const selectInputName = `${index}[name]`; const valInputName = `${index}[value]`; const selectedParamName = watch(selectInputName, name); @@ -31,12 +30,6 @@ const CustomParamValue: React.FC = ({ } }, [selectedParamName]); - React.useEffect(() => { - if (isFirstParam(index)) { - unregister(valInputName); - } - }); - return ( <> diff --git a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParams.tsx b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParams.tsx index 8fe22db036..5dc2840ced 100644 --- a/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParams.tsx +++ b/kafka-ui-react-app/src/components/Topics/New/CustomParams/CustomParams.tsx @@ -5,22 +5,20 @@ import { TopicFormCustomParams } from 'redux/interfaces'; import CustomParamSelect from './CustomParamSelect'; import CustomParamValue from './CustomParamValue'; import CustomParamAction from './CustomParamAction'; +import CustomParamButton, { CustomParamButtonType } from './CustomParamButton'; -const DEFAULT_INDEX = 'default'; export const INDEX_PREFIX = 'customParams'; -export const isFirstParam = (index: string) => (index === DEFAULT_INDEX); interface Props { isSubmitting: boolean; } -const CustomParams: React.FC = ({ - isSubmitting, -}) => { - - const [formCustomParams, setFormCustomParams] = React.useState({ - byIndex: { [DEFAULT_INDEX]: { name: '', value: '' } }, - allIndexes: [DEFAULT_INDEX], +const CustomParams: React.FC = ({ isSubmitting }) => { + const [formCustomParams, setFormCustomParams] = React.useState< + TopicFormCustomParams + >({ + byIndex: {}, + allIndexes: [], }); const onAdd = (event: React.MouseEvent) => { @@ -34,54 +32,54 @@ const CustomParams: React.FC = ({ ...formCustomParams.byIndex, [newIndex]: { name: '', value: '' }, }, - allIndexes: [ - formCustomParams.allIndexes[0], - newIndex, - ...formCustomParams.allIndexes.slice(1), - ], + allIndexes: [newIndex, ...formCustomParams.allIndexes], }); - } + }; const onRemove = (index: string) => { setFormCustomParams({ ...formCustomParams, byIndex: omit(formCustomParams.byIndex, index), - allIndexes: reject(formCustomParams.allIndexes, (i) => (i === index)), + allIndexes: reject(formCustomParams.allIndexes, (i) => i === index), }); - } + }; return ( <> - { - formCustomParams.allIndexes.map((index) => ( -
-
- -
- -
- -
- -
- -
+
+
+ +
+
+ {formCustomParams.allIndexes.map((index) => ( +
+
+
- )) - } + +
+ +
+ +
+ +
+
+ ))} ); };