index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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) => Promise<any> | void;
  38. onConfirm?: (e: React.MouseEvent) => Promise<any> | void;
  39. onVisibleChange?: (visible: boolean) => void;
  40. onClickOutSide?: (e: React.MouseEvent) => void;
  41. }
  42. export interface PopconfirmState {
  43. visible: boolean;
  44. cancelLoading: boolean;
  45. confirmLoading: boolean;
  46. }
  47. interface PopProps {
  48. [x: string]: any;
  49. }
  50. export default class Popconfirm extends BaseComponent<PopconfirmProps, PopconfirmState> {
  51. static contextType = ConfigContext;
  52. static propTypes = {
  53. motion: PropTypes.oneOfType([PropTypes.bool, PropTypes.func, PropTypes.object]),
  54. disabled: PropTypes.bool,
  55. content: PropTypes.any,
  56. title: PropTypes.any,
  57. prefixCls: PropTypes.string,
  58. className: PropTypes.string,
  59. style: PropTypes.object,
  60. icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  61. okText: PropTypes.string,
  62. okType: PropTypes.string,
  63. cancelText: PropTypes.string,
  64. cancelType: PropTypes.string,
  65. onCancel: PropTypes.func,
  66. onConfirm: PropTypes.func,
  67. onClickOutSide: PropTypes.func,
  68. onVisibleChange: PropTypes.func,
  69. visible: PropTypes.bool,
  70. defaultVisible: PropTypes.bool,
  71. okButtonProps: PropTypes.object,
  72. cancelButtonProps: PropTypes.object,
  73. stopPropagation: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
  74. zIndex: PropTypes.number,
  75. // private
  76. trigger: PropTypes.string,
  77. position: PropTypes.string,
  78. };
  79. static defaultProps = {
  80. stopPropagation: true,
  81. trigger: 'click',
  82. // position: 'bottomLeft',
  83. onVisibleChange: noop,
  84. disabled: false,
  85. icon: <IconAlertTriangle size="extra-large" />,
  86. okType: 'primary',
  87. cancelType: 'tertiary',
  88. prefixCls: cssClasses.PREFIX,
  89. zIndex: numbers.DEFAULT_Z_INDEX,
  90. onCancel: noop,
  91. onConfirm: noop,
  92. onClickOutSide: noop,
  93. };
  94. constructor(props: PopconfirmProps) {
  95. super(props);
  96. this.state = {
  97. cancelLoading: false,
  98. confirmLoading: false,
  99. visible: props.defaultVisible || false,
  100. };
  101. this.foundation = new PopconfirmFoundation(this.adapter);
  102. }
  103. context: ContextValue;
  104. static getDerivedStateFromProps(props: PopconfirmProps, state: PopconfirmState) {
  105. const willUpdateStates: Partial<PopconfirmState> = {};
  106. const { hasOwnProperty } = Object.prototype;
  107. if (hasOwnProperty.call(props, 'visible')) {
  108. willUpdateStates.visible = props.visible;
  109. }
  110. return willUpdateStates;
  111. }
  112. get adapter(): PopconfirmAdapter<PopconfirmProps, PopconfirmState> {
  113. return {
  114. ...super.adapter,
  115. setVisible: (visible: boolean): void => this.setState({ visible }),
  116. updateConfirmLoading: (loading: boolean): void => this.setState({ confirmLoading: loading }),
  117. updateCancelLoading: (loading: boolean): void => this.setState({ cancelLoading: loading }),
  118. notifyConfirm: (e: React.MouseEvent): Promise<any> | void => this.props.onConfirm(e),
  119. notifyCancel: (e: React.MouseEvent): Promise<any> | void => this.props.onCancel(e),
  120. notifyVisibleChange: (visible: boolean): void => this.props.onVisibleChange(visible),
  121. notifyClickOutSide: (e: React.MouseEvent) => this.props.onClickOutSide(e),
  122. };
  123. }
  124. handleCancel = (e: React.MouseEvent): void => this.foundation.handleCancel(e && e.nativeEvent);
  125. handleConfirm = (e: React.MouseEvent): void => this.foundation.handleConfirm(e && e.nativeEvent);
  126. handleVisibleChange = (visible: boolean): void => this.foundation.handleVisibleChange(visible);
  127. handleClickOutSide = (e: React.MouseEvent) => this.foundation.handleClickOutSide(e);
  128. stopImmediatePropagation = (e: React.SyntheticEvent): void => e && e.nativeEvent && e.nativeEvent.stopImmediatePropagation();
  129. renderControls() {
  130. const { okText, cancelText, okType, cancelType, cancelButtonProps, okButtonProps } = this.props;
  131. const { cancelLoading, confirmLoading } = this.state;
  132. return (
  133. <LocaleConsumer componentName="Popconfirm">
  134. {(locale: LocaleObject['Popconfirm'], localeCode: string) => (
  135. <>
  136. <Button type={cancelType} onClick={this.handleCancel} loading={cancelLoading} {...cancelButtonProps}>
  137. {cancelText || get(locale, 'cancel')}
  138. </Button>
  139. <Button type={okType} theme="solid" onClick={this.handleConfirm} loading={confirmLoading} {...okButtonProps}>
  140. {okText || get(locale, 'confirm')}
  141. </Button>
  142. </>
  143. )}
  144. </LocaleConsumer>
  145. );
  146. }
  147. renderConfirmPopCard() {
  148. const { content, title, className, style, cancelType, icon, prefixCls } = this.props;
  149. const { direction } = this.context;
  150. const popCardCls = cls(
  151. prefixCls,
  152. className,
  153. {
  154. [`${prefixCls}-rtl`]: direction === 'rtl',
  155. }
  156. );
  157. const showTitle = title !== null && typeof title !== 'undefined';
  158. const showContent = !(content === null || typeof content === 'undefined');
  159. return (
  160. // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
  161. <div className={popCardCls} onClick={this.stopImmediatePropagation} style={style}>
  162. <div className={`${prefixCls}-inner`}>
  163. <div className={`${prefixCls}-header`}>
  164. <i className={`${prefixCls}-header-icon`} x-semi-prop="icon">
  165. {React.isValidElement(icon) ? icon : null}
  166. </i>
  167. <div className={`${prefixCls}-header-body`}>
  168. {showTitle ? (
  169. <div className={`${prefixCls}-header-title`} x-semi-prop="title">
  170. {title}
  171. </div>
  172. ) : null}
  173. </div>
  174. <Button
  175. className={`${prefixCls}-btn-close`}
  176. icon={<IconClose />}
  177. size="small"
  178. theme={'borderless'}
  179. type={cancelType}
  180. onClick={this.handleCancel}
  181. />
  182. </div>
  183. {showContent ? (
  184. <div className={`${prefixCls}-body`} x-semi-prop="content">
  185. {content}
  186. </div>
  187. ) : null}
  188. <div className={`${prefixCls}-footer`}>{this.renderControls()}</div>
  189. </div>
  190. </div>
  191. );
  192. }
  193. render() {
  194. // rtl changes the default position
  195. const { direction } = this.context;
  196. const defaultPosition = direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
  197. const {
  198. className,
  199. prefixCls,
  200. disabled,
  201. children,
  202. style,
  203. position = defaultPosition,
  204. ...attrs
  205. } = this.props;
  206. if (disabled) {
  207. return children;
  208. }
  209. const { visible } = this.state;
  210. const popContent = this.renderConfirmPopCard();
  211. const popProps: PopProps = {
  212. onVisibleChange: this.handleVisibleChange,
  213. className: cssClasses.POPOVER,
  214. onClickOutSide: this.handleClickOutSide,
  215. };
  216. if (this.isControlled('visible')) {
  217. popProps.trigger = 'custom';
  218. }
  219. return (
  220. <Popover
  221. {...attrs}
  222. content={popContent}
  223. visible={visible}
  224. position={position}
  225. {...popProps}
  226. >
  227. {children}
  228. </Popover>
  229. );
  230. }
  231. }