item.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }
  19. export type GenericProps = BasicProps & AvatarProps;
  20. const sizeSet = strings.SIZE;
  21. const generator = <T extends BasicProps >(type: string) =>
  22. (BasicComponent: ComponentType<T>): FC<T> =>
  23. (props): ReactElement => <BasicComponent type={type} {...props} />;
  24. class Generic extends PureComponent<GenericProps> {
  25. static propTypes = {
  26. type: PropTypes.string,
  27. prefixCls: PropTypes.string,
  28. style: PropTypes.object,
  29. className: PropTypes.string,
  30. size: PropTypes.oneOf(sizeSet),
  31. };
  32. static defaultProps = {
  33. prefixCls: cssClasses.PREFIX,
  34. size: 'medium'
  35. };
  36. render() {
  37. const { prefixCls, className, type, size, ...others } = this.props;
  38. const classString = cls(className, `${prefixCls}-${type}`, {
  39. [`${prefixCls}-${type}-${size}`]: type.toUpperCase() === 'AVATAR'
  40. });
  41. return React.createElement('div', { className: classString, ...others });
  42. }
  43. }
  44. export const Avatar = generator<AvatarProps>('avatar')(Generic);
  45. export const Image = generator<BasicProps>('image')(Generic);
  46. export const Title = generator<BasicProps>('title')(Generic);
  47. export const Button = generator<BasicProps>('button')(Generic);
  48. export class Paragraph extends PureComponent<ParagraphProps> {
  49. static propTypes = {
  50. rows: PropTypes.number,
  51. prefixCls: PropTypes.string,
  52. style: PropTypes.object,
  53. className: PropTypes.string,
  54. };
  55. static defaultProps = {
  56. prefixCls: cssClasses.PREFIX,
  57. rows: 4,
  58. };
  59. render(): ReactNode {
  60. const { prefixCls, className, style, rows } = this.props;
  61. const classString = cls(className, `${prefixCls}-paragraph`);
  62. return (
  63. <ul className={classString} style={style}>
  64. {[...Array(rows)].map((e, i) => <li key={i} />)}
  65. </ul>
  66. );
  67. }
  68. }