Skip to content

Commit

Permalink
feat: #1424
Browse files Browse the repository at this point in the history
  • Loading branch information
solarjoker committed Dec 15, 2020
1 parent 682f9c4 commit dd96003
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
7 changes: 4 additions & 3 deletions components/table/BodyTable.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useState, useContext, useRef, useEffect, useCallback } from 'react'
import React, { useContext, useRef, useEffect, useCallback } from 'react'
import Row from './Row'
import TableContext from './context'
import _ from 'lodash'
import { flatTreeData, setDepth } from './util'

const BodyTable = ({ fatherRef, emptyContent }) => {
const [expandedTreeRows, setExpandedTreeRows] = useState([])
const {
bordered,
data,
Expand All @@ -28,7 +27,9 @@ const BodyTable = ({ fatherRef, emptyContent }) => {
expandedRender,
expandedRowKeys,
rowSelection,
localeDatas
localeDatas,
expandedTreeRows,
setExpandedTreeRows
} = useContext(TableContext)
// **************** 获取colgroup
const _columns = _.cloneDeep(columns)
Expand Down
8 changes: 4 additions & 4 deletions components/table/Cell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ const Cell = ({
>
{level > 1 && columnIndex === 0 && <Indent times={level - 1} />}
{columnIndex === 0 &&
(rowData.children && rowData.children.length > 0 ? (
(allRowData.children && allRowData.children.length > 0 ? (
<Icon
style={{ marginRight: 4, cursor: 'pointer' }}
name={expandedTree ? 'caret-down' : 'caret-right'}
onClick={() => {
const _expandedTreeRows = [...expandedTreeRows]
if (_expandedTreeRows.includes(rowData.key)) {
const idx = _expandedTreeRows.findIndex((row) => row === rowData.key)
if (_expandedTreeRows.includes(allRowData.key)) {
const idx = _expandedTreeRows.findIndex((row) => row === allRowData.key)
_expandedTreeRows.splice(idx, 1)
setExpandedTreeRows(_expandedTreeRows)
} else {
_expandedTreeRows.push(rowData.key)
_expandedTreeRows.push(allRowData.key)
setExpandedTreeRows(_expandedTreeRows)
}
}}
Expand Down
35 changes: 26 additions & 9 deletions components/table/FixedBodyTable.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useState, useContext, useRef } from 'react'
import React, { useContext, useRef } from 'react'
import Row from './Row'
import TableContext from './context'
import _ from 'lodash'
import { flatTreeData, setDepth } from './util'

const FixedBodyTable = ({ isFixed, rightFixedIndex }) => {
const [expandedTreeRows, setExpandedTreeRows] = useState([])
const {
data,
leftFixedData,
Expand All @@ -25,7 +24,9 @@ const FixedBodyTable = ({ isFixed, rightFixedIndex }) => {
bordered,
eachRowHeight,
rowSelection,
expandedRender
expandedRender,
expandedTreeRows,
setExpandedTreeRows
} = useContext(TableContext)
let _columns
if (isFixed === 'left') {
Expand All @@ -50,7 +51,18 @@ const FixedBodyTable = ({ isFixed, rightFixedIndex }) => {
// ***********
const bodyInner = useRef(null)

const renderRow = (row, level, index, allRowData) => {
let hasTree = false
if (data && data.length) {
hasTree = data.some((row) => {
return row.children && row.children.length
})
}

const renderRow = (row, level, index, allRowData, isTree) => {
let childrenHasTree = false
if (allRowData.children && allRowData.children.length) {
childrenHasTree = allRowData.children.some((child) => child.children && child.children.length)
}
return (
<React.Fragment key={row.key}>
<Row
Expand All @@ -63,11 +75,12 @@ const FixedBodyTable = ({ isFixed, rightFixedIndex }) => {
expandedTree={expandedTreeRows.includes(row.key)}
expandedTreeRows={expandedTreeRows}
setExpandedTreeRows={setExpandedTreeRows}
isTree={isTree}
/>
{row.children &&
expandedTreeRows.includes(row.key) &&
row.children.map((child) => {
return renderRow(child, level + 1)
{allRowData.children &&
expandedTreeRows.includes(allRowData.key) &&
allRowData.children.map((child, idx) => {
return renderRow(child, level + 1, index, allRowData.children[idx], childrenHasTree || isTree)
})}
</React.Fragment>
)
Expand Down Expand Up @@ -173,7 +186,11 @@ const FixedBodyTable = ({ isFixed, rightFixedIndex }) => {
)
})}
</colgroup>
<tbody>{_fixedData.map((row, index) => renderRow(row, 1, index, data[index]))}</tbody>
<tbody>
{_fixedData.map((row, index) => {
return renderRow(row, 1, index, data[index], hasTree)
})}
</tbody>
</table>
</div>
</div>
Expand Down
5 changes: 4 additions & 1 deletion components/table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const Table = ({
const [eachHeaderHeight, setEachHeaderHeight] = useState(null)

const [realColumnsWidth, setRealColumnsWidth] = useState(columns.map((c) => c.width || 'auto'))
const [expandedTreeRows, setExpandedTreeRows] = useState([])

const firstRowRef = useRef(null)
useEffect(() => {
Expand Down Expand Up @@ -226,7 +227,9 @@ const Table = ({
eachHeaderHeight,
setEachHeaderHeight,
theme,
localeDatas
localeDatas,
expandedTreeRows,
setExpandedTreeRows
}}
>
<div
Expand Down
2 changes: 2 additions & 0 deletions docs/demo/table/section-special.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ const code = [
class Demo extends React.Component {
render() {
return <Table
scrollWidth={1700}
fixedToColumn={'a'}
data={[
{
a: 'a-1',
Expand Down

0 comments on commit dd96003

Please sign in to comment.