Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 7d477849

Přidáno uživatelem Václav Honzík před asi 2 roky(ů)

reidxing + drag jump fix

re #9741

Zobrazit rozdíly:

frontend/src/features/TrackingTool/DraggableList/DraggableListItem.tsx
5 5
    ListItemAvatar,
6 6
    ListItemText,
7 7
} from '@mui/material'
8
import makeStyles from '@mui/material/styles/makeStyles'
9 8
import { Draggable } from 'react-beautiful-dnd'
10 9
import { MapPoint, PathVariant } from '../Map/pathUtils'
11 10
import LocationOnIcon from '@mui/icons-material/LocationOn'
12 11
import { CatalogItemDto } from '../../../swagger/data-contracts'
13 12
import { useDispatch } from 'react-redux'
14 13
import { updateMapMarker, updateMapMarkerWithId } from '../trackingToolSlice'
15
import { Fragment, useEffect, useState } from 'react'
14
import { useMemo } from 'react'
16 15

  
17 16
export type DraggableListItemProps = {
18 17
    list: PathVariant
19
    index: number
18
    idx: number
20 19
}
21 20

  
22 21
const getFormattedLocationOrEmpty = (catalogItem: CatalogItemDto) => {
......
27 26
    return `${catalogItem.latitude}°, ${catalogItem.longitude}°`
28 27
}
29 28

  
30
const DraggableListItem = ({ list, index }: DraggableListItemProps) => {
31
    const item = list[index]
29
const DraggableListItem = ({ list, idx }: DraggableListItemProps) => {
30
    const item = list[idx]
32 31
    const dispatch = useDispatch()
33
    const toggleHidden = () => {
34
        dispatch(
35
            updateMapMarkerWithId({
36
                item: {
37
                    ...item,
38
                    active: !item?.active,
39
                } as MapPoint,
40
                id: item.id,
41
            })
42
        )
43
    }
44 32

  
45
    return (
46
        <Draggable key={`${item.id}`} draggableId={`${item.id}`} index={index}>
47
            {(provided, snapshot) => (
48
                <ListItem
49
                    ref={provided.innerRef}
50
                    {...provided.draggableProps}
51
                    {...provided.dragHandleProps}
52
                    sx={{
53
                        background: snapshot.isDragging
54
                            ? 'rgb(235,235,235)'
55
                            : 'inherit',
56
                    }}
57
                >
58
                    <ListItemAvatar>
59
                        <Avatar>
60
                            <LocationOnIcon />
61
                        </Avatar>
62
                    </ListItemAvatar>
63
                    <ListItemText
64
                        primary={item.catalogItem.name ?? 'Unknown name'}
65
                        secondary={getFormattedLocationOrEmpty(
66
                            item.catalogItem
67
                        )}
68
                    />
69
                    <Checkbox checked={item.active} onChange={toggleHidden} />
70
                </ListItem>
71
            )}
72
        </Draggable>
73
    )
33
    // useMemo to prevent unnecessary re-renders which will make the list jumpy
34
    return useMemo(() => {
35
        const toggleHidden = () => {
36
            dispatch(
37
                updateMapMarkerWithId({
38
                    item: {
39
                        ...item,
40
                        active: !item?.active,
41
                    } as MapPoint,
42
                    id: item.id,
43
                })
44
            )
45
        }
46

  
47
        return (
48
            <Draggable
49
                key={`${item.id}`}
50
                draggableId={`${item.id}`}
51
                index={idx}
52
            >
53
                {(provided, snapshot) => (
54
                    <ListItem
55
                        ref={provided.innerRef}
56
                        {...provided.draggableProps}
57
                        {...provided.dragHandleProps}
58
                        sx={{
59
                            background: snapshot.isDragging
60
                                ? 'rgb(235,235,235)'
61
                                : 'inherit',
62
                        }}
63
                    >
64
                        <ListItemAvatar>
65
                            <Avatar>
66
                                <LocationOnIcon />
67
                            </Avatar>
68
                        </ListItemAvatar>
69
                        <ListItemText
70
                            primary={item.catalogItem.name ?? 'Unknown name'}
71
                            secondary={getFormattedLocationOrEmpty(
72
                                item.catalogItem
73
                            )}
74
                        />
75
                        <Checkbox
76
                            checked={item.active}
77
                            onChange={toggleHidden}
78
                        />
79
                    </ListItem>
80
                )}
81
            </Draggable>
82
        )
83
    }, [item, idx, dispatch])
74 84
}
75 85

  
76 86
export default DraggableListItem

Také k dispozici: Unified diff