index.tsx 11 KB

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