Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Feat: Highlight currently selected row in table #1482

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
5 changes: 4 additions & 1 deletion src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1545,9 +1545,12 @@ table {
tr {
cursor: pointer;

&:hover {
&.is-highlighted {
background: $gray-hover !important;
}

&:hover {
background: $gray-hover !important;

.dropdown {
background: $gray-hover;
Expand Down
22 changes: 20 additions & 2 deletions src/components/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ function Home(props) {
const [fetched, setFetched] = useState(false);
const [activeStateCode, setActiveStateCode] = useState('TT');
const [regionHighlighted, setRegionHighlighted] = useState(undefined);
const [rowHighlighted, setRowHighlighted] = useState({
statecode: undefined,
isDistrict: false,
districtName: undefined,
});
const [showUpdates, setShowUpdates] = useState(false);
const [anchor, setAnchor] = useState(null);
const [lastViewedLog, setLastViewedLog] = useLocalStorage(
Expand Down Expand Up @@ -110,8 +115,20 @@ function Home(props) {
setRegionHighlighted({district, state, index});
};

const onMapHighlightChange = useCallback(({statecode}) => {
setActiveStateCode(statecode);
const onMapHighlightChange = useCallback((region) => {
setActiveStateCode(region.statecode);
if ('districtName' in region)
setRowHighlighted({
statecode: region.statecode,
isDistrict: true,
districtName: region.districtName,
});
else
setRowHighlighted({
statecode: region.statecode,
isDistrict: false,
districtName: undefined,
});
}, []);

return (
Expand Down Expand Up @@ -159,6 +176,7 @@ function Home(props) {
states={states}
summary={false}
stateDistrictWiseData={stateDistrictWiseData}
rowHighlighted={rowHighlighted}
onHighlightState={onHighlightState}
onHighlightDistrict={onHighlightDistrict}
/>
Expand Down
1 change: 1 addition & 0 deletions src/components/mapexplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function MapExplorer({
setPanelRegion(panelRegion);
currentHoveredRegion.statecode = panelRegion.statecode;
setCurrentHoveredRegion(currentHoveredRegion);
panelRegion.districtName = currentHoveredRegion.name;
if (onMapHighlightChange) onMapHighlightChange(panelRegion);
}
},
Expand Down
8 changes: 6 additions & 2 deletions src/components/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function Row(props) {
<tr
className={`state ${props.total ? 'is-total' : ''} ${
props.index % 2 === 0 ? 'is-odd' : ''
}`}
} ${props.isHighlighted ? 'is-highlighted' : ''}`}
onMouseEnter={() => props.onHighlightState?.(state, props.index)}
onMouseLeave={() => props.onHighlightState?.()}
onClick={!props.total ? handleReveal : null}
Expand Down Expand Up @@ -275,7 +275,11 @@ function Row(props) {
return (
<tr
key={index}
className={`district ${index % 2 === 0 ? 'is-odd' : ''}`}
className={`district ${index % 2 === 0 ? 'is-odd' : ''} ${
props.highlightedDistrict === district
? 'is-highlighted'
: ''
}`}
style={{
background: index % 2 === 0 ? '#f8f9fa' : '',
}}
Expand Down
17 changes: 15 additions & 2 deletions src/components/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import Row from './row';
import React, {useState, useEffect} from 'react';
import {Link} from 'react-router-dom';

const isEqual = () => {
return true;
const isEqual = (prevProps, currProps) => {
return (
JSON.stringify(prevProps.rowHighlighted) ===
JSON.stringify(currProps.rowHighlighted)
);
};

function Table(props) {
Expand Down Expand Up @@ -271,6 +274,16 @@ function Table(props) {
? districts[state.state].districtData
: []
}
isHighlighted={
!props.rowHighlighted.isDistrict &&
props.rowHighlighted.statecode === state.statecode
}
highlightedDistrict={
props.rowHighlighted.isDistrict &&
props.rowHighlighted.statecode === state.statecode
? props.rowHighlighted.districtName
: null
}
onHighlightState={props.onHighlightState}
onHighlightDistrict={props.onHighlightDistrict}
handleReveal={handleReveal}
Expand Down