Skip to content

Commit

Permalink
refactor(console): extract the FormField from KeyValueInput
Browse files Browse the repository at this point in the history
extract the FormField from KeyValueInput
  • Loading branch information
simeng-li committed Mar 6, 2024
1 parent b47bc46 commit 305a220
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
28 changes: 11 additions & 17 deletions packages/console/src/ds-components/KeyValueInputField/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { type AdminConsoleKey } from '@logto/phrases';
import { type ReactElement, useCallback } from 'react';
import { useCallback } from 'react';
import { type FieldError } from 'react-hook-form';

import CirclePlus from '@/assets/icons/circle-plus.svg';
import Minus from '@/assets/icons/minus.svg';
import Button from '@/ds-components/Button';
import type DangerousRaw from '@/ds-components/DangerousRaw';
import FormField from '@/ds-components/FormField';
import IconButton from '@/ds-components/IconButton';
import TextInput, { type Props as TextInputProps } from '@/ds-components/TextInput';

Expand All @@ -27,8 +24,7 @@ type InputFieldPropsGetter = {
};

type Props = {
title: AdminConsoleKey | ReactElement<typeof DangerousRaw>;
tip?: string;
className?: string;
fields: Array<FieldType & { id: string }>; // Id is required to uniquely identify each field
errors?: Array<ErrorType | undefined>;
getInputFieldProps: InputFieldPropsGetter;
Expand All @@ -42,18 +38,16 @@ type Props = {
* This component is used to add multiple key-value pairs.
* For most of the cases, it is designed to be used along with react-hook-form.
* All the input properties are registered with react-hook-form.
*
* @param {string} title - The title of the field.
* @param {string} [tip] - The tip for the field.
* @param {FieldType} fields - The array of key-value pairs. @see {@link https://react-hook-form.com/docs/usefieldarray}
* @param {ErrorType[]} [errors] - The array of errors for each field. Accepts both string and FieldError from RHF.
* @param {Function} onRemove - The function to remove a field. @see {@link https://react-hook-form.com/docs/usefieldarray}
* @param {Function} onAppend - The function to append a new field. @see {@link https://react-hook-form.com/docs/usefieldarray}
* @param {Props} props - The props for the component.
* @param {string} [props.className] - The class name for the container.
* @param {FieldType} props.fields - The array of key-value pairs. @see {@link https://react-hook-form.com/docs/usefieldarray}
* @param {ErrorType[]} [props.errors] - The array of errors for each field. Accepts both string and FieldError from RHF.
* @param {Function} props.onRemove - The function to remove a field. @see {@link https://react-hook-form.com/docs/usefieldarray}
* @param {Function} props.onAppend - The function to append a new field. @see {@link https://react-hook-form.com/docs/usefieldarray}
* @param {InputFieldPropsGetter} getInputFieldProps - The function bundle to get the input field props for each field. e.g. Use React Hook Form's register method to register the input field.
*/
function KeyValueInputField({
title,
tip,
className,
fields,
errors,
getInputFieldProps,
Expand All @@ -78,7 +72,7 @@ function KeyValueInputField({
);

return (
<FormField title={title} tip={tip}>
<div className={className}>
{fields.map((field, index) => {
return (
// Use id as the element key if it exists (generated by react-hook-form useFieldArray method), otherwise use the key
Expand Down Expand Up @@ -120,7 +114,7 @@ function KeyValueInputField({
onAppend({ key: '', value: '' });
}}
/>
</FormField>
</div>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useMemo } from 'react';
import { useFieldArray, useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';

import FormField from '@/ds-components/FormField';
import KeyValueInputField from '@/ds-components/KeyValueInputField';
import { type WebhookDetailsFormType } from '@/pages/WebhookDetails/types';

Expand Down Expand Up @@ -96,16 +97,19 @@ function CustomHeaderField() {
);

return (
<KeyValueInputField
<FormField
title="webhook_details.settings.custom_headers"
tip={t('webhook_details.settings.custom_headers_tip')}
fields={fields}
// Force headerErrors to be an array, otherwise return undefined
errors={headerErrors?.map?.((error) => error)}
getInputFieldProps={getInputFieldProps}
onAppend={append}
onRemove={remove}
/>
>
<KeyValueInputField
fields={fields}
// Force headerErrors to be an array, otherwise return undefined
errors={headerErrors?.map?.((error) => error)}
getInputFieldProps={getInputFieldProps}
onAppend={append}
onRemove={remove}
/>
</FormField>
);
}

Expand Down

0 comments on commit 305a220

Please sign in to comment.