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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ 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} />;

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


const value = type === 'percentage'
? `${formatNumber(numericValue, decimals)}%`
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Percentage values should be multiplied by 100 before display. The old code correctly did Number(fieldValue) * 100, but this was removed. This will cause percentages to display incorrectly (e.g. 0.5 will show as '0.5%' instead of '50%').

: 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,
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: The fallback to fieldMetadataItem.settings could be undefined if both settings are undefined, potentially causing runtime issues

}}
objectMetadataItem={objectMetadataItem}
/>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
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';

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

Choose a reason for hiding this comment

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

logic: 'settings' property is picked but never used in the function

}) => {
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 || 2000;
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Magic number 2000 should be defined as a constant with meaningful name

}

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