feat: table select

This commit is contained in:
ysandler 2020-07-17 23:31:08 -05:00 committed by Joshua Shoemaker
parent 9ca49b9ef0
commit ba54fdf044
2 changed files with 18 additions and 13 deletions

View File

@ -23,15 +23,9 @@ class Tables {
} }
} }
removeTableById = id => { removeById = id => {
const indexToRemove = this.collection.findIndex(t => t.id === id) const indexToRemove = this.collection.findIndex(t => t.id === id)
if (this.collection.length === 1 && indexToRemove > -1) { if (indexToRemove > -1) this.collection.splice(indexToRemove, 1)
this.collection = []
}
else {
const modifiedCollection = this.collection.splice(indexToRemove, 1)
this.collection = modifiedCollection
}
} }
getCollectionProps = () => this.collection.map(table => table.getProperties()) getCollectionProps = () => this.collection.map(table => table.getProperties())

View File

@ -9,14 +9,25 @@ class TableSelect extends Component {
super() super()
this.tables = new Tables() this.tables = new Tables()
this.state = { this.state = {
selectedTables: [], selectedTablesLabels: [],
tables: this.tables.getCollectionProps() tables: this.tables.getCollectionProps()
} }
document.addEventListener('updateTables', this.updateTableList) document.addEventListener('updateTables', this.updateTableList)
} }
getSelectedTableLabels = () => this.state.selectedTablesLabels
toggleSelect = label => { toggleSelect = label => {
console.log(label) const { selectedTablesLabels } = this.state
let newselectedTablesLabels = selectedTablesLabels
const selectedIndex = selectedTablesLabels.findIndex(selected => selected === label)
if (selectedIndex === -1) newselectedTablesLabels.push(label)
else if (selectedIndex === 0 && selectedTablesLabels.length === 1) newselectedTablesLabels = []
else newselectedTablesLabels.splice(selectedIndex, 1)
this.setState({ selectedTablesLabels: newselectedTablesLabels })
} }
updateTableList = () => { updateTableList = () => {
@ -24,11 +35,11 @@ class TableSelect extends Component {
} }
renderTableLabels = () => { renderTableLabels = () => {
const { selectedTables, tables } = this.state const { selectedTablesLabels, tables } = this.state
const tableLabelElements = tables.map(t => { const tableLabelElements = tables.map(t => {
const isSelected = label => selectedTables.includes(label) const isSelected = selectedTablesLabels.includes(t.label)
return ( return (
<Label onClick={() => this.toggleSelect(t.label)}> <Label onClick={() => this.toggleSelect(t.label)} color={isSelected ? 'blue' : 'grey'} style={{ cursor: 'pointer' }}>
{t.label} {t.label}
{ isSelected ? <Icon name='delete' /> : '' } { isSelected ? <Icon name='delete' /> : '' }
</Label> </Label>