-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Regression: Fixed room edit custom field validation (#28078)
- Loading branch information
1 parent
d4b3cb8
commit ee55b3e
Showing
4 changed files
with
138 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable react/no-multi-comp */ | ||
import type { SelectOption } from '@rocket.chat/fuselage'; | ||
import { Field, Select, TextInput } from '@rocket.chat/fuselage'; | ||
import type { TranslationKey } from '@rocket.chat/ui-contexts'; | ||
import { useTranslation } from '@rocket.chat/ui-contexts'; | ||
import React from 'react'; | ||
import type { Control, FieldValues } from 'react-hook-form'; | ||
import { Controller, get } from 'react-hook-form'; | ||
|
||
export type CustomFieldMetadata = { | ||
name: string; | ||
label: string; | ||
type: 'select' | 'text'; | ||
required?: boolean; | ||
defaultValue?: any; | ||
options?: SelectOption[]; | ||
}; | ||
|
||
type CustomFieldFormProps<T extends FieldValues> = { | ||
metadata: CustomFieldMetadata[]; | ||
formControl: Control<T>; | ||
formName: string; | ||
}; | ||
|
||
type CustomFieldProps<T extends FieldValues> = Omit<CustomFieldMetadata, 'name'> & { | ||
control: Control<T>; | ||
name: string; | ||
}; | ||
|
||
const FIELD_TYPES = { | ||
select: Select, | ||
text: TextInput, | ||
} as const; | ||
|
||
export const CustomField = <T extends FieldValues>({ | ||
name, | ||
type, | ||
control, | ||
label, | ||
required, | ||
defaultValue, | ||
options = [], | ||
...props | ||
}: CustomFieldProps<T>) => { | ||
const t = useTranslation(); | ||
const Component = FIELD_TYPES[type] ?? null; | ||
|
||
return ( | ||
<Controller<T, any> | ||
name={name} | ||
control={control} | ||
defaultValue={defaultValue ?? ''} | ||
rules={{ required: t('The_field_is_required', label || name) }} | ||
render={({ field, formState: { errors } }) => ( | ||
<Field> | ||
<Field.Label> | ||
{label || t(name as TranslationKey)} | ||
{required && '*'} | ||
</Field.Label> | ||
<Field.Row> | ||
<Component {...props} {...field} options={options} error={get(errors, name) as string} flexGrow={1} /> | ||
</Field.Row> | ||
<Field.Error>{get(errors, name)?.message}</Field.Error> | ||
</Field> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
CustomField.displayName = 'CustomField'; | ||
|
||
export const CustomFieldsForm = <T extends FieldValues>({ formName, formControl, metadata }: CustomFieldFormProps<T>) => ( | ||
<> | ||
{metadata.map(({ name: fieldName, ...props }) => ( | ||
<CustomField key={fieldName} name={`${formName}.${fieldName}`} control={formControl} {...props} /> | ||
))} | ||
</> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 11 additions & 22 deletions
33
apps/meteor/client/views/omnichannel/directory/utils/formatCustomFieldsMetadata.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,23 @@ | ||
import type { ILivechatCustomField, Serialized } from '@rocket.chat/core-typings'; | ||
|
||
type CustomFieldsMetadata = Record< | ||
string, | ||
{ | ||
label: string; | ||
type: 'select' | 'text'; | ||
required?: boolean; | ||
defaultValue?: unknown; | ||
options?: string[]; | ||
} | ||
>; | ||
import type { CustomFieldMetadata } from '../../../../components/CustomFieldsFormV2'; | ||
|
||
export const formatCustomFieldsMetadata = ( | ||
customFields: Serialized<ILivechatCustomField>[], | ||
scope: 'visitor' | 'room', | ||
): CustomFieldsMetadata => { | ||
): CustomFieldMetadata[] => { | ||
if (!customFields) { | ||
return {}; | ||
return []; | ||
} | ||
|
||
return customFields | ||
.filter((field) => field.visibility === 'visible' && field.scope === scope) | ||
.reduce((obj, { _id, label, options, defaultValue, required }) => { | ||
obj[_id] = { | ||
label, | ||
type: options ? 'select' : 'text', | ||
required, | ||
defaultValue, | ||
options: options?.split(',').map((item) => item.trim()), | ||
}; | ||
return obj; | ||
}, {} as CustomFieldsMetadata); | ||
.map(({ _id, label, options, defaultValue, required }) => ({ | ||
name: _id, | ||
label, | ||
type: options ? 'select' : 'text', | ||
required, | ||
defaultValue, | ||
options: options?.split(',').map((item) => [item.trim(), item.trim()]), | ||
})); | ||
}; |