import React from 'react' import { Modal, Button, List, Checkbox } from 'antd' const ListItem = List.Item import { CheckboxChangeEvent } from 'antd/lib/checkbox' import { IViewModel } from '../types' interface IModelAuthModalProps { visible: boolean model: IViewModel roleId: number auth: string[] onSave: (auth: string[]) => void onCancel: () => void } interface IModelAuthModalStates { localRoleId: number localAuth: string[] } export class ModelAuthModal extends React.PureComponent { public state: Readonly = { localRoleId: 0, localAuth: [] } public static getDerivedStateFromProps: React.GetDerivedStateFromProps = (props, state) => { const { roleId, auth } = props const { localRoleId } = state if (roleId !== localRoleId) { return { localRoleId: roleId, localAuth: [...auth] } } return null } private save = () => { this.props.onSave([...this.state.localAuth]) } private modalFooter = [( ), ( )] private toggleCheckAll = (e: CheckboxChangeEvent) => { const localAuth = e.target.checked ? [] : Object.keys(this.props.model) this.setState({ localAuth }) } private toggleCheck = (name: string) => (e: CheckboxChangeEvent) => { const checked = e.target.checked this.setState(({ localAuth }) => { if (checked) { return { localAuth: localAuth.filter((item) => item !== name) } } return { localAuth: [...localAuth, name] } }) } private renderListHeader (props: IModelAuthModalProps) { const { model } = props const { localAuth } = this.state const indeterminate = (localAuth.length > 0 && localAuth.length !== Object.keys(model).length) const checkAll = (localAuth.length === 0) return ( 全选 ) } private renderItem = (name: string) => { const { localAuth } = this.state const checked = !localAuth.includes(name) return ( {name} ) } public render () { const { visible, model, onCancel } = this.props const listDataSource = Object.keys(model) return ( ) } } export default ModelAuthModal