Skip to content
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 shouldSyncFocus prop to ListItem components #40234

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = () => {},
}: BaseListItemProps<TItem>) {
const theme = useTheme();
Expand All @@ -37,7 +38,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 @@ -92,6 +92,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 @@ -349,7 +350,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 @@ -522,6 +524,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}
>
{(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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure what this onFocus is doing?

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 @@ -137,6 +137,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 @@ -147,6 +153,7 @@ type BaseListItemProps<TItem extends ListItem> = CommonListItemProps<TItem> & {
pendingAction?: PendingAction | null;
FooterComponent?: ReactElement;
children?: ReactElement<ListItemProps<TItem>> | ((hovered: boolean) => ReactElement<ListItemProps<TItem>>);
shouldSyncFocus?: boolean;
};

type UserListItemProps<TItem extends ListItem> = ListItemProps<TItem> & {
Expand Down
Loading