-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
base: main
Are you sure you want to change the base?
Reactive form preview #8663
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
const value = type === 'percentage' | ||
? `${formatNumber(numericValue, decimals)}%` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
: 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, | ||
|
@@ -12,7 +16,7 @@ type SettingsDataModelFieldNumberSettingsFormCardProps = { | |
disabled?: boolean; | ||
fieldMetadataItem: Pick< | ||
FieldMetadataItem, | ||
'icon' | 'label' | 'type' | 'defaultValue' | ||
'icon' | 'label' | 'type' | 'defaultValue' | 'settings' | ||
>; | ||
} & Pick<SettingsDataModelFieldPreviewCardProps, 'objectMetadataItem'>; | ||
|
||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
/> | ||
} | ||
|
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' | ||
>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
There was a problem hiding this comment.
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