index.tsx 9.5 KB

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