item.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React, { PureComponent, CSSProperties, ComponentType, FC, ReactElement, ReactNode } from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { cssClasses } from '@douyinfe/semi-foundation/skeleton/constants';
  5. import { strings } from '@douyinfe/semi-foundation/avatar/constants';
  6. import '@douyinfe/semi-foundation/skeleton/skeleton.scss';
  7. export type BasicProps = {
  8. prefixCls?: string;
  9. className?: string;
  10. style?: CSSProperties;
  11. type?: string
  12. };
  13. export interface ParagraphProps extends BasicProps {
  14. rows?: number
  15. }
  16. export interface AvatarProps extends BasicProps {
  17. size?: typeof strings.SIZE[number];
  18. shape?: string
  19. }
  20. export type GenericProps = BasicProps & AvatarProps;
  21. const sizeSet = strings.SIZE;
  22. const shapeSet = strings.SHAPE;
  23. const generator = <T extends BasicProps>(type: string) => (BasicComponent: ComponentType<T>): FC<T> => (
  24. props
  25. ): ReactElement => <BasicComponent type={type} {...props} />;
  26. class Generic extends PureComponent<GenericProps> {
  27. static propTypes = {
  28. type: PropTypes.string,
  29. prefixCls: PropTypes.string,
  30. style: PropTypes.object,
  31. className: PropTypes.string,
  32. size: PropTypes.oneOf(sizeSet),
  33. shape: PropTypes.oneOf(shapeSet),
  34. };
  35. static defaultProps = {
  36. prefixCls: cssClasses.PREFIX,
  37. size: 'medium',
  38. shape: 'circle',
  39. };
  40. render() {
  41. const { prefixCls, className, type, size, shape, ...others } = this.props;
  42. const classString = cls(
  43. className,
  44. `${prefixCls}-${type}`,
  45. {
  46. [`${prefixCls}-${type}-${size}`]: type.toUpperCase() === 'AVATAR',
  47. },
  48. {
  49. [`${prefixCls}-${type}-${shape}`]: type.toUpperCase() === 'AVATAR',
  50. }
  51. );
  52. return React.createElement('div', { className: classString, ...others });
  53. }
  54. }
  55. export const Avatar = generator<AvatarProps>('avatar')(Generic);
  56. export const Image = generator<BasicProps>('image')(Generic);
  57. export const Title = generator<BasicProps>('title')(Generic);
  58. export const Button = generator<BasicProps>('button')(Generic);
  59. export class Paragraph extends PureComponent<ParagraphProps> {
  60. static propTypes = {
  61. rows: PropTypes.number,
  62. prefixCls: PropTypes.string,
  63. style: PropTypes.object,
  64. className: PropTypes.string,
  65. };
  66. static defaultProps = {
  67. prefixCls: cssClasses.PREFIX,
  68. rows: 4,
  69. };
  70. render(): ReactNode {
  71. const { prefixCls, className, style, rows } = this.props;
  72. const classString = cls(className, `${prefixCls}-paragraph`);
  73. return (
  74. <ul className={classString} style={style}>
  75. {[...Array(rows)].map((e, i) => (
  76. <li key={i} />
  77. ))}
  78. </ul>
  79. );
  80. }
  81. }