item.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React, { PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { cssClasses } from '@douyinfe/semi-foundation/descriptions/constants';
  4. import '@douyinfe/semi-foundation/descriptions/descriptions.scss';
  5. import getDataAttr from '@douyinfe/semi-foundation/utils/getDataAttr';
  6. import DescriptionsContext, { DescriptionsContextValue } from './descriptions-context';
  7. export interface DescriptionsItemProps {
  8. hidden?: boolean;
  9. className?: string;
  10. children?: React.ReactNode | (() => React.ReactNode);
  11. style?: React.CSSProperties;
  12. itemKey?: React.ReactNode
  13. }
  14. const prefixCls = cssClasses.PREFIX;
  15. const keyCls = `${prefixCls}-key`;
  16. const valCls = `${prefixCls}-value`;
  17. export default class Item extends PureComponent<DescriptionsItemProps> {
  18. static propTypes = {
  19. itemKey: PropTypes.node,
  20. hidden: PropTypes.bool,
  21. className: PropTypes.string,
  22. style: PropTypes.object
  23. };
  24. static contextType = DescriptionsContext;
  25. context: DescriptionsContextValue;
  26. render() {
  27. const { itemKey, hidden, className, style, children, ...rest } = this.props;
  28. const { align } = this.context;
  29. if (hidden) {
  30. return null;
  31. }
  32. const item = align === 'plain' ?
  33. (
  34. <tr className={className} style={style} {...getDataAttr(rest)}>
  35. <td className={`${prefixCls}-item`}>
  36. <span className={keyCls}>
  37. {itemKey}:
  38. </span>
  39. <span className={valCls}>
  40. {typeof children === 'function' ? children() : children}
  41. </span>
  42. </td>
  43. </tr>
  44. ) :
  45. (
  46. <tr className={className} style={style} {...getDataAttr(rest)}>
  47. <th className={`${prefixCls}-item ${prefixCls}-item-th`}>
  48. <span className={keyCls}>
  49. {itemKey}
  50. </span>
  51. </th>
  52. <td className={`${prefixCls}-item ${prefixCls}-item-td`}>
  53. <span className={valCls}>
  54. {typeof children === 'function' ? children() : children}
  55. </span>
  56. </td>
  57. </tr>
  58. );
  59. return item;
  60. }
  61. }