Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. /**
  2. * @desc antd Input 组件封装
  3. * @auth yanxi@douyu.tv
  4. * @date 2019/5/21
  5. */
  6. import React, { Component } from 'react';
  7. import AntdInput from 'antd/lib/input';
  8. // import Form from 'antd/lib/form';
  9. import {
  10. string, object, bool, func,
  11. } from 'prop-types';
  12. import FormItem from '../Form/FormItem';
  13.  
  14. // const FormItem = Form.Item;
  15. const defaultId = `${Math.random()}`;
  16.  
  17. class Input extends Component {
  18. static propTypes = {
  19. label: string,
  20. id: string,
  21. labelCol: object,
  22. wrapperCol: object,
  23. visibility: bool,
  24. getValue: func,
  25. };
  26.  
  27. static defaultProps = {
  28. label: '默认label',
  29. id: defaultId,
  30. labelCol: {},
  31. wrapperCol: {},
  32. visibility: true,
  33. getValue: () => {},
  34. };
  35.  
  36. constructor(props) {
  37. super(props);
  38. this.firstIn = 1;
  39. this.value = props.value;
  40. }
  41.  
  42. componentWillReceiveProps(nextProps) {
  43. const { id, value, form: { setFieldsValue } } = nextProps;
  44. const update = this.updateValue(value);
  45.  
  46. this.firstIn = 0;
  47. if (update) {
  48. setFieldsValue({
  49. [id]: value,
  50. });
  51. }
  52. }
  53.  
  54. checkValue = () => {
  55. const { id, form: { getFieldValue } } = this.props;
  56. const newValue = getFieldValue(id || defaultId);
  57. const update = this.updateValue(newValue);
  58.  
  59. if (update) {
  60. this.onChange({
  61. target: {
  62. value: newValue,
  63. },
  64. });
  65. }
  66. }
  67.  
  68. updateValue = (newValue) => {
  69. let update = 0;
  70.  
  71. if (newValue !== this.value) {
  72. this.value = newValue;
  73. update = 1;
  74. }
  75.  
  76. return update;
  77. }
  78.  
  79. onChange = (e) => {
  80. const { onChange } = this.props;
  81.  
  82. if (onChange) {
  83. onChange(e.target.value, e);
  84. }
  85. }
  86.  
  87. render() {
  88. const {
  89. label, id, labelCol, wrapperCol, getValue,
  90. rules = {}, visibility, batchStyle, formStyle,
  91. form: { getFieldDecorator },
  92. remarks, $form, propsConfig, permissions, $formPermissions,
  93. ...others
  94. } = this.props;
  95. const targetRules = [];
  96. const formLayout = {
  97. labelCol,
  98. wrapperCol,
  99. };
  100.  
  101. if (!this.firstIn) {
  102. this.checkValue();
  103. }
  104.  
  105. if (!label) {
  106. formLayout.labelCol.span = 0;
  107. formLayout.wrapperCol.span = 24;
  108. }
  109.  
  110. if (rules.required) {
  111. targetRules.push({
  112. required: true, message: '此项必填',
  113. });
  114. }
  115.  
  116. if (rules.max) {
  117. targetRules.push({
  118. max: rules.max, message: rules.maxMsg,
  119. });
  120. }
  121.  
  122. if (rules.pattern) {
  123. targetRules.push({
  124. pattern: new RegExp(rules.pattern), message: rules.message,
  125. });
  126. }
  127.  
  128. if (!visibility) {
  129. return null;
  130. }
  131.  
  132. delete others.value;
  133.  
  134. return (
  135. <FormItem
  136. {...formLayout}
  137. label={label}
  138. style={Object.assign({}, batchStyle || {}, formStyle || {})}
  139. remarks={remarks}
  140. >
  141. {getFieldDecorator(id || defaultId, {
  142. initialValue: others.defaultValue,
  143. rules: targetRules,
  144. })(
  145. <AntdInput
  146. {...others}
  147. onPressEnter={() => {
  148. if (getValue) {
  149. getValue();
  150. }
  151. }}
  152. onChange={this.onChange}
  153. />,
  154. )}
  155. </FormItem>
  156. );
  157. }
  158. }
  159.  
  160. export default Input;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement