Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactive form preview #8663

Merged
merged 11 commits into from
Dec 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ export const NumberFieldDisplay = () => {
const { fieldValue, fieldDefinition } = useNumberFieldDisplay();
const decimals = fieldDefinition.metadata.settings?.decimals;
const type = fieldDefinition.metadata.settings?.type;
const value =
type === 'percentage' && fieldValue
? `${formatNumber(Number(fieldValue) * 100, decimals)}%`
: fieldValue
? formatNumber(Number(fieldValue), decimals)
: null;

if (!fieldValue) return <NumberDisplay value={null} />;
guillim marked this conversation as resolved.
Show resolved Hide resolved

const numericValue = Number(fieldValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider handling invalid number conversion (NaN) cases here

if (Number.isNaN(numericValue)) return <NumberDisplay value={null} />;

const value = type === 'percentage'
? `${formatNumber(numericValue * 100, decimals)}%`
: formatNumber(numericValue, decimals);

return <NumberDisplay value={value} decimals={decimals} />;
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import styled from '@emotion/styled';
import { useFormContext } from 'react-hook-form';

import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { SettingsDataModelPreviewFormCard } from '@/settings/data-model/components/SettingsDataModelPreviewFormCard';
import { SettingsDataModelFieldNumberForm } from '@/settings/data-model/fields/forms/number/components/SettingsDataModelFieldNumberForm';
import {
SettingsDataModelFieldNumberForm,
SettingsDataModelFieldNumberFormValues,
} from '@/settings/data-model/fields/forms/number/components/SettingsDataModelFieldNumberForm';
import {
SettingsDataModelFieldPreviewCard,
SettingsDataModelFieldPreviewCardProps,
Expand All @@ -12,7 +16,7 @@ type SettingsDataModelFieldNumberSettingsFormCardProps = {
disabled?: boolean;
fieldMetadataItem: Pick<
FieldMetadataItem,
'icon' | 'label' | 'type' | 'defaultValue'
'icon' | 'label' | 'type' | 'defaultValue' | 'settings'
>;
} & Pick<SettingsDataModelFieldPreviewCardProps, 'objectMetadataItem'>;

Expand All @@ -26,11 +30,18 @@ export const SettingsDataModelFieldNumberSettingsFormCard = ({
fieldMetadataItem,
objectMetadataItem,
}: SettingsDataModelFieldNumberSettingsFormCardProps) => {
const { watch } = useFormContext<SettingsDataModelFieldNumberFormValues>();

const settings = watch('settings');

return (
<SettingsDataModelPreviewFormCard
preview={
<StyledFieldPreviewCard
fieldMetadataItem={fieldMetadataItem}
fieldMetadataItem={{
...fieldMetadataItem,
settings: settings ?? fieldMetadataItem.settings ?? { decimals: 0 },
}}
objectMetadataItem={objectMetadataItem}
/>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { isFieldValueEmpty } from '@/object-record/record-field/utils/isFieldValueEmpty';
import { generateDefaultFieldValue } from '@/object-record/utils/generateDefaultFieldValue';
import { SettingsFieldType } from '@/settings/data-model/types/SettingsFieldType';
import { getSettingsFieldTypeConfig } from '@/settings/data-model/utils/getSettingsFieldTypeConfig';
import { isFieldTypeSupportedInSettings } from '@/settings/data-model/utils/isFieldTypeSupportedInSettings';
import { isDefined } from '~/utils/isDefined';
import { FieldMetadataType } from '~/generated-metadata/graphql';

const DEFAULT_NUMBER_PREVIEW_VALUE = 2000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you define something that is specific to Number.
However, this preview helper is agnositc (neutral) of its children.

Consequence is that if you look at an adress preview, it is now broken. Maybe you can find a way to make it ok for all kind of fieldMetadataItem types ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, will look into this


export const getFieldPreviewValue = ({
fieldMetadataItem,
}: {
fieldMetadataItem: Pick<FieldMetadataItem, 'type' | 'defaultValue'>;
fieldMetadataItem: Pick<
FieldMetadataItem,
'type' | 'defaultValue'
>;

This comment was marked as spam.

}) => {
if (!isFieldTypeSupportedInSettings(fieldMetadataItem.type)) return null;

if (
!isFieldValueEmpty({
fieldDefinition: { type: fieldMetadataItem.type },
fieldValue: fieldMetadataItem.defaultValue,
})
) {
return generateDefaultFieldValue(fieldMetadataItem);
if (fieldMetadataItem.defaultValue !== undefined && fieldMetadataItem.defaultValue !== null) {
return fieldMetadataItem.defaultValue;
}

const fieldTypeConfig = getSettingsFieldTypeConfig(fieldMetadataItem.type);

if (
isDefined(fieldTypeConfig) &&
'exampleValue' in fieldTypeConfig &&
isDefined(fieldTypeConfig.exampleValue)
) {
return fieldTypeConfig.exampleValue;
const fieldTypeConfig = getSettingsFieldTypeConfig(fieldMetadataItem.type as SettingsFieldType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Type assertion to SettingsFieldType may be unsafe if fieldMetadataItem.type is not a valid SettingsFieldType


if (fieldMetadataItem.type === FieldMetadataType.Number) {
return fieldTypeConfig.exampleValue || DEFAULT_NUMBER_PREVIEW_VALUE;
}

return null;
return fieldTypeConfig.exampleValue ?? null;
};
Loading