Skip to content

Commit

Permalink
feat: Table components now forward ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Crash-- authored and JF-Cozy committed Nov 9, 2022
1 parent 39e57d9 commit f599a39
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
3 changes: 2 additions & 1 deletion react/Table/Readme.md
Expand Up @@ -8,11 +8,12 @@ import {
TableCell
} from 'cozy-ui/transpiled/react/Table';

let refRow = ''
const cellStyles = { flexGrow: 1 };

<Table>
<TableHead>
<TableRow>
<TableRow ref={c => (refRow = c)} onClick={() => alert(refRow)}>
<TableHeader style={cellStyles}>Firstname</TableHeader>
<TableHeader style={cellStyles}>Lastname</TableHeader>
</TableRow>
Expand Down
66 changes: 48 additions & 18 deletions react/Table/index.jsx
@@ -1,39 +1,69 @@
import React from 'react'
import styles from './styles.styl'
import cx from 'classnames'
import PropTypes from 'prop-types'

import styles from './styles.styl'

export const Table = props => {
export const Table = React.forwardRef((props, ref) => {
const { className, ...rest } = props

return <div className={cx(styles.Table, className)} {...rest} />
}
return <div className={cx(styles.Table, className)} {...rest} ref={ref} />
})

export const TableHead = props => {
export const TableHead = React.forwardRef((props, ref) => {
const { className, ...rest } = props

return <div className={cx(styles.TableHead, className)} {...rest} />
}
return <div className={cx(styles.TableHead, className)} {...rest} ref={ref} />
})

export const TableBody = props => {
export const TableBody = React.forwardRef((props, ref) => {
const { className, ...rest } = props

return <div className={cx(styles.TableBody, className)} {...rest} />
}
return <div className={cx(styles.TableBody, className)} {...rest} ref={ref} />
})

export const TableRow = props => {
export const TableRow = React.forwardRef((props, ref) => {
const { className, ...rest } = props

return <div className={cx(styles.TableRow, className)} {...rest} />
}
return <div className={cx(styles.TableRow, className)} {...rest} ref={ref} />
})

export const TableCell = props => {
export const TableCell = React.forwardRef((props, ref) => {
const { className, ...rest } = props

return <div className={cx(styles.TableCell, className)} {...rest} />
}
return <div className={cx(styles.TableCell, className)} {...rest} ref={ref} />
})

export const TableHeader = props => {
export const TableHeader = React.forwardRef((props, ref) => {
const { className, ...rest } = props

return <div className={cx(styles.TableHeader, className)} {...rest} />
return (
<div className={cx(styles.TableHeader, className)} {...rest} ref={ref} />
)
})

Table.propTypes = {
className: PropTypes.string,
ref: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}
TableHead.propTypes = {
className: PropTypes.string,
ref: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}
TableBody.propTypes = {
className: PropTypes.string,
ref: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}

TableRow.propTypes = {
className: PropTypes.string,
ref: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}
TableCell.propTypes = {
className: PropTypes.string,
ref: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}
TableHeader.propTypes = {
className: PropTypes.string,
ref: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
}

0 comments on commit f599a39

Please sign in to comment.