import styled from '@emotion/styled'; import { useObjectMetadataItemForSettings } from '@/object-metadata/hooks/useObjectMetadataItemForSettings'; import { validateMetadataLabel } from '@/object-metadata/utils/validateMetadataLabel'; import { IconPicker } from '@/ui/input/components/IconPicker'; import { Select } from '@/ui/input/components/Select'; import { TextInput } from '@/ui/input/components/TextInput'; import { useLazyLoadIcons } from '@/ui/input/hooks/useLazyLoadIcons'; import { Field } from '~/generated-metadata/graphql'; import { relationTypes } from '../constants/relationTypes'; import { RelationType } from '../types/RelationType'; export type SettingsObjectFieldRelationFormValues = Partial<{ field: Partial>; objectMetadataId: string; type: RelationType; }>; type SettingsObjectFieldRelationFormProps = { disableFieldEdition?: boolean; disableRelationEdition?: boolean; onChange: (values: SettingsObjectFieldRelationFormValues) => void; values?: SettingsObjectFieldRelationFormValues; }; const StyledSelectsContainer = styled.div` display: grid; gap: ${({ theme }) => theme.spacing(4)}; grid-template-columns: 1fr 1fr; margin-bottom: ${({ theme }) => theme.spacing(4)}; `; const StyledInputsLabel = styled.span` color: ${({ theme }) => theme.font.color.light}; display: block; font-size: ${({ theme }) => theme.font.size.xs}; font-weight: ${({ theme }) => theme.font.weight.semiBold}; margin-bottom: ${({ theme }) => theme.spacing(1)}; text-transform: uppercase; `; const StyledInputsContainer = styled.div` display: flex; gap: ${({ theme }) => theme.spacing(2)}; width: 100%; `; export const SettingsObjectFieldRelationForm = ({ disableFieldEdition, disableRelationEdition, onChange, values, }: SettingsObjectFieldRelationFormProps) => { const { icons } = useLazyLoadIcons(); const { objectMetadataItems, findObjectMetadataItemById } = useObjectMetadataItemForSettings(); const selectedObjectMetadataItem = (values?.objectMetadataId ? findObjectMetadataItemById(values.objectMetadataId) : undefined) || objectMetadataItems[0]; return (
({ label: objectMetadataItem.labelPlural, value: objectMetadataItem.id, Icon: objectMetadataItem.icon ? icons[objectMetadataItem.icon] : undefined, }))} onChange={(value) => onChange({ objectMetadataId: value })} /> Field on {selectedObjectMetadataItem?.labelPlural} onChange({ field: { ...values?.field, icon: value.iconKey }, }) } variant="primary" /> { if (!value || validateMetadataLabel(value)) { onChange({ field: { ...values?.field, label: value }, }); } }} fullWidth />
); };