-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Country/Region field (#5132)
* feat: add country field backend (#3770) * feat: add country field backend * fix: Fix Property 'country' is missing * fix: replace BasicField.Dropdown in CountryField story * fix: import CountryField into FieldFactory * fix: fix type errors due to lack of handling for new country field Co-authored-by: Kar Rui <[email protected]> * feat: add CountryRegion field to frontend (#4112) * feat: add country field backend (#3770) * feat: add country field backend * fix: Fix Property 'country' is missing * fix: replace BasicField.Dropdown in CountryField story * fix: import CountryField into FieldFactory * fix: fix type errors due to lack of handling for new country field Co-authored-by: Kar Rui <[email protected]> * feat: add Country field to frontend * feat: move Singapore up to the first value in the Country enum * test: add tests for CountryField * fix: update the typed characters in the partial typing test case * test: adjust test case for partial typing in Country field * refactor: rename Country enum to CountryRegion * refactor: rename BasicField.Country to BasicField.CountryRegion * refactor: rename CountryField to CountryRegionField * fix: commit renaming changes for tests/ * refactor: rename *Country* to *CountryRegion* in frontend edit field folder * feat: sort CountryRegion enums inside CountryRegionField * fix: remove removed isSaveEnabled prop * fix: inline sort country options constant * test: update CountryRegionField tests * fix(CountryRegionField): correctly set singapore as the first option also update tests to use the options that the field uses * style: use flag icon and fix capitalisation for CountryRegion * feat: allow validation rule overriding for DropdownField * fix: update error message for invalid Country/Region * test: fix country/region capitalisation in test Co-authored-by: Kar Rui <[email protected]> * fix: remove duplicate country files * fix: remove duplicate country field files and restore TODO * chore: rename test file * fix: add missing imports * fix: use new CreatePageDrawerContentContainer component * fix: dropdown now uses requiredSingleAnswerValidationFn for validation * fix: add validation and processing of CountryRegion fields on client * refactor: implement specific CRField instead of using DropdownField * fix: remove unused field schema regression * feat: make CountryRegion values uppercase when sent to the server * Revert "feat: make CountryRegion values uppercase when sent to the server" This reverts commit be8a9d3. * fix: use title-case country/region only in render * Revert "fix: use title-case country/region only in render" This reverts commit 589af79. * feat: transform Country/Region form inputs to upper-case on submit * fix: validate against upper-case country/region, add comments on why this is done, and remove reference to any * fix: remove reference to toString() * fix: simulate transformations from handleSubmitForm in country-region-validation.spec.ts * fix: resolve rebase and use === instead of == * fix: remove duplicate import * chore: fix prettier violations * fix: update generate-form-data import path * fix: follow DropdownField's way of querying input element and casting options * fix: update INVALID_COUNTRY_REGION_OPTION_ERROR * fix: linting * fix: use uppercased countryregion for non-fetch submission --------- Co-authored-by: Kar Rui <[email protected]> Co-authored-by: Jiayee Lim <[email protected]> Co-authored-by: Jiayee <[email protected]> Co-authored-by: tshuli <[email protected]>
- Loading branch information
1 parent
cfa0544
commit 6cda531
Showing
39 changed files
with
943 additions
and
320 deletions.
There are no files selected for viewing
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
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
51 changes: 51 additions & 0 deletions
51
...signDrawer/EditFieldDrawer/edit-fieldtype/EditCountryRegion/EditCountryRegion.stories.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,51 @@ | ||
import { Meta, Story } from '@storybook/react' | ||
|
||
import { CountryRegion } from '~shared/constants/countryRegion' | ||
import { BasicField, CountryRegionFieldBase } from '~shared/types' | ||
|
||
import { createFormBuilderMocks } from '~/mocks/msw/handlers/admin-form' | ||
|
||
import { EditFieldDrawerDecorator, StoryRouter } from '~utils/storybook' | ||
|
||
import { EditCountryRegion } from './EditCountryRegion' | ||
|
||
const DEFAULT_COUNTRY_REGION_FIELD: CountryRegionFieldBase = { | ||
title: 'Storybook Country/Region', | ||
description: 'Some description about Country/Region', | ||
required: true, | ||
disabled: false, | ||
fieldType: BasicField.CountryRegion, | ||
fieldOptions: Object.values(CountryRegion), | ||
globalId: 'unused', | ||
} | ||
|
||
export default { | ||
title: 'Features/AdminForm/EditFieldDrawer/EditCountryRegion', | ||
component: EditCountryRegion, | ||
decorators: [ | ||
StoryRouter({ | ||
initialEntries: ['/61540ece3d4a6e50ac0cc6ff'], | ||
path: '/:formId', | ||
}), | ||
EditFieldDrawerDecorator, | ||
], | ||
parameters: { | ||
// Required so skeleton "animation" does not hide content. | ||
chromatic: { pauseAnimationAtEnd: true }, | ||
msw: createFormBuilderMocks({}, 0), | ||
}, | ||
args: { | ||
field: DEFAULT_COUNTRY_REGION_FIELD, | ||
}, | ||
} as Meta<StoryArgs> | ||
|
||
interface StoryArgs { | ||
field: CountryRegionFieldBase | ||
} | ||
|
||
const Template: Story<StoryArgs> = ({ field }) => { | ||
return <EditCountryRegion field={field} /> | ||
} | ||
|
||
export const Default = Template.bind({}) | ||
Default.storyName = 'EditCountryRegion' |
92 changes: 92 additions & 0 deletions
92
...derAndDesignDrawer/EditFieldDrawer/edit-fieldtype/EditCountryRegion/EditCountryRegion.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,92 @@ | ||
import { useMemo } from 'react' | ||
import { FormControl } from '@chakra-ui/react' | ||
import { extend, pick } from 'lodash' | ||
|
||
import { CountryRegionFieldBase } from '~shared/types/field' | ||
|
||
import { createBaseValidationRules } from '~utils/fieldValidation' | ||
import FormErrorMessage from '~components/FormControl/FormErrorMessage' | ||
import FormLabel from '~components/FormControl/FormLabel' | ||
import Input from '~components/Input' | ||
import Textarea from '~components/Textarea' | ||
import Toggle from '~components/Toggle' | ||
|
||
import { CreatePageDrawerContentContainer } from '~features/admin-form/create/common' | ||
|
||
import { FormFieldDrawerActions } from '../common/FormFieldDrawerActions' | ||
import { EditFieldProps } from '../common/types' | ||
import { useEditFieldForm } from '../common/useEditFieldForm' | ||
|
||
type EditCountryRegionProps = EditFieldProps<CountryRegionFieldBase> | ||
|
||
const EDIT_COUNTRY_FIELD_KEYS = ['title', 'description', 'required'] as const | ||
|
||
type EditCountryRegionKeys = typeof EDIT_COUNTRY_FIELD_KEYS[number] | ||
|
||
type EditCountryRegionInputs = Pick< | ||
CountryRegionFieldBase, | ||
EditCountryRegionKeys | ||
> | ||
|
||
const transformCountryRegionFieldToEditForm = ( | ||
field: CountryRegionFieldBase, | ||
): EditCountryRegionInputs => { | ||
return { | ||
...pick(field, EDIT_COUNTRY_FIELD_KEYS), | ||
} | ||
} | ||
|
||
const transformCountryRegionEditFormToField = ( | ||
inputs: EditCountryRegionInputs, | ||
originalField: CountryRegionFieldBase, | ||
): CountryRegionFieldBase => { | ||
return extend({}, originalField, inputs, {}) | ||
} | ||
|
||
export const EditCountryRegion = ({ | ||
field, | ||
}: EditCountryRegionProps): JSX.Element => { | ||
const { | ||
register, | ||
formState: { errors }, | ||
buttonText, | ||
handleUpdateField, | ||
isLoading, | ||
handleCancel, | ||
} = useEditFieldForm<EditCountryRegionInputs, CountryRegionFieldBase>({ | ||
field, | ||
transform: { | ||
input: transformCountryRegionFieldToEditForm, | ||
output: transformCountryRegionEditFormToField, | ||
}, | ||
}) | ||
|
||
const requiredValidationRule = useMemo( | ||
() => createBaseValidationRules({ required: true }), | ||
[], | ||
) | ||
|
||
return ( | ||
<CreatePageDrawerContentContainer> | ||
<FormControl isRequired isReadOnly={isLoading} isInvalid={!!errors.title}> | ||
<FormLabel>Question</FormLabel> | ||
<Input autoFocus {...register('title', requiredValidationRule)} /> | ||
<FormErrorMessage>{errors?.title?.message}</FormErrorMessage> | ||
</FormControl> | ||
<FormControl isReadOnly={isLoading} isInvalid={!!errors.description}> | ||
<FormLabel>Description</FormLabel> | ||
<Textarea {...register('description')} /> | ||
<FormErrorMessage>{errors?.description?.message}</FormErrorMessage> | ||
</FormControl> | ||
<FormControl isReadOnly={isLoading}> | ||
<Toggle {...register('required')} label="Required" /> | ||
</FormControl> | ||
<FormFieldDrawerActions | ||
isLoading={isLoading} | ||
buttonText={buttonText} | ||
handleClick={handleUpdateField} | ||
handleCancel={handleCancel} | ||
/> | ||
</CreatePageDrawerContentContainer> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
...d-design/BuilderAndDesignDrawer/EditFieldDrawer/edit-fieldtype/EditCountryRegion/index.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 @@ | ||
export { EditCountryRegion } from './EditCountryRegion' |
1 change: 1 addition & 0 deletions
1
.../create/builder-and-design/BuilderAndDesignDrawer/EditFieldDrawer/edit-fieldtype/index.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
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
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.