SearchForm.jsx 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React, { PureComponent } from 'react'
  2. import PropTypes from 'prop-types'
  3. import createForm from '@zjxpcyc/xrc-form2'
  4. import { Button } from 'antd'
  5. const XForm = createForm()
  6. function clearProperties (o) {
  7. return Object.keys(o).reduce((acc, k) => {
  8. return {
  9. ...acc,
  10. [`${k}`]: undefined,
  11. }
  12. }, {})
  13. }
  14. export default class SearchForm extends PureComponent {
  15. static propTypes = {
  16. storage: PropTypes.object,
  17. onSubmit: PropTypes.func,
  18. onCancel: PropTypes.func,
  19. configs: PropTypes.object,
  20. }
  21. wrapperForm = null
  22. storage = this.props.storage || {}
  23. componentDidMount() {
  24. const conditions = this.storage.get('conditions')
  25. if (this.wrapperForm && conditions) {
  26. this.wrapperForm.props.form.setFieldsValue(conditions)
  27. }
  28. }
  29. handleCancel = () => {
  30. const conditions = this.storage.get('conditions') || {}
  31. this.storage.set('conditions', {})
  32. if (this.wrapperForm) {
  33. this.wrapperForm.props.form.setFieldsValue(clearProperties(conditions))
  34. }
  35. if (this.props.onCancel) {
  36. this.props.onCancel()
  37. }
  38. }
  39. wrapperFields = (fields) => {
  40. return [
  41. ...fields,
  42. {
  43. action: true,
  44. render: (props) => (<Button htmlType="submit" type="primary">搜索</Button>)
  45. },
  46. {
  47. action: true,
  48. render: (props) => (<Button htmlType="button" onClick={this.handleCancel} style={{ marginLeft: '16px' }}>重置</Button>)
  49. },
  50. ]
  51. }
  52. render() {
  53. const { fields, ...leftProps } = this.props.configs || {}
  54. return (
  55. <XForm
  56. submitBtn={false}
  57. cancelBtn={false}
  58. layout="inline"
  59. fields={this.wrapperFields(fields)}
  60. onSubmit={this.props.onSubmit}
  61. wrappedComponentRef={f => this.wrapperForm = f}
  62. {...leftProps}
  63. />
  64. )
  65. }
  66. }