Skip to content

Commit

Permalink
Expandable list remove anchor (twentyhq#5559)
Browse files Browse the repository at this point in the history
Deprecate anchorElement on ExpandableList to avoid props drilling. The
anchorElement should be the ExpandableList container itself
  • Loading branch information
charlesBochet committed May 24, 2024
1 parent 7f7ea59 commit 82ec30c
Show file tree
Hide file tree
Showing 10 changed files with 7 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ export const CalendarEventParticipantsResponseStatusField = ({
</StyledLabelAndIconContainer>
<StyledDiv ref={participantsContainerRef}>
{isRightDrawerAnimationCompleted && (
<ExpandableList
anchorElement={participantsContainerRef.current || undefined}
isChipCountDisplayed
>
{styledChips}
</ExpandableList>
<ExpandableList isChipCountDisplayed>{styledChips}</ExpandableList>
)}
</StyledDiv>
</StyledInlineCellBaseContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ExpandableList } from '@/ui/layout/expandable-list/components/Expandabl

type ActivityTargetChipsProps = {
activityTargetObjectRecords: ActivityTargetWithTargetRecord[];
anchorElement?: HTMLElement;
maxWidth?: number;
};

Expand All @@ -19,12 +18,11 @@ const StyledContainer = styled.div<{ maxWidth?: number }>`

export const ActivityTargetChips = ({
activityTargetObjectRecords,
anchorElement,
maxWidth,
}: ActivityTargetChipsProps) => {
return (
<StyledContainer maxWidth={maxWidth}>
<ExpandableList anchorElement={anchorElement} isChipCountDisplayed>
<ExpandableList isChipCountDisplayed>
{activityTargetObjectRecords.map(
(activityTargetObjectRecord, index) => (
<RecordChip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ export const ActivityTargetsInlineCell = ({
/>
}
label="Relations"
displayModeContent={({ cellElement }) => (
displayModeContent={() => (
<ActivityTargetChips
anchorElement={cellElement}
activityTargetObjectRecords={activityTargetObjectRecords}
maxWidth={maxWidth}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export const FieldDisplay = ({
) : isFieldLinks(fieldDefinition) ? (
<LinksFieldDisplay
isCellSoftFocused={isCellSoftFocused}
cellElement={cellElement}
fromTableCell={fromTableCell}
/>
) : isFieldCurrency(fieldDefinition) ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ import { LinksDisplay } from '@/ui/field/display/components/LinksDisplay';

type LinksFieldDisplayProps = {
isCellSoftFocused?: boolean;
cellElement?: HTMLElement;
fromTableCell?: boolean;
};

export const LinksFieldDisplay = ({
isCellSoftFocused,
cellElement,
fromTableCell,
}: LinksFieldDisplayProps) => {
const { fieldValue } = useLinksField();

return (
<LinksDisplay
value={fieldValue}
anchorElement={cellElement}
isChipCountDisplayed={isCellSoftFocused}
withExpandedListBorder={fromTableCell}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type MultiSelectFieldDisplayProps = {

export const MultiSelectFieldDisplay = ({
isCellSoftFocused,
cellElement,
fromTableCell,
}: MultiSelectFieldDisplayProps) => {
const { fieldValues, fieldDefinition } = useMultiSelectField();
Expand All @@ -26,7 +25,6 @@ export const MultiSelectFieldDisplay = ({

return (
<ExpandableList
anchorElement={cellElement}
isChipCountDisplayed={isCellSoftFocused}
withExpandedListBorder={fromTableCell}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import { getUrlHostName } from '~/utils/url/getUrlHostName';

type LinksDisplayProps = Pick<
ExpandableListProps,
'anchorElement' | 'isChipCountDisplayed' | 'withExpandedListBorder'
'isChipCountDisplayed' | 'withExpandedListBorder'
> & {
value?: FieldLinksValue;
};

export const LinksDisplay = ({
anchorElement,
isChipCountDisplayed,
withExpandedListBorder,
value,
Expand Down Expand Up @@ -55,7 +54,6 @@ export const LinksDisplay = ({

return (
<ExpandableList
anchorElement={anchorElement}
isChipCountDisplayed={isChipCountDisplayed}
withExpandedListBorder={withExpandedListBorder}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const StyledChipCount = styled(Chip)`
`;

export type ExpandableListProps = {
anchorElement?: HTMLElement;
isChipCountDisplayed?: boolean;
withExpandedListBorder?: boolean;
};
Expand All @@ -53,7 +52,6 @@ export type ChildrenProperty = {

export const ExpandableList = ({
children,
anchorElement,
isChipCountDisplayed: isChipCountDisplayedFromProps,
withExpandedListBorder = false,
}: {
Expand All @@ -75,10 +73,10 @@ export const ExpandableList = ({
// @see https://floating-ui.com/docs/useFloating#elements
const [childrenContainerElement, setChildrenContainerElement] =
useState<HTMLDivElement | null>(null);

const [previousChildrenContainerWidth, setPreviousChildrenContainerWidth] =
useState(childrenContainerElement?.clientWidth ?? 0);

// Used with useListenClickOutside.
const containerRef = useRef<HTMLDivElement>(null);

const [firstHiddenChildIndex, setFirstHiddenChildIndex] = useState(
Expand Down Expand Up @@ -165,7 +163,7 @@ export const ExpandableList = ({
)}
{isListExpanded && (
<ExpandedListDropdown
anchorElement={anchorElement ?? childrenContainerElement ?? undefined}
anchorElement={containerRef.current ?? undefined}
onClickOutside={() => {
resetFirstHiddenChildIndex();
setIsListExpanded(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const ExpandedListDropdown = ({
const { refs, floatingStyles } = useFloating({
// @ts-expect-error placement accepts 'start' as value even if the typing does not permit it
placement: 'start',
middleware: [offset({ mainAxis: -1, crossAxis: -1 })],
middleware: [offset({ mainAxis: -9, crossAxis: -7 })],
elements: { reference: anchorElement },
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const meta: Meta<typeof ExpandableList> = {
},
argTypes: {
children: { control: false },
anchorElement: { control: false },
},
};

Expand Down

0 comments on commit 82ec30c

Please sign in to comment.