-
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
Display and update fields from fromManyObjects relations in Show card #5801
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c2bf742
Extract MultiRecordSelect from MultipleObjectRecordSelect
ijreilly 393723b
Extract useRelationEntities from SingleEntitySelectMenuItems
ijreilly aafc80e
Implement RelationManyFieldInput
ijreilly d667907
improve code quality
ijreilly 6bd5034
Merge branch 'main' of github.com:twentyhq/twenty into fix-relations-…
ijreilly 7350e40
Use unique scope Id and reorganize code
ijreilly 505ff39
Improve storybook coverage
ijreilly 6d5937f
Use more lenient averageThreshold
ijreilly 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
Next
Next commit
Extract MultiRecordSelect from MultipleObjectRecordSelect
- Loading branch information
commit c2bf742f311eae15ddde71921b83b38ad15704a4
There are no files selected for viewing
163 changes: 163 additions & 0 deletions
163
...s/twenty-front/src/modules/object-record/relation-picker/components/MultiRecordSelect.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,163 @@ | ||
import { useEffect, useMemo, useRef, useState } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { isNonEmptyString } from '@sniptt/guards'; | ||
import { useDebouncedCallback } from 'use-debounce'; | ||
|
||
import { MultipleObjectRecordOnClickOutsideEffect } from '@/object-record/relation-picker/components/MultipleObjectRecordOnClickOutsideEffect'; | ||
import { MultipleObjectRecordSelectItem } from '@/object-record/relation-picker/components/MultipleObjectRecordSelectItem'; | ||
import { MULTI_OBJECT_RECORD_SELECT_SELECTABLE_LIST_ID } from '@/object-record/relation-picker/constants/MultiObjectRecordSelectSelectableListId'; | ||
import { ObjectRecordForSelect } from '@/object-record/relation-picker/hooks/useMultiObjectSearch'; | ||
import { RelationPickerHotkeyScope } from '@/object-record/relation-picker/types/RelationPickerHotkeyScope'; | ||
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu'; | ||
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer'; | ||
import { DropdownMenuSearchInput } from '@/ui/layout/dropdown/components/DropdownMenuSearchInput'; | ||
import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator'; | ||
import { SelectableItem } from '@/ui/layout/selectable-list/components/SelectableItem'; | ||
import { SelectableList } from '@/ui/layout/selectable-list/components/SelectableList'; | ||
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
export const StyledSelectableItem = styled(SelectableItem)` | ||
height: 100%; | ||
width: 100%; | ||
`; | ||
export const MultiRecordSelect = ({ | ||
onChange, | ||
onSubmit, | ||
selectedObjectRecords, | ||
allRecords, | ||
loading, | ||
searchFilter, | ||
setSearchFilter, | ||
}: { | ||
onChange?: ( | ||
changedRecordForSelect: ObjectRecordForSelect, | ||
newSelectedValue: boolean, | ||
) => void; | ||
onSubmit?: (objectRecordsForSelect: ObjectRecordForSelect[]) => void; | ||
selectedObjectRecords: ObjectRecordForSelect[]; | ||
allRecords: ObjectRecordForSelect[]; | ||
loading: boolean; | ||
searchFilter: string; | ||
setSearchFilter: (searchFilter: string) => void; | ||
}) => { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
const [internalSelectedRecords, setInternalSelectedRecords] = useState< | ||
ObjectRecordForSelect[] | ||
>([]); | ||
|
||
useEffect(() => { | ||
if (!loading) { | ||
setInternalSelectedRecords(selectedObjectRecords); | ||
} | ||
}, [selectedObjectRecords, loading]); | ||
|
||
const debouncedSetSearchFilter = useDebouncedCallback(setSearchFilter, 100, { | ||
leading: true, | ||
}); | ||
|
||
const handleFilterChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
debouncedSetSearchFilter(event.currentTarget.value); | ||
}; | ||
|
||
const handleSelectChange = ( | ||
changedRecordForSelect: ObjectRecordForSelect, | ||
newSelectedValue: boolean, | ||
) => { | ||
const newSelectedRecords = newSelectedValue | ||
? [...internalSelectedRecords, changedRecordForSelect] | ||
: internalSelectedRecords.filter( | ||
(selectedRecord) => | ||
selectedRecord.record.id !== changedRecordForSelect.record.id, | ||
); | ||
|
||
setInternalSelectedRecords(newSelectedRecords); | ||
|
||
onChange?.(changedRecordForSelect, newSelectedValue); | ||
}; | ||
|
||
const entitiesInDropdown = useMemo( | ||
() => | ||
[...(allRecords ?? [])].filter((entity) => | ||
isNonEmptyString(entity.recordIdentifier.id), | ||
), | ||
[allRecords], | ||
); | ||
|
||
const selectableItemIds = entitiesInDropdown.map( | ||
(entity) => entity.record.id, | ||
); | ||
|
||
return ( | ||
<> | ||
<MultipleObjectRecordOnClickOutsideEffect | ||
containerRef={containerRef} | ||
onClickOutside={() => { | ||
onSubmit?.(internalSelectedRecords); | ||
}} | ||
/> | ||
<DropdownMenu ref={containerRef} data-select-disable> | ||
<DropdownMenuSearchInput | ||
value={searchFilter} | ||
onChange={handleFilterChange} | ||
autoFocus | ||
/> | ||
<DropdownMenuSeparator /> | ||
<DropdownMenuItemsContainer hasMaxHeight> | ||
{loading ? ( | ||
<MenuItem text="Loading..." /> | ||
) : ( | ||
<> | ||
<SelectableList | ||
selectableListId={MULTI_OBJECT_RECORD_SELECT_SELECTABLE_LIST_ID} | ||
selectableItemIdArray={selectableItemIds} | ||
hotkeyScope={RelationPickerHotkeyScope.RelationPicker} | ||
onEnter={(recordId) => { | ||
const recordIsSelected = internalSelectedRecords?.some( | ||
(selectedRecord) => selectedRecord.record.id === recordId, | ||
); | ||
|
||
const correspondingRecordForSelect = entitiesInDropdown?.find( | ||
(entity) => entity.record.id === recordId, | ||
); | ||
|
||
if (isDefined(correspondingRecordForSelect)) { | ||
handleSelectChange( | ||
correspondingRecordForSelect, | ||
!recordIsSelected, | ||
); | ||
} | ||
}} | ||
> | ||
{entitiesInDropdown?.map((objectRecordForSelect) => ( | ||
<MultipleObjectRecordSelectItem | ||
key={objectRecordForSelect.record.id} | ||
objectRecordForSelect={objectRecordForSelect} | ||
onSelectedChange={(newSelectedValue) => | ||
handleSelectChange( | ||
objectRecordForSelect, | ||
newSelectedValue, | ||
) | ||
} | ||
selected={internalSelectedRecords?.some( | ||
(selectedRecord) => { | ||
return ( | ||
selectedRecord.record.id === | ||
objectRecordForSelect.record.id | ||
); | ||
}, | ||
)} | ||
/> | ||
))} | ||
</SelectableList> | ||
{entitiesInDropdown?.length === 0 && ( | ||
<MenuItem text="No result" /> | ||
)} | ||
</> | ||
)} | ||
</DropdownMenuItemsContainer> | ||
</DropdownMenu> | ||
</> | ||
); | ||
}; |
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
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.
this logic was mostly extracted from MultipleObjectRecordSelect to be shared with RelationManyFieldInput