Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/The-Code-Monkey/react-sup…
…er-responsive-table into The-Code-Monkey-master
- Loading branch information
Showing
2 changed files
with
168 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React from 'react'; | ||
|
||
import { Table, Tbody, Td, Th, Thead, Tr } from './SuperResponsiveTable' | ||
|
||
const getType = el => el.type; | ||
|
||
const makeTable = (tableChildren) => ( | ||
<Table> | ||
{Array.isArray(tableChildren) ? | ||
tableChildren.map(child => { | ||
if (getType(child) === 'thead') { | ||
return makeThead(child.props.children); | ||
} | ||
if (getType(child) === 'tbody') { | ||
return makeTbody(child.props.children); | ||
} | ||
return child; | ||
}) | ||
: | ||
getType(tableChildren) === 'thead' ? ( | ||
makeThead(tableChildren.props.children) | ||
) | ||
: | ||
tableChildren | ||
} | ||
</Table> | ||
); | ||
const makeTbody = (tbodyChildren) => ( | ||
<Tbody> | ||
{Array.isArray(tbodyChildren) ? | ||
tbodyChildren.map((child, key) => { | ||
if (getType(child) === 'tr') { | ||
return makeTbodyTr(child.props.children, key) | ||
} | ||
}) | ||
: | ||
getType(tbodyChildren) === 'tr' ? | ||
makeTbodyTr(tbodyChildren.props.children, 0) | ||
: | ||
tbodyChildren | ||
} | ||
</Tbody> | ||
) | ||
const makeThead = (theadChildren) => ( | ||
<Thead> | ||
{Array.isArray(theadChildren) ? | ||
|
||
theadChildren.map((child, key) => { | ||
if (getType(child) === 'tr') { | ||
return makeTheadTr(child.props.children, key) | ||
} | ||
}) | ||
: | ||
getType(theadChildren) === 'tr' ? ( | ||
makeTheadTr(theadChildren.props.children, 0) | ||
) | ||
: | ||
theadChildren | ||
} | ||
</Thead> | ||
); | ||
const makeTbodyTr = (tbodyTrChildren, key) => ( | ||
<Tr key={key} columnKey={key}> | ||
{Array.isArray(tbodyTrChildren) ? | ||
tbodyTrChildren.map(child => { | ||
if (getType(child) === 'td') { | ||
return makeTbodyTd(child.props.children, key) | ||
} | ||
}) | ||
: | ||
getType(tbodyTrChildren) === 'td' ? | ||
makeTbodyTd(tbodyTrChildren.props.children, key) | ||
: | ||
tbodyTrChildren | ||
} | ||
</Tr> | ||
) | ||
const makeTheadTr = (theadTrChildren, key) => ( | ||
<Tr key={key} columnKey={key}> | ||
{Array.isArray(theadTrChildren) ? | ||
theadTrChildren.map(child => { | ||
if (getType(child) === 'th') { | ||
return makeTheadTh(child.props.children, key) | ||
} | ||
}) | ||
: | ||
getType(theadTrChildren) === 'th' ? | ||
makeTheadTh(theadTrChildren.props.children, key) | ||
: | ||
theadTrChildren | ||
} | ||
</Tr> | ||
); | ||
const makeTbodyTd = (tbodyTdChildren, key) => ( | ||
<Td> | ||
{tbodyTdChildren} | ||
</Td> | ||
); | ||
const makeTheadTh = (theadThChildren, key) => ( | ||
<Th> | ||
{theadThChildren} | ||
</Th> | ||
) | ||
|
||
const findTable = (props) => { | ||
const { children } = props; | ||
if (getType(children) === 'table') { | ||
return makeTable(children.props.children) | ||
} else { | ||
return React.createElement(children.type, {...props}, findTable(children.props)) | ||
} | ||
}; | ||
|
||
export class AutoTableWrapper extends React.Component { | ||
render() { | ||
const { props } = this; | ||
const { children } = props; | ||
if (children) { | ||
return findTable(props); | ||
} else { | ||
return "Auto Table Requires Children To Work" | ||
} | ||
} | ||
} |