|
| 1 | +import * as React from 'react'; |
| 2 | +import * as S from 'widgets/ClusterConfigForm/ClusterConfigForm.styled'; |
| 3 | +import { Button } from 'components/common/Button/Button'; |
| 4 | +import Input from 'components/common/Input/Input'; |
| 5 | +import { useFieldArray, useFormContext } from 'react-hook-form'; |
| 6 | +import PlusIcon from 'components/common/Icons/PlusIcon'; |
| 7 | +import IconButtonWrapper from 'components/common/Icons/IconButtonWrapper'; |
| 8 | +import CloseCircleIcon from 'components/common/Icons/CloseCircleIcon'; |
| 9 | +import { |
| 10 | + FlexGrow1, |
| 11 | + FlexRow, |
| 12 | +} from 'widgets/ClusterConfigForm/ClusterConfigForm.styled'; |
| 13 | +import SectionHeader from 'widgets/ClusterConfigForm/common/SectionHeader'; |
| 14 | + |
| 15 | +import PropertiesFields from './PropertiesFields'; |
| 16 | + |
| 17 | +const Serdes = () => { |
| 18 | + const { control } = useFormContext(); |
| 19 | + const { fields, append, remove } = useFieldArray({ |
| 20 | + control, |
| 21 | + name: 'serde', |
| 22 | + }); |
| 23 | + |
| 24 | + const handleAppend = () => |
| 25 | + append({ |
| 26 | + name: '', |
| 27 | + className: '', |
| 28 | + filePath: '', |
| 29 | + topicKeysPattern: '%s-key', |
| 30 | + topicValuesPattern: '%s-value', |
| 31 | + }); |
| 32 | + const toggleConfig = () => (fields.length === 0 ? handleAppend() : remove()); |
| 33 | + |
| 34 | + const hasFields = fields.length > 0; |
| 35 | + |
| 36 | + return ( |
| 37 | + <> |
| 38 | + <SectionHeader |
| 39 | + title="Serdes" |
| 40 | + addButtonText="Configure Serdes" |
| 41 | + adding={!hasFields} |
| 42 | + onClick={toggleConfig} |
| 43 | + /> |
| 44 | + {hasFields && ( |
| 45 | + <S.GroupFieldWrapper> |
| 46 | + {fields.map((item, index) => ( |
| 47 | + <div key={item.id}> |
| 48 | + <FlexRow> |
| 49 | + <FlexGrow1> |
| 50 | + <Input |
| 51 | + label="Name *" |
| 52 | + name={`serde.${index}.name`} |
| 53 | + placeholder="Name" |
| 54 | + type="text" |
| 55 | + hint="Serde name" |
| 56 | + withError |
| 57 | + /> |
| 58 | + <Input |
| 59 | + label="Class Name *" |
| 60 | + name={`serde.${index}.className`} |
| 61 | + placeholder="className" |
| 62 | + type="text" |
| 63 | + hint="Serde class name" |
| 64 | + withError |
| 65 | + /> |
| 66 | + <Input |
| 67 | + label="File Path *" |
| 68 | + name={`serde.${index}.filePath`} |
| 69 | + placeholder="serde file path" |
| 70 | + type="text" |
| 71 | + hint="Serde file path" |
| 72 | + withError |
| 73 | + /> |
| 74 | + <Input |
| 75 | + label="Topic Keys Pattern *" |
| 76 | + name={`serde.${index}.topicKeysPattern`} |
| 77 | + placeholder="topicKeysPattern" |
| 78 | + type="text" |
| 79 | + hint="Serde topic keys pattern" |
| 80 | + withError |
| 81 | + /> |
| 82 | + <Input |
| 83 | + label="Topic Values Pattern *" |
| 84 | + name={`serde.${index}.topicValuesPattern`} |
| 85 | + placeholder="topicValuesPattern" |
| 86 | + type="text" |
| 87 | + hint="Serde topic values pattern" |
| 88 | + withError |
| 89 | + /> |
| 90 | + <hr /> |
| 91 | + <PropertiesFields nestedId={index} /> |
| 92 | + </FlexGrow1> |
| 93 | + <S.RemoveButton onClick={() => remove(index)}> |
| 94 | + <IconButtonWrapper aria-label="deleteProperty"> |
| 95 | + <CloseCircleIcon aria-hidden /> |
| 96 | + </IconButtonWrapper> |
| 97 | + </S.RemoveButton> |
| 98 | + </FlexRow> |
| 99 | + |
| 100 | + <hr /> |
| 101 | + </div> |
| 102 | + ))} |
| 103 | + <Button |
| 104 | + type="button" |
| 105 | + buttonSize="M" |
| 106 | + buttonType="secondary" |
| 107 | + onClick={handleAppend} |
| 108 | + > |
| 109 | + <PlusIcon /> |
| 110 | + Add Serde |
| 111 | + </Button> |
| 112 | + </S.GroupFieldWrapper> |
| 113 | + )} |
| 114 | + </> |
| 115 | + ); |
| 116 | +}; |
| 117 | +export default Serdes; |
0 commit comments