Skip to content

Commit

Permalink
Merge pull request #40234 from software-mansion-labs/fix/focus-sync-s…
Browse files Browse the repository at this point in the history
…election-list

Add shouldSyncFocus prop to ListItem components
  • Loading branch information
roryabraham authored Apr 18, 2024
2 parents 447c7c9 + 9db05c7 commit a573219
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/components/SelectionList/BaseListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function BaseListItem<TItem extends ListItem>({
FooterComponent,
children,
isFocused,
shouldSyncFocus = true,
onFocus = () => {},
hoverStyle,
}: BaseListItemProps<TItem>) {
Expand All @@ -38,7 +39,7 @@ function BaseListItem<TItem extends ListItem>({
const pressableRef = useRef<View | HTMLDivElement>(null);

// Sync focus on an item
useSyncFocus(pressableRef, Boolean(isFocused));
useSyncFocus(pressableRef, Boolean(isFocused && shouldSyncFocus));

const rightHandSideComponentRender = () => {
if (canSelectMultiple || !rightHandSideComponent) {
Expand Down
6 changes: 5 additions & 1 deletion src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function BaseSelectionList<TItem extends ListItem>(
const [itemsToHighlight, setItemsToHighlight] = useState<Set<string> | null>(null);
const itemFocusTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const [currentPage, setCurrentPage] = useState(1);
const isTextInputFocusedRef = useRef<boolean>(false);

const incrementPage = () => setCurrentPage((prev) => prev + 1);

Expand Down Expand Up @@ -354,7 +355,8 @@ function BaseSelectionList<TItem extends ListItem>(
rightHandSideComponent={rightHandSideComponent}
keyForList={item.keyForList ?? ''}
isMultilineSupported={isRowMultilineSupported}
onFocus={() => setFocusedIndex(index)}
onFocus={() => setFocusedIndex(normalizedIndex)}
shouldSyncFocus={!isTextInputFocusedRef.current}
/>
);
};
Expand Down Expand Up @@ -527,6 +529,8 @@ function BaseSelectionList<TItem extends ListItem>(
textInputRef.current = element as RNTextInput;
}
}}
onFocus={() => (isTextInputFocusedRef.current = true)}
onBlur={() => (isTextInputFocusedRef.current = false)}
label={textInputLabel}
accessibilityLabel={textInputLabel}
hint={textInputHint}
Expand Down
4 changes: 4 additions & 0 deletions src/components/SelectionList/InviteMemberListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function InviteMemberListItem<TItem extends ListItem>({
onDismissError,
shouldPreventDefaultFocusOnSelectRow,
rightHandSideComponent,
onFocus,
shouldSyncFocus,
}: InviteMemberListItemProps<TItem>) {
const styles = useThemeStyles();
const theme = useTheme();
Expand Down Expand Up @@ -66,6 +68,8 @@ function InviteMemberListItem<TItem extends ListItem>({
) : undefined
}
keyForList={item.keyForList}
onFocus={onFocus}
shouldSyncFocus={shouldSyncFocus}
>
{(hovered?: boolean) => (
<>
Expand Down
2 changes: 2 additions & 0 deletions src/components/SelectionList/RadioListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function RadioListItem<TItem extends ListItem>({
rightHandSideComponent,
isMultilineSupported = false,
onFocus,
shouldSyncFocus,
}: RadioListItemProps<TItem>) {
const styles = useThemeStyles();
const fullTitle = isMultilineSupported ? item.text?.trimStart() : item.text;
Expand All @@ -36,6 +37,7 @@ function RadioListItem<TItem extends ListItem>({
rightHandSideComponent={rightHandSideComponent}
keyForList={item.keyForList}
onFocus={onFocus}
shouldSyncFocus={shouldSyncFocus}
>
<>
<View style={[styles.flex1, styles.alignItemsStart]}>
Expand Down
2 changes: 2 additions & 0 deletions src/components/SelectionList/TableListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function TableListItem<TItem extends ListItem>({
shouldPreventDefaultFocusOnSelectRow,
rightHandSideComponent,
onFocus,
shouldSyncFocus,
}: TableListItemProps<TItem>) {
const styles = useThemeStyles();
const theme = useTheme();
Expand Down Expand Up @@ -58,6 +59,7 @@ function TableListItem<TItem extends ListItem>({
pendingAction={item.pendingAction}
keyForList={item.keyForList}
onFocus={onFocus}
shouldSyncFocus={shouldSyncFocus}
hoverStyle={item.isSelected && styles.activeComponentBG}
>
{(hovered) => (
Expand Down
4 changes: 4 additions & 0 deletions src/components/SelectionList/UserListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function UserListItem<TItem extends ListItem>({
onDismissError,
shouldPreventDefaultFocusOnSelectRow,
rightHandSideComponent,
onFocus,
shouldSyncFocus,
}: UserListItemProps<TItem>) {
const styles = useThemeStyles();
const theme = useTheme();
Expand Down Expand Up @@ -67,6 +69,8 @@ function UserListItem<TItem extends ListItem>({
) : undefined
}
keyForList={item.keyForList}
onFocus={onFocus}
shouldSyncFocus={shouldSyncFocus}
>
{(hovered?: boolean) => (
<>
Expand Down
7 changes: 7 additions & 0 deletions src/components/SelectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ type ListItemProps<TItem extends ListItem> = CommonListItemProps<TItem> & {

/** Key used internally by React */
keyForList?: string;

/**
* Whether the focus on the element should be synchronized. For example it should be set to false when the text input above list items is currently focused.
* When we type something into the text input, the first element found is focused, in this situation we should not synchronize the focus on the element because we will lose the focus from the text input.
*/
shouldSyncFocus?: boolean;
};

type BaseListItemProps<TItem extends ListItem> = CommonListItemProps<TItem> & {
Expand All @@ -149,6 +155,7 @@ type BaseListItemProps<TItem extends ListItem> = CommonListItemProps<TItem> & {
pendingAction?: PendingAction | null;
FooterComponent?: ReactElement;
children?: ReactElement<ListItemProps<TItem>> | ((hovered: boolean) => ReactElement<ListItemProps<TItem>>);
shouldSyncFocus?: boolean;
hoverStyle?: StyleProp<ViewStyle>;
};

Expand Down

0 comments on commit a573219

Please sign in to comment.