forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reset field default value on type change in Settings (twent…
…yhq#5534) Related issue: twentyhq#5412 See twentyhq#5436 (comment) for context.
- Loading branch information
1 parent
18fafbd
commit 7f7ea59
Showing
23 changed files
with
326 additions
and
131 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts
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
57 changes: 57 additions & 0 deletions
57
...a-model/fields/forms/boolean/components/SettingsDataModelFieldBooleanSettingsFormCard.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useFormContext } from 'react-hook-form'; | ||
import styled from '@emotion/styled'; | ||
|
||
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem'; | ||
import { SettingsDataModelPreviewFormCard } from '@/settings/data-model/components/SettingsDataModelPreviewFormCard'; | ||
import { | ||
SettingsDataModelFieldBooleanForm, | ||
SettingsDataModelFieldBooleanFormValues, | ||
} from '@/settings/data-model/fields/forms/boolean/components/SettingsDataModelFieldBooleanForm'; | ||
import { useBooleanSettingsFormInitialValues } from '@/settings/data-model/fields/forms/boolean/hooks/useBooleanSettingsFormInitialValues'; | ||
import { | ||
SettingsDataModelFieldPreviewCard, | ||
SettingsDataModelFieldPreviewCardProps, | ||
} from '@/settings/data-model/fields/preview/components/SettingsDataModelFieldPreviewCard'; | ||
|
||
type SettingsDataModelFieldBooleanSettingsFormCardProps = { | ||
fieldMetadataItem: Pick< | ||
FieldMetadataItem, | ||
'icon' | 'label' | 'type' | 'defaultValue' | ||
>; | ||
} & Pick<SettingsDataModelFieldPreviewCardProps, 'objectMetadataItem'>; | ||
|
||
const StyledFieldPreviewCard = styled(SettingsDataModelFieldPreviewCard)` | ||
display: grid; | ||
flex: 1 1 100%; | ||
`; | ||
|
||
export const SettingsDataModelFieldBooleanSettingsFormCard = ({ | ||
fieldMetadataItem, | ||
objectMetadataItem, | ||
}: SettingsDataModelFieldBooleanSettingsFormCardProps) => { | ||
const { initialDefaultValue } = useBooleanSettingsFormInitialValues({ | ||
fieldMetadataItem, | ||
}); | ||
|
||
const { watch: watchFormValue } = | ||
useFormContext<SettingsDataModelFieldBooleanFormValues>(); | ||
|
||
return ( | ||
<SettingsDataModelPreviewFormCard | ||
preview={ | ||
<StyledFieldPreviewCard | ||
fieldMetadataItem={{ | ||
...fieldMetadataItem, | ||
defaultValue: watchFormValue('defaultValue', initialDefaultValue), | ||
}} | ||
objectMetadataItem={objectMetadataItem} | ||
/> | ||
} | ||
form={ | ||
<SettingsDataModelFieldBooleanForm | ||
fieldMetadataItem={fieldMetadataItem} | ||
/> | ||
} | ||
/> | ||
); | ||
}; |
26 changes: 26 additions & 0 deletions
26
...les/settings/data-model/fields/forms/boolean/hooks/useBooleanSettingsFormInitialValues.ts
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,26 @@ | ||
import { useFormContext } from 'react-hook-form'; | ||
|
||
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem'; | ||
import { SettingsDataModelFieldBooleanFormValues } from '@/settings/data-model/fields/forms/boolean/components/SettingsDataModelFieldBooleanForm'; | ||
|
||
export const useBooleanSettingsFormInitialValues = ({ | ||
fieldMetadataItem, | ||
}: { | ||
fieldMetadataItem?: Pick<FieldMetadataItem, 'defaultValue'>; | ||
}) => { | ||
const initialDefaultValue = | ||
(fieldMetadataItem?.defaultValue as SettingsDataModelFieldBooleanFormValues['defaultValue']) ?? | ||
true; | ||
|
||
const { resetField } = | ||
useFormContext<SettingsDataModelFieldBooleanFormValues>(); | ||
|
||
const resetDefaultValueField = () => { | ||
resetField('defaultValue', { defaultValue: initialDefaultValue }); | ||
}; | ||
|
||
return { | ||
initialDefaultValue, | ||
resetDefaultValueField, | ||
}; | ||
}; |
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
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
Oops, something went wrong.