TableList.js 9.75 KB
Newer Older
1 2
import React, { PureComponent } from 'react';
import { connect } from 'dva';
afc163's avatar
afc163 committed
3
import { Row, Col, Card, Form, Input, Select, Icon, Button, Dropdown, Menu, InputNumber, DatePicker, Modal, message } from 'antd';
4 5 6 7 8 9
import StandardTable from '../../components/StandardTable';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';

import styles from './TableList.less';

const FormItem = Form.Item;
afc163's avatar
afc163 committed
10
const { Option } = Select;
11 12
const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');

13 14 15 16 17 18 19 20 21 22 23 24 25
const CreateForm = Form.create()((props) => {
  const { modalVisible, addInputValue, parent, form } = props;
  const okHandle = () => {
    form.validateFields((err/* , fieldsValue */) => {
      if (err) return;
      parent.handleAdd();
    });
  };
  return (
    <Modal
      title="新建规则"
      visible={modalVisible}
      onOk={okHandle}
jim chen's avatar
jim chen committed
26
      destroyOnClose
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
      onCancel={() => parent.handleModalVisible()}
    >
      <FormItem
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 15 }}
        label="描述"
      >
        {form.getFieldDecorator('desc', {
          rules: [{ required: true, message: 'Please input some description...' }],
        })(
          <Input placeholder="请输入" onChange={parent.handleAddInput} value={addInputValue} />
        )}
      </FormItem>
    </Modal>
  );
});

Andreas Cederström's avatar
Andreas Cederström committed
44 45 46
@connect(({ rule, loading }) => ({
  rule,
  loading: loading.models.rule,
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}))
@Form.create()
export default class TableList extends PureComponent {
  state = {
    addInputValue: '',
    modalVisible: false,
    expandForm: false,
    selectedRows: [],
    formValues: {},
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'rule/fetch',
    });
  }

  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;

    const filters = Object.keys(filtersArg).reduce((obj, key) => {
      const newObj = { ...obj };
      newObj[key] = getValue(filtersArg[key]);
      return newObj;
    }, {});

    const params = {
      currentPage: pagination.current,
      pageSize: pagination.pageSize,
      ...formValues,
      ...filters,
    };
    if (sorter.field) {
      params.sorter = `${sorter.field}_${sorter.order}`;
    }

    dispatch({
      type: 'rule/fetch',
      payload: params,
    });
  }

  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
94 95 96
    this.setState({
      formValues: {},
    });
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    dispatch({
      type: 'rule/fetch',
      payload: {},
    });
  }

  toggleForm = () => {
    this.setState({
      expandForm: !this.state.expandForm,
    });
  }

  handleMenuClick = (e) => {
    const { dispatch } = this.props;
    const { selectedRows } = this.state;

    if (!selectedRows) return;

    switch (e.key) {
      case 'remove':
        dispatch({
          type: 'rule/remove',
          payload: {
            no: selectedRows.map(row => row.no).join(','),
          },
          callback: () => {
            this.setState({
              selectedRows: [],
            });
          },
        });
        break;
      default:
        break;
    }
  }

  handleSelectRows = (rows) => {
    this.setState({
      selectedRows: rows,
    });
  }

  handleSearch = (e) => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
        updatedAt: fieldsValue.updatedAt && fieldsValue.updatedAt.valueOf(),
      };

      this.setState({
        formValues: values,
      });

      dispatch({
        type: 'rule/fetch',
        payload: values,
      });
    });
  }

  handleModalVisible = (flag) => {
    this.setState({
      modalVisible: !!flag,
    });
  }

  handleAddInput = (e) => {
    this.setState({
      addInputValue: e.target.value,
    });
  }

  handleAdd = () => {
    this.props.dispatch({
      type: 'rule/add',
      payload: {
        description: this.state.addInputValue,
      },
    });

    message.success('添加成功');
    this.setState({
      modalVisible: false,
    });
  }

afc163's avatar
afc163 committed
190 191 192 193
  renderSimpleForm() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
194 195
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
196 197 198 199 200 201
            <FormItem label="规则编号">
              {getFieldDecorator('no')(
                <Input placeholder="请输入" />
              )}
            </FormItem>
          </Col>
202
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
203 204 205 206 207 208 209 210 211
            <FormItem label="使用状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
212
          <Col md={8} sm={24}>
FOCUS's avatar
FOCUS committed
213
            <span className={styles.submitButtons}>
afc163's avatar
afc163 committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
              <Button type="primary" htmlType="submit">查询</Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>重置</Button>
              <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
                展开 <Icon type="down" />
              </a>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }

  renderAdvancedForm() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
afc163's avatar
afc163 committed
230
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
231
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
232 233 234 235 236 237
            <FormItem label="规则编号">
              {getFieldDecorator('no')(
                <Input placeholder="请输入" />
              )}
            </FormItem>
          </Col>
238
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
239 240 241 242 243 244 245 246 247
            <FormItem label="使用状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
248
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
249 250 251 252 253 254 255
            <FormItem label="调用次数">
              {getFieldDecorator('number')(
                <InputNumber style={{ width: '100%' }} />
              )}
            </FormItem>
          </Col>
        </Row>
256 257
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
258 259 260 261 262 263
            <FormItem label="更新日期">
              {getFieldDecorator('date')(
                <DatePicker style={{ width: '100%' }} placeholder="请输入更新日期" />
              )}
            </FormItem>
          </Col>
264
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
265 266 267 268 269 270 271 272 273
            <FormItem label="使用状态">
              {getFieldDecorator('status3')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
274
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
275 276 277 278 279 280 281 282 283 284
            <FormItem label="使用状态">
              {getFieldDecorator('status4')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
        </Row>
afc163's avatar
afc163 committed
285
        <div style={{ overflow: 'hidden' }}>
286
          <span style={{ float: 'right', marginBottom: 24 }}>
afc163's avatar
afc163 committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
            <Button type="primary" htmlType="submit">查询</Button>
            <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>重置</Button>
            <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
              收起 <Icon type="up" />
            </a>
          </span>
        </div>
      </Form>
    );
  }

  renderForm() {
    return this.state.expandForm ? this.renderAdvancedForm() : this.renderSimpleForm();
  }

302
  render() {
Andreas Cederström's avatar
Andreas Cederström committed
303
    const { rule: { data }, loading } = this.props;
304 305 306 307 308
    const { selectedRows, modalVisible, addInputValue } = this.state;

    const menu = (
      <Menu onClick={this.handleMenuClick} selectedKeys={[]}>
        <Menu.Item key="remove">删除</Menu.Item>
309
        <Menu.Item key="approval">批量审批</Menu.Item>
310 311 312
      </Menu>
    );

313 314 315 316 317 318
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
      handleAddInput: this.handleAddInput,
    };

319
    return (
niko's avatar
niko committed
320
      <PageHeaderLayout title="查询表格">
321
        <Card bordered={false}>
322 323
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>
afc163's avatar
afc163 committed
324
              {this.renderForm()}
325 326
            </div>
            <div className={styles.tableListOperator}>
afc163's avatar
afc163 committed
327 328 329
              <Button icon="plus" type="primary" onClick={() => this.handleModalVisible(true)}>
                新建
              </Button>
330
              {
afc163's avatar
afc163 committed
331 332 333 334 335 336 337 338 339 340
                selectedRows.length > 0 && (
                  <span>
                    <Button>批量操作</Button>
                    <Dropdown overlay={menu}>
                      <Button>
                        更多操作 <Icon type="down" />
                      </Button>
                    </Dropdown>
                  </span>
                )
341
              }
342 343 344
            </div>
            <StandardTable
              selectedRows={selectedRows}
Andreas Cederström's avatar
Andreas Cederström committed
345
              loading={loading}
346 347 348 349 350 351
              data={data}
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
352 353 354 355 356
        <CreateForm
          parent={parentMethods}
          modalVisible={modalVisible}
          addInputValue={addInputValue}
        />
357 358 359 360
      </PageHeaderLayout>
    );
  }
}