1
0

index.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* eslint-disable max-len */
  2. import React from 'react';
  3. import cls from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import { noop, get } from 'lodash';
  6. import { cssClasses, numbers } from '@douyinfe/semi-foundation/popconfirm/constants';
  7. import PopconfirmFoundation, { PopconfirmAdapter } from '@douyinfe/semi-foundation/popconfirm/popconfirmFoundation';
  8. import { IconClose, IconAlertTriangle } from '@douyinfe/semi-icons';
  9. import BaseComponent from '../_base/baseComponent';
  10. import Popover, { PopoverProps } from '../popover';
  11. import { Position, Trigger } from '../tooltip';
  12. import Button, { ButtonProps } from '../button';
  13. import { Type as ButtonType } from '../button/Button';
  14. import ConfigContext, { ContextValue } from '../configProvider/context';
  15. import LocaleConsumer from '../locale/localeConsumer';
  16. import { Locale as LocaleObject } from '../locale/interface';
  17. import '@douyinfe/semi-foundation/popconfirm/popconfirm.scss';
  18. import { Motion } from '../_base/base';
  19. export interface PopconfirmProps extends PopoverProps {
  20. cancelText?: string;
  21. cancelButtonProps?: ButtonProps;
  22. cancelType?: ButtonType;
  23. content?: React.ReactNode;
  24. defaultVisible?: boolean;
  25. disabled?: boolean;
  26. icon?: React.ReactNode;
  27. okText?: string;
  28. okType?: ButtonType;
  29. okButtonProps?: ButtonProps;
  30. motion?: Motion;
  31. title?: React.ReactNode;
  32. visible?: boolean;
  33. prefixCls?: string;
  34. zIndex?: number;
  35. trigger?: Trigger;
  36. position?: Position;
  37. onCancel?: (e: React.MouseEvent) => void;
  38. onConfirm?: (e: React.MouseEvent) => void;
  39. onVisibleChange?: (visible: boolean) => void;
  40. onClickOutSide?: (e: React.MouseEvent) => void;
  41. }
  42. export interface PopconfirmState {
  43. visible: boolean;
  44. }
  45. interface PopProps {
  46. [x: string]: any;
  47. }
  48. export default class Popconfirm extends BaseComponent<PopconfirmProps, PopconfirmState> {
  49. static contextType = ConfigContext;
  50. static propTypes = {
  51. motion: PropTypes.oneOfType([PropTypes.bool, PropTypes.func, PropTypes.object]),
  52. disabled: PropTypes.bool,
  53. content: PropTypes.any,
  54. title: PropTypes.any,
  55. prefixCls: PropTypes.string,
  56. className: PropTypes.string,
  57. style: PropTypes.object,
  58. icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  59. okText: PropTypes.string,
  60. okType: PropTypes.string,
  61. cancelText: PropTypes.string,
  62. cancelType: PropTypes.string,
  63. onCancel: PropTypes.func,
  64. onConfirm: PropTypes.func,
  65. onClickOutSide: PropTypes.func,
  66. onVisibleChange: PropTypes.func,
  67. visible: PropTypes.bool,
  68. defaultVisible: PropTypes.bool,
  69. okButtonProps: PropTypes.object,
  70. cancelButtonProps: PropTypes.object,
  71. stopPropagation: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
  72. zIndex: PropTypes.number,
  73. // private
  74. trigger: PropTypes.string,
  75. position: PropTypes.string,
  76. };
  77. static defaultProps = {
  78. stopPropagation: true,
  79. trigger: 'click',
  80. // position: 'bottomLeft',
  81. onVisibleChange: noop,
  82. disabled: false,
  83. icon: <IconAlertTriangle size="extra-large" />,
  84. okType: 'primary',
  85. cancelType: 'tertiary',
  86. prefixCls: cssClasses.PREFIX,
  87. zIndex: numbers.DEFAULT_Z_INDEX,
  88. onCancel: noop,
  89. onConfirm: noop,
  90. onClickOutSide: noop,
  91. };
  92. constructor(props: PopconfirmProps) {
  93. super(props);
  94. this.state = {
  95. visible: props.defaultVisible || false,
  96. };
  97. this.foundation = new PopconfirmFoundation(this.adapter);
  98. }
  99. context: ContextValue;
  100. static getDerivedStateFromProps(props: PopconfirmProps, state: PopconfirmState) {
  101. const willUpdateStates: Partial<PopconfirmState> = {};
  102. const { hasOwnProperty } = Object.prototype;
  103. if (hasOwnProperty.call(props, 'visible')) {
  104. willUpdateStates.visible = props.visible;
  105. }
  106. return willUpdateStates;
  107. }
  108. get adapter(): PopconfirmAdapter<PopconfirmProps, PopconfirmState> {
  109. return {
  110. ...super.adapter,
  111. setVisible: (visible: boolean): void => this.setState({ visible }),
  112. notifyConfirm: (e: React.MouseEvent): void => this.props.onConfirm(e),
  113. notifyCancel: (e: React.MouseEvent): void => this.props.onCancel(e),
  114. notifyVisibleChange: (visible: boolean): void => this.props.onVisibleChange(visible),
  115. notifyClickOutSide: (e: React.MouseEvent) => this.props.onClickOutSide(e),
  116. };
  117. }
  118. handleCancel = (e: React.MouseEvent): void => this.foundation.handleCancel(e && e.nativeEvent);
  119. handleConfirm = (e: React.MouseEvent): void => this.foundation.handleConfirm(e && e.nativeEvent);
  120. handleVisibleChange = (visible: boolean): void => this.foundation.handleVisibleChange(visible);
  121. handleClickOutSide = (e: React.MouseEvent) => this.foundation.handleClickOutSide(e);
  122. stopImmediatePropagation = (e: React.SyntheticEvent): void => e && e.nativeEvent && e.nativeEvent.stopImmediatePropagation();
  123. renderControls() {
  124. const { okText, cancelText, okType, cancelType, cancelButtonProps, okButtonProps } = this.props;
  125. return (
  126. <LocaleConsumer componentName="Popconfirm">
  127. {(locale: LocaleObject['Popconfirm'], localeCode: string) => (
  128. <>
  129. <Button type={cancelType} onClick={this.handleCancel} {...cancelButtonProps}>
  130. {cancelText || get(locale, 'cancel')}
  131. </Button>
  132. <Button type={okType} theme="solid" onClick={this.handleConfirm} {...okButtonProps}>
  133. {okText || get(locale, 'confirm')}
  134. </Button>
  135. </>
  136. )}
  137. </LocaleConsumer>
  138. );
  139. }
  140. renderConfirmPopCard() {
  141. const { content, title, className, style, cancelType, icon, prefixCls } = this.props;
  142. const { direction } = this.context;
  143. const popCardCls = cls(
  144. prefixCls,
  145. className,
  146. {
  147. [`${prefixCls}-rtl`]: direction === 'rtl',
  148. }
  149. );
  150. const showTitle = title !== null && typeof title !== 'undefined';
  151. const showContent = content !== null || typeof content !== 'undefined';
  152. return (
  153. <div className={popCardCls} onClick={this.stopImmediatePropagation} style={style}>
  154. <div className={`${prefixCls}-inner`}>
  155. <div className={`${prefixCls}-header`}>
  156. <i className={`${prefixCls}-header-icon`}>
  157. {React.isValidElement(icon) ? icon : null}
  158. </i>
  159. <div className={`${prefixCls}-header-body`}>
  160. {showTitle ? <div className={`${prefixCls}-header-title`}>{title}</div> : null}
  161. {showContent ? <div className={`${prefixCls}-header-content`}>{content}</div> : null}
  162. </div>
  163. <Button
  164. className={`${prefixCls}-btn-close`}
  165. icon={<IconClose />}
  166. size="small"
  167. theme={'borderless'}
  168. type={cancelType}
  169. onClick={this.handleCancel}
  170. />
  171. </div>
  172. <div className={`${prefixCls}-footer`}>{this.renderControls()}</div>
  173. </div>
  174. </div>
  175. );
  176. }
  177. render() {
  178. // rtl changes the default position
  179. const { direction } = this.context;
  180. const defaultPosition = direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
  181. const {
  182. className,
  183. prefixCls,
  184. disabled,
  185. children,
  186. style,
  187. position = defaultPosition,
  188. ...attrs
  189. } = this.props;
  190. if (disabled) {
  191. return children;
  192. }
  193. const { visible } = this.state;
  194. const popContent = this.renderConfirmPopCard();
  195. const popProps: PopProps = {
  196. onVisibleChange: this.handleVisibleChange,
  197. className: cssClasses.POPOVER,
  198. onClickOutSide: this.handleClickOutSide,
  199. };
  200. if (this.isControlled('visible')) {
  201. popProps.trigger = 'custom';
  202. }
  203. return (
  204. <Popover
  205. {...attrs}
  206. content={popContent}
  207. visible={visible}
  208. position={position}
  209. {...popProps}
  210. >
  211. {children}
  212. </Popover>
  213. );
  214. }
  215. }