ModelAuthModal.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React from 'react'
  2. import { Modal, Button, List, Checkbox } from 'antd'
  3. const ListItem = List.Item
  4. import { CheckboxChangeEvent } from 'antd/lib/checkbox'
  5. import { IViewModel } from '../types'
  6. interface IModelAuthModalProps {
  7. visible: boolean
  8. model: IViewModel
  9. roleId: number
  10. auth: string[]
  11. onSave: (auth: string[]) => void
  12. onCancel: () => void
  13. }
  14. interface IModelAuthModalStates {
  15. localRoleId: number
  16. localAuth: string[]
  17. }
  18. export class ModelAuthModal extends React.PureComponent<IModelAuthModalProps, IModelAuthModalStates> {
  19. public state: Readonly<IModelAuthModalStates> = {
  20. localRoleId: 0,
  21. localAuth: []
  22. }
  23. public static getDerivedStateFromProps:
  24. React.GetDerivedStateFromProps<IModelAuthModalProps, IModelAuthModalStates>
  25. = (props, state) => {
  26. const { roleId, auth } = props
  27. const { localRoleId } = state
  28. if (roleId !== localRoleId) {
  29. return {
  30. localRoleId: roleId,
  31. localAuth: [...auth] }
  32. }
  33. return null
  34. }
  35. private save = () => {
  36. this.props.onSave([...this.state.localAuth])
  37. }
  38. private modalFooter = [(
  39. <Button key="cancel" size="large" onClick={this.props.onCancel}>取 消</Button>
  40. ), (
  41. <Button key="save" size="large" type="primary" onClick={this.save}>保 存</Button>
  42. )]
  43. private toggleCheckAll = (e: CheckboxChangeEvent) => {
  44. const localAuth = e.target.checked ? [] : Object.keys(this.props.model)
  45. this.setState({ localAuth })
  46. }
  47. private toggleCheck = (name: string) => (e: CheckboxChangeEvent) => {
  48. const checked = e.target.checked
  49. this.setState(({ localAuth }) => {
  50. if (checked) {
  51. return { localAuth: localAuth.filter((item) => item !== name) }
  52. }
  53. return { localAuth: [...localAuth, name] }
  54. })
  55. }
  56. private renderListHeader (props: IModelAuthModalProps) {
  57. const { model } = props
  58. const { localAuth } = this.state
  59. const indeterminate = (localAuth.length > 0 && localAuth.length !== Object.keys(model).length)
  60. const checkAll = (localAuth.length === 0)
  61. return (
  62. <Checkbox
  63. indeterminate={indeterminate}
  64. onChange={this.toggleCheckAll}
  65. checked={checkAll}
  66. >
  67. 全选
  68. </Checkbox>
  69. )
  70. }
  71. private renderItem = (name: string) => {
  72. const { localAuth } = this.state
  73. const checked = !localAuth.includes(name)
  74. return (
  75. <ListItem>
  76. <Checkbox
  77. onChange={this.toggleCheck(name)}
  78. checked={checked}
  79. >
  80. {name}
  81. </Checkbox>
  82. </ListItem>
  83. )
  84. }
  85. public render () {
  86. const { visible, model, onCancel } = this.props
  87. const listDataSource = Object.keys(model)
  88. return (
  89. <Modal
  90. title="勾选可见字段"
  91. visible={visible}
  92. footer={this.modalFooter}
  93. onCancel={onCancel}
  94. >
  95. <List
  96. bordered
  97. header={this.renderListHeader(this.props)}
  98. dataSource={listDataSource}
  99. renderItem={this.renderItem}
  100. />
  101. </Modal>
  102. )
  103. }
  104. }
  105. export default ModelAuthModal