index.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { PureComponent } from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { strings, cssClasses } from '@douyinfe/semi-foundation/descriptions/constants';
  5. import '@douyinfe/semi-foundation/descriptions/descriptions.scss';
  6. import { isPlainObject } from 'lodash';
  7. import DescriptionsContext, { DescriptionsAlign, DescriptionsContextValue } from './descriptions-context';
  8. import Item from './item';
  9. export { DescriptionsItemProps } from './item';
  10. export type DescriptionsSize = 'small' | 'medium' | 'large';
  11. export interface Data {
  12. key?: React.ReactNode;
  13. value?: (() => React.ReactNode) | React.ReactNode;
  14. hidden?: boolean;
  15. }
  16. export interface DescriptionsProps {
  17. align?: DescriptionsAlign;
  18. row?: boolean;
  19. size?: DescriptionsSize;
  20. style?: React.CSSProperties;
  21. className?: string;
  22. children?: React.ReactNode | undefined;
  23. data?: Data[];
  24. }
  25. const prefixCls = cssClasses.PREFIX;
  26. class Descriptions extends PureComponent<DescriptionsProps> {
  27. static Item = Item;
  28. static contextType = DescriptionsContext;
  29. static propTypes = {
  30. align: PropTypes.oneOf(strings.ALIGN_SET),
  31. row: PropTypes.bool,
  32. size: PropTypes.oneOf(strings.SIZE_SET),
  33. style: PropTypes.object,
  34. className: PropTypes.string,
  35. data: PropTypes.arrayOf(PropTypes.shape({
  36. key: PropTypes.node,
  37. value: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
  38. hidden: PropTypes.bool,
  39. className: PropTypes.string,
  40. style: PropTypes.object
  41. })),
  42. };
  43. static defaultProps = {
  44. align: 'center',
  45. row: false,
  46. size: 'medium',
  47. data: [] as Array<Data>,
  48. };
  49. render() {
  50. const { align, row, size, className, style, children, data } = this.props;
  51. const classNames = cls(prefixCls, className, {
  52. [`${prefixCls}-${align}`]: !row,
  53. [`${prefixCls}-double`]: row,
  54. [`${prefixCls}-double-${size}`]: row,
  55. });
  56. const childrenList = data && data.length ?
  57. data.map((item, index) => (
  58. isPlainObject(item) ? <Item itemKey={item.key} {...item} key={index}>{item.value}</Item> : null
  59. )) :
  60. children;
  61. return (
  62. <div className={classNames} style={style}>
  63. <table>
  64. <tbody>
  65. <DescriptionsContext.Provider value={{ align }}>
  66. {childrenList}
  67. </DescriptionsContext.Provider>
  68. </tbody>
  69. </table>
  70. </div>
  71. );
  72. }
  73. }
  74. export default Descriptions;