Skip to content

Support overriding backgroundColor for SortableListItem #3255

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

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions demo/src/screens/componentScreens/SortableListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const SortableListScreen = () => {
<SortableList
flexMigration
data={items}
// itemProps={{backgroundColor: 'transparent'}}
renderItem={renderItem}
keyExtractor={keyExtractor}
onOrderChange={onOrderChange}
Expand Down
12 changes: 6 additions & 6 deletions src/components/sortableList/SortableListContext.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {createContext} from 'react';
import {ViewProps} from 'react-native';
import type {ViewProps, FlatListProps} from 'react-native';
import {SharedValue} from 'react-native-reanimated';
import type {Dictionary} from '../../typings/common';
import {Data, SortableListItemProps} from './types';
import {Data, OrderChangeInfo, SortableListItemProps, SortableListProps} from './types';

export interface SortableListContextType<ItemT extends SortableListItemProps> {
data: Data<ItemT>;
itemsOrder: SharedValue<string[]>;
lockedIds: SharedValue<Dictionary<boolean>>;
onChange: () => void;
onChange: (info: OrderChangeInfo) => void;
itemSize: SharedValue<number>;
horizontal?: boolean;
horizontal?: FlatListProps<ItemT>['horizontal'];
onItemLayout: ViewProps['onLayout'];
enableHaptic?: boolean;
scale?: number;
itemProps?: {margins?: {marginTop?: number; marginBottom?: number; marginLeft?: number; marginRight?: number}};
itemProps?: SortableListProps<ItemT>['itemProps'];
}

// @ts-ignore
const SortableListContext = createContext<SortableListContextType>({});
const SortableListContext = createContext<SortableListContextType<SortableListItemProps>>({});

export default SortableListContext;
4 changes: 2 additions & 2 deletions src/components/sortableList/SortableListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const SortableListItem = (props: Props) => {
} = useContext(SortableListContext);
const {getTranslationByIndexChange, getItemIndexById, getIndexByPosition, getIdByItemIndex} = usePresenter();
const id: string = data[index].id;
const locked: boolean = data[index].locked;
const locked: boolean = data[index].locked ?? false;
const initialIndex = useSharedValue<number>(map(data, 'id').indexOf(id));
const lastSwap = useSharedValue({from: -1, to: -1});
const currIndex = useSharedValue(initialIndex.value);
Expand Down Expand Up @@ -172,7 +172,7 @@ const SortableListItem = (props: Props) => {
: defaultItemShadow.value;

return {
backgroundColor: LIST_ITEM_BACKGROUND, // required for elevation to work in Android
backgroundColor: itemProps?.backgroundColor ?? LIST_ITEM_BACKGROUND, // required for elevation to work in Android
zIndex,
transform: [horizontal ? {translateX: translation.value} : {translateY: translation.value}, {scale}],
opacity,
Expand Down
3 changes: 2 additions & 1 deletion src/components/sortableList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ const SortableList = <ItemT extends SortableListItemProps>(props: SortableListPr
enableHaptic,
scale
};
}, [data]);
}, [data, onChange]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did this cause a bug (it looks like this is only affected by the user changing onOrderChange)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You mean it's on purpose?
But if the user changes the onOrderChange, it's not implausible


return (
<GestureHandlerRootView style={flexMigration ? styles.container : undefined}>
<SortableListContext.Provider value={context}>
Expand Down
7 changes: 5 additions & 2 deletions src/components/sortableList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface SortableListItemProps {
}

// Internal
export type Data<ItemT extends SortableListItemProps> = FlatListProps<ItemT>['data'];
export type Data<ItemT extends SortableListItemProps> = ArrayLike<ItemT>;
export type OrderChangeInfo = {from: number, to: number};

export interface SortableListProps<ItemT extends SortableListItemProps>
Expand All @@ -31,7 +31,10 @@ export interface SortableListProps<ItemT extends SortableListItemProps>
/**
* Extra props for the item
*/
itemProps?: {margins?: {marginTop?: number; marginBottom?: number; marginLeft?: number; marginRight?: number}};
itemProps?: {
backgroundColor?: string;
margins?: {marginTop?: number; marginBottom?: number; marginLeft?: number; marginRight?: number}
};
/**
* List forwarded ref.
*/
Expand Down