Created
August 30, 2018 18:39
-
-
Save majames/5ddc43655ed0d3971f66e4df4031e645 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface TableProps { | |
historyItems: HistoryItem[]; | |
} | |
interface TableState { | |
activeItems: boolean[] | |
} | |
export class HistoryTable extends Component<TableProps, TableState> { | |
state = { | |
activeItems: this.getDeactivatedArray() | |
} | |
componentWillReceiveProps(nextProps) { | |
if (nextProps.historyItems !== this.props.historyItems) { | |
this.setState({ activeItems: this.getDeactivatedArray() }) | |
} | |
} | |
render() { | |
const { historyItems } = this.props; | |
const itemEls = historyItems.map((item, i) => { | |
<li key={item.id}> | |
<HistoryItem item={item} onActivateChange={() => this.handleItemActivate(i)} /> | |
</li> | |
}); | |
return ( | |
<ul> | |
{itemEls} | |
</ul> | |
); | |
} | |
private handleItemActivate = (index: number) => { | |
this.setState(prevState => { | |
const activeItems = prevState.activeItems; | |
return { | |
activeItems: activeItems.map( | |
(isActive, i) => index === i ? !isActive : isActive | |
) | |
}; | |
}); | |
} | |
private getDeactivatedArray = () => { | |
return this.props.historyItems.map(_ => false); | |
} | |
} | |
interface ItemProps { | |
onActivateChange: () => void; | |
item: HistoryItem; | |
} | |
export function HistoryItem(props: ItemProps) { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment