DictionaryList.js 6.5 KB
Newer Older
1 2 3 4 5
/* eslint-disable */

import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
6
import { Card, Form, Input, InputNumber, Select, Button, Modal, message, Table, Divider } from 'antd';
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import PageHeaderWrapper from '@/components/PageHeaderWrapper';

import styles from './DictionaryList.less';

const FormItem = Form.Item;
const { Option } = Select;
const types = ['未知', '菜单', '链接'];

// 添加 form 表单
const CreateForm = Form.create()(props => {
  const { modalVisible, form, handleAdd, handleModalVisible, modalType, initValues } = props;

  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      form.resetFields();
      handleAdd({
        fields: fieldsValue,
        modalType,
        initValues,
      });
    });
  };

  const selectStyle = {
    width: 200,
  };

35 36
  const title = modalType === 'add' ? '新建数据字典' : '编辑数据字典';
  const okText = '保存';
37 38 39 40 41 42 43 44 45
  return (
    <Modal
      destroyOnClose
      title={title}
      visible={modalVisible}
      onOk={okHandle}
      okText={okText}
      onCancel={() => handleModalVisible()}
    >
46 47 48 49
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="大类枚举值">
        {form.getFieldDecorator('enumValue', {
          rules: [{ required: true, message: '请输入大类枚举值!', min: 2 }],
          initialValue: initValues.enumValue,
50 51
        })(<Input placeholder="请输入" />)}
      </FormItem>
52 53 54 55
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="小类数值">
        {form.getFieldDecorator('value', {
          rules: [{ required: true, message: '请输入小类数值!' }],
          initialValue: initValues.value,
56 57
        })(<Input placeholder="请输入" />)}
      </FormItem>
58 59 60 61
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="展示名">
        {form.getFieldDecorator('displayName', {
          rules: [{ required: true, message: '请输入展示名!' }],
          initialValue: initValues.displayName,
62 63
        })(<Input placeholder="请输入" />)}
      </FormItem>
64
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="排序值">
65
        {form.getFieldDecorator('sort', {
66
          rules: [{ required: true, message: '请输入排序值!' }],
67
          initialValue: initValues.sort,
68
        })(<InputNumber placeholder="请输入" />)}
69
      </FormItem>
70 71 72 73 74
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="备注">
        {form.getFieldDecorator('memo', {
          rules: [{ required: true, message: '请输入备注!' }],
          initialValue: initValues.memo,
         })(<Input.TextArea placeholder="请输入" />)}
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      </FormItem>
    </Modal>
  );
});

@connect(({ dictionaryList, loading }) => ({
  dictionaryList,
  list: dictionaryList.list,
  loading: loading.models.dictionaryList,
}))

@Form.create()
class DictionaryList extends PureComponent {
  state = {
    modalVisible: false,
    modalType: 'add', //add update
    initValues: {},
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'dictionaryList/tree',
      payload: {},
    });
  }

  handleModalVisible = (flag, modalType, initValues) => {
    this.setState({
      modalVisible: !!flag,
      initValues: initValues || {},
      modalType: modalType || 'add',
    });
  };

  handleAdd = ({ fields, modalType, initValues }) => {
    const { dispatch } = this.props;
    if (modalType === 'add') {
      dispatch({
        type: 'dictionaryList/add',
        payload: {
          body: {
            ...fields,
          },
          callback: () => {
            message.success('添加成功');
            this.handleModalVisible();
          },
        },
      });
    } else {
      dispatch({
        type: 'dictionaryList/update',
        payload: {
          body: {
            ...initValues,
            ...fields,
          },
          callback: () => {
            message.success('更新成功');
            this.handleModalVisible();
          },
        },
      });
    }
  };

  handleDelete(row) {
    const { dispatch } = this.props;
    Modal.confirm({
      title: `确认删除?`,
      content: `${row.displayName}`,
      onOk() {
        dispatch({
          type: 'dictionaryList/delete',
          payload: {
            id: row.id,
          },
        });
      },
      onCancel() {},
    });
  }

  render() {
    const { list } = this.props;
    const { modalVisible, modalType, initValues } = this.state;
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
      modalType,
      initValues,
    };
168
    let that = this;
169 170 171

    const columns = [
      {
172
        title: '大类枚举值',
173 174
        dataIndex: 'enumValue'
      },
175 176 177 178
      // {
      //   title: '编号',
      //   dataIndex: 'id',
      // },
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
      {
        title: '小类数值',
        dataIndex: 'value'
      },
      {
        title: '展示名',
        dataIndex: 'displayName'
      },
      {
        title: '排序值',
        dataIndex: 'sort'
      },
      {
        title: '备注',
        dataIndex: 'memo'
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        sorter: true,
        render: val => <span>{moment(val).format('YYYY-MM-DD')}</span>,
      },
      {
        title: '操作',
203 204 205 206 207 208 209 210 211 212 213 214
        render: function(text, record) {
          if (!record.id) {
              return '';
          }
          return <Fragment>
                <a onClick={() => that.handleModalVisible(true, 'update', record)}>编辑</a>
                <Divider type="vertical" />
                <a className={styles.tableDelete} onClick={() => that.handleDelete(record)}>
                    删除
                </a>
            </Fragment>
        }
215 216 217 218 219 220 221 222 223 224 225 226 227
      },
    ];

    return (
      <PageHeaderWrapper>
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListOperator}>
              <Button
                icon="plus"
                type="primary"
                onClick={() => this.handleModalVisible(true, 'add', {})}
              >
228
                新建数据字典
229 230 231
              </Button>
            </div>
          </div>
232
          <Table defaultExpandAllRows={true} columns={columns} dataSource={list} rowKey="index" />
233 234 235 236 237 238 239 240
        </Card>
        <CreateForm {...parentMethods} modalVisible={modalVisible} />
      </PageHeaderWrapper>
    );
  }
}

export default DictionaryList;