UNPKG

@fluent-windows/core

Version:

React components that inspired by Microsoft's Fluent Design System.

45 lines (44 loc) 1.82 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } import * as React from 'react'; import * as PropTypes from 'prop-types'; import { createUseStyles } from '@fluent-windows/styles'; import { styles } from '../Table.styled'; import { TableContext } from './TableContext'; import classNames from 'classnames'; export const name = 'TableCell'; const useStyles = createUseStyles(styles, { name }); const Cell = React.forwardRef((props, ref) => { const { className: classNameProp, textAlign = 'left', children, ...rest } = props; const tableContext = React.useContext(TableContext); const Component = tableContext.variant === 'head' ? 'th' : 'td'; const classes = useStyles(props); const className = classNames({ [classes.cellRoot]: tableContext.variant !== 'head', [classes.headCellRoot]: tableContext.variant === 'head', [classes.cellTextAlignLeft]: textAlign === 'left', [classes.cellTextAlignRight]: textAlign === 'right', [classes.cellTextAlignCenter]: textAlign === 'center', [classes.cellTextAlignJustify]: textAlign === 'justify' }, classNameProp); return React.createElement(Component, _extends({ className: className, ref: ref }, rest), children); }); Cell.displayName = `F${name}`; Cell.propTypes = { className: PropTypes.string, children: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.element]).isRequired, textAlign: PropTypes.oneOf(['inherit', 'left', 'right', 'center', 'justify']) }; Cell.defaultProps = { textAlign: 'left' }; export default Cell;