-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated TablePanel to the latest react features
- Loading branch information
1 parent
0209efd
commit 7d1363b
Showing
5 changed files
with
74 additions
and
95 deletions.
There are no files selected for viewing
This file contains 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
42 changes: 18 additions & 24 deletions
42
showcase/src/main/scala/scommons/client/showcase/demo/TablePanelDemo.scala
This file contains 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
83 changes: 31 additions & 52 deletions
83
ui/src/main/scala/scommons/client/ui/table/TablePanel.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,53 @@ | ||
package scommons.client.ui.table | ||
|
||
import io.github.shogowada.scalajs.reactjs.React | ||
import io.github.shogowada.scalajs.reactjs.React.Self | ||
import io.github.shogowada.scalajs.reactjs.VirtualDOM._ | ||
import io.github.shogowada.scalajs.reactjs.classes.ReactClass | ||
import io.github.shogowada.scalajs.reactjs.events.MouseSyntheticEvent | ||
import scommons.client.ui.table.TablePanelCss._ | ||
import scommons.react.UiComponent | ||
import scommons.react._ | ||
|
||
case class TablePanelProps(header: List[TableColumnData], | ||
rows: List[TableRowData], | ||
selectedIds: Set[String] = Set.empty, | ||
onSelect: TableRowData => Unit = _ => ()) | ||
|
||
object TablePanel extends UiComponent[TablePanelProps] { | ||
object TablePanel extends FunctionComponent[TablePanelProps] { | ||
|
||
private type TablePanelSelf = Self[PropsType, TablePanelState] | ||
protected def render(compProps: Props): ReactElement = { | ||
val props = compProps.wrapped | ||
|
||
private case class TablePanelState(selectedIds: Set[String]) | ||
|
||
protected def create(): ReactClass = React.createClass[PropsType, TablePanelState]( | ||
getInitialState = { self => | ||
TablePanelState(self.props.wrapped.selectedIds) | ||
}, | ||
componentWillReceiveProps = { (self, nextProps) => | ||
val props = nextProps.wrapped | ||
if (self.props.wrapped.selectedIds != props.selectedIds) { | ||
self.setState(_.copy(selectedIds = props.selectedIds)) | ||
} | ||
}, | ||
render = { self => | ||
val props = self.props.wrapped | ||
|
||
val tableHeader = props.header.map { column => | ||
<.th(^.colspan := 1)(column.title) | ||
} | ||
|
||
val tableBody = props.rows.map { row => | ||
val rowClass = | ||
if (isRowSelected(self.state, row)) tablePanelSelectedRow | ||
else tablePanelRow | ||
|
||
<.tr( | ||
^.className := rowClass, | ||
^.onClick := rowClick(self, row) | ||
)(row.cells.map { cell => | ||
<.td()(cell) | ||
}) | ||
} | ||
val tableHeader = props.header.map { column => | ||
<.th(^.colspan := 1)(column.title) | ||
} | ||
|
||
<.table(^.className := "table table-condensed", ^("cellSpacing") := "0")( | ||
<.thead(^("aria-hidden") := "false")( | ||
<.tr()(tableHeader) | ||
), | ||
<.tbody()(tableBody) | ||
) | ||
val tableBody = props.rows.map { row => | ||
val rowClass = | ||
if (isRowSelected(props, row)) tablePanelSelectedRow | ||
else tablePanelRow | ||
|
||
<.tr( | ||
^.className := rowClass, | ||
^.onClick := rowClick(props, row) | ||
)(row.cells.map { cell => | ||
<.td()(cell) | ||
}) | ||
} | ||
) | ||
|
||
private def isRowSelected(state: TablePanelState, row: TableRowData): Boolean = { | ||
state.selectedIds.contains(row.id) | ||
<.table(^.className := "table table-condensed", ^("cellSpacing") := "0")( | ||
<.thead(^("aria-hidden") := "false")( | ||
<.tr()(tableHeader) | ||
), | ||
<.tbody()(tableBody) | ||
) | ||
} | ||
|
||
private def rowClick(self: TablePanelSelf, row: TableRowData): MouseSyntheticEvent => Unit = { _ => | ||
if (!isRowSelected(self.state, row)) { | ||
//event.stopPropagation() | ||
private def isRowSelected(props: TablePanelProps, row: TableRowData): Boolean = { | ||
props.selectedIds.contains(row.id) | ||
} | ||
|
||
self.setState(s => s.copy(selectedIds = Set(row.id))) | ||
private def rowClick(props: TablePanelProps, row: TableRowData): MouseSyntheticEvent => Unit = { _ => | ||
if (!isRowSelected(props, row)) { | ||
//event.stopPropagation() | ||
|
||
self.props.wrapped.onSelect(row) | ||
props.onSelect(row) | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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