index.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import React, {useState} from 'react'
  2. import ReactDOM from 'react-dom'
  3. import styles from './style.module.css'
  4. interface Props {
  5. node: HTMLDivElement,
  6. children: any,
  7. stateChange?: Function,
  8. show?: boolean,
  9. title?: string
  10. }
  11. function Dialog({node, children, stateChange, show = false, title}: Props) {
  12. const [ishow, setIShow] = useState(false)
  13. const showChange = (show: boolean) => {
  14. setIShow(show)
  15. stateChange && stateChange(show)
  16. }
  17. if (show !== ishow) {
  18. setIShow(show)
  19. return null
  20. }
  21. const Layer = ishow ? (
  22. <div className={styles.layer}>
  23. <div className={styles.content}>
  24. <span className="icon-cancel" onClick={() => showChange(false)}></span>
  25. {title && <div className={styles.title}>{title}</div>}
  26. <div className={styles.content}>
  27. {children}
  28. </div>
  29. <div className={styles.bar}>
  30. <span onClick={() => showChange(false)} >取消</span>
  31. <span onClick={() => showChange(true)}>确定</span>
  32. </div>
  33. </div>
  34. </div>
  35. ) : null
  36. return ReactDOM.createPortal(Layer, node)
  37. }
  38. const node = document.querySelector('#dialog')
  39. const GDialog = (props: any) => <Dialog node={node} {...props} />
  40. export default GDialog