index.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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`} x-semi-prop="icon">
  157. {React.isValidElement(icon) ? icon : null}
  158. </i>
  159. <div className={`${prefixCls}-header-body`}>
  160. {showTitle ? (
  161. <div className={`${prefixCls}-header-title`} x-semi-prop="title">
  162. {title}
  163. </div>
  164. ) : null}
  165. {showContent ? (
  166. <div className={`${prefixCls}-header-content`} x-semi-prop="content">
  167. {content}
  168. </div>
  169. ) : null}
  170. </div>
  171. <Button
  172. className={`${prefixCls}-btn-close`}
  173. icon={<IconClose />}
  174. size="small"
  175. theme={'borderless'}
  176. type={cancelType}
  177. onClick={this.handleCancel}
  178. />
  179. </div>
  180. <div className={`${prefixCls}-footer`}>{this.renderControls()}</div>
  181. </div>
  182. </div>
  183. );
  184. }
  185. render() {
  186. // rtl changes the default position
  187. const { direction } = this.context;
  188. const defaultPosition = direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
  189. const {
  190. className,
  191. prefixCls,
  192. disabled,
  193. children,
  194. style,
  195. position = defaultPosition,
  196. ...attrs
  197. } = this.props;
  198. if (disabled) {
  199. return children;
  200. }
  201. const { visible } = this.state;
  202. const popContent = this.renderConfirmPopCard();
  203. const popProps: PopProps = {
  204. onVisibleChange: this.handleVisibleChange,
  205. className: cssClasses.POPOVER,
  206. onClickOutSide: this.handleClickOutSide,
  207. };
  208. if (this.isControlled('visible')) {
  209. popProps.trigger = 'custom';
  210. }
  211. return (
  212. <Popover
  213. {...attrs}
  214. content={popContent}
  215. visible={visible}
  216. position={position}
  217. {...popProps}
  218. >
  219. {children}
  220. </Popover>
  221. );
  222. }
  223. }