-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add address composite form field #9022
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
94 changes: 94 additions & 0 deletions
94
...nt/src/modules/object-record/record-field/form-types/components/FormAddressFieldInput.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,94 @@ | ||
import { FormCountrySelectInput } from '@/object-record/record-field/form-types/components/FormCountrySelectInput'; | ||
import { FormTextFieldInput } from '@/object-record/record-field/form-types/components/FormTextFieldInput'; | ||
import { StyledFormCompositeFieldInputContainer } from '@/object-record/record-field/form-types/components/StyledFormCompositeFieldInputContainer'; | ||
import { StyledFormFieldInputContainer } from '@/object-record/record-field/form-types/components/StyledFormFieldInputContainer'; | ||
import { VariablePickerComponent } from '@/object-record/record-field/form-types/types/VariablePickerComponent'; | ||
import { FieldAddressDraftValue } from '@/object-record/record-field/types/FieldInputDraftValue'; | ||
import { FieldAddressValue } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { InputLabel } from '@/ui/input/components/InputLabel'; | ||
|
||
type FormAddressFieldInputProps = { | ||
label?: string; | ||
defaultValue: FieldAddressDraftValue | null; | ||
onPersist: (value: FieldAddressValue) => void; | ||
VariablePicker?: VariablePickerComponent; | ||
readonly?: boolean; | ||
}; | ||
|
||
export const FormAddressFieldInput = ({ | ||
label, | ||
defaultValue, | ||
onPersist, | ||
readonly, | ||
VariablePicker, | ||
}: FormAddressFieldInputProps) => { | ||
const handleChange = | ||
(field: keyof FieldAddressDraftValue) => (updatedAddressPart: string) => { | ||
const updatedAddress = { | ||
addressStreet1: defaultValue?.addressStreet1 ?? '', | ||
addressStreet2: defaultValue?.addressStreet2 ?? '', | ||
addressCity: defaultValue?.addressCity ?? '', | ||
addressState: defaultValue?.addressState ?? '', | ||
addressPostcode: defaultValue?.addressPostcode ?? '', | ||
addressCountry: defaultValue?.addressCountry ?? '', | ||
addressLat: defaultValue?.addressLat ?? null, | ||
addressLng: defaultValue?.addressLng ?? null, | ||
[field]: updatedAddressPart, | ||
}; | ||
onPersist(updatedAddress); | ||
}; | ||
|
||
return ( | ||
<StyledFormFieldInputContainer> | ||
{label ? <InputLabel>{label}</InputLabel> : null} | ||
<StyledFormCompositeFieldInputContainer> | ||
<FormTextFieldInput | ||
label="Address 1" | ||
defaultValue={defaultValue?.addressStreet1 ?? ''} | ||
onPersist={handleChange('addressStreet1')} | ||
readonly={readonly} | ||
VariablePicker={VariablePicker} | ||
placeholder="Street address" | ||
/> | ||
<FormTextFieldInput | ||
label="Address 2" | ||
defaultValue={defaultValue?.addressStreet2 ?? ''} | ||
onPersist={handleChange('addressStreet2')} | ||
readonly={readonly} | ||
VariablePicker={VariablePicker} | ||
placeholder="Street address 2" | ||
/> | ||
<FormTextFieldInput | ||
label="City" | ||
defaultValue={defaultValue?.addressCity ?? ''} | ||
onPersist={handleChange('addressCity')} | ||
readonly={readonly} | ||
VariablePicker={VariablePicker} | ||
placeholder="City" | ||
/> | ||
<FormTextFieldInput | ||
label="State" | ||
defaultValue={defaultValue?.addressState ?? ''} | ||
onPersist={handleChange('addressState')} | ||
readonly={readonly} | ||
VariablePicker={VariablePicker} | ||
placeholder="State" | ||
/> | ||
<FormTextFieldInput | ||
label="Post Code" | ||
defaultValue={defaultValue?.addressPostcode ?? ''} | ||
onPersist={handleChange('addressPostcode')} | ||
readonly={readonly} | ||
VariablePicker={VariablePicker} | ||
placeholder="Post Code" | ||
/> | ||
<FormCountrySelectInput | ||
selectedCountryName={defaultValue?.addressCountry ?? ''} | ||
onPersist={handleChange('addressCountry')} | ||
readonly={readonly} | ||
VariablePicker={VariablePicker} | ||
/> | ||
</StyledFormCompositeFieldInputContainer> | ||
</StyledFormFieldInputContainer> | ||
); | ||
}; |
63 changes: 63 additions & 0 deletions
63
...t/src/modules/object-record/record-field/form-types/components/FormCountrySelectInput.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,63 @@ | ||
import { useMemo } from 'react'; | ||
import { IconCircleOff, IconComponentProps } from 'twenty-ui'; | ||
|
||
import { FormSelectFieldInput } from '@/object-record/record-field/form-types/components/FormSelectFieldInput'; | ||
import { VariablePickerComponent } from '@/object-record/record-field/form-types/types/VariablePickerComponent'; | ||
import { SelectOption } from '@/spreadsheet-import/types'; | ||
import { useCountries } from '@/ui/input/components/internal/hooks/useCountries'; | ||
|
||
export const FormCountrySelectInput = ({ | ||
selectedCountryName, | ||
onPersist, | ||
readonly = false, | ||
VariablePicker, | ||
}: { | ||
selectedCountryName: string; | ||
onPersist: (countryCode: string) => void; | ||
readonly?: boolean; | ||
VariablePicker?: VariablePickerComponent; | ||
}) => { | ||
const countries = useCountries(); | ||
|
||
const options: SelectOption[] = useMemo(() => { | ||
const countryList = countries.map<SelectOption>( | ||
({ countryName, Flag }) => ({ | ||
label: countryName, | ||
value: countryName, | ||
thomtrp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
color: 'transparent', | ||
icon: (props: IconComponentProps) => | ||
Flag({ width: props.size, height: props.size }), | ||
}), | ||
); | ||
return [ | ||
{ | ||
label: 'No country', | ||
value: '', | ||
icon: IconCircleOff, | ||
}, | ||
...countryList, | ||
]; | ||
}, [countries]); | ||
|
||
const onChange = (countryCode: string | null) => { | ||
if (readonly) { | ||
return; | ||
} | ||
|
||
if (countryCode === null) { | ||
onPersist(''); | ||
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. I don't know much about addresses, but are we sure we want to store an empty string vs. 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. yep default values have to be string |
||
} else { | ||
onPersist(countryCode); | ||
} | ||
}; | ||
thomtrp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ( | ||
<FormSelectFieldInput | ||
label="Country" | ||
onPersist={onChange} | ||
options={options} | ||
defaultValue={selectedCountryName} | ||
VariablePicker={VariablePicker} | ||
/> | ||
); | ||
}; |
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
37 changes: 37 additions & 0 deletions
37
...t-record/record-field/form-types/components/__stories__/FormAddressFieldInput.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,37 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { within } from '@storybook/test'; | ||
import { FormAddressFieldInput } from '../FormAddressFieldInput'; | ||
|
||
const meta: Meta<typeof FormAddressFieldInput> = { | ||
title: 'UI/Data/Field/Form/Input/FormAddressFieldInput', | ||
component: FormAddressFieldInput, | ||
args: {}, | ||
argTypes: {}, | ||
thomtrp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof FormAddressFieldInput>; | ||
|
||
export const Default: Story = { | ||
thomtrp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
args: { | ||
label: 'Address', | ||
defaultValue: { | ||
addressStreet1: '123 Main St', | ||
addressStreet2: 'Apt 123', | ||
addressCity: 'Springfield', | ||
addressState: 'IL', | ||
addressCountry: 'US', | ||
addressPostcode: '12345', | ||
thomtrp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
addressLat: 39.781721, | ||
addressLng: -89.650148, | ||
}, | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
|
||
await canvas.findByText('123 Main St'); | ||
await canvas.findByText('Address'); | ||
await canvas.findByText('Post Code'); | ||
}, | ||
thomtrp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; |
26 changes: 26 additions & 0 deletions
26
...-record/record-field/form-types/components/__stories__/FormCountrySelectInput.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,26 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { within } from '@storybook/test'; | ||
import { FormCountrySelectInput } from '../FormCountrySelectInput'; | ||
|
||
const meta: Meta<typeof FormCountrySelectInput> = { | ||
title: 'UI/Data/Field/Form/Input/FormCountrySelectInput', | ||
component: FormCountrySelectInput, | ||
args: {}, | ||
argTypes: {}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof FormCountrySelectInput>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
selectedCountryName: 'Canada', | ||
}, | ||
thomtrp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
|
||
await canvas.findByText('Country'); | ||
await canvas.findByText('Canada'); | ||
}, | ||
thomtrp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Dissociating the field definition from the input is excellent. I'm unsure if it will "scale" well as the other fields make extensive use of the
useNumberField
-like functions and get access to the field definition that way.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.
let's see. If it becomes too complex we will still be able to come back to full field!