Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. import React from 'react';
  2. import { Field, reduxForm } from 'redux-form';
  3. import { AutoComplete, FlatButton } from 'material-ui';
  4.  
  5. // Component
  6.  
  7. const mapError = (
  8. {
  9. meta: { touched, error, warning } = {},
  10. input,
  11. ...props
  12. },
  13. errorProp = 'errorText',
  14. ) =>
  15. (touched && (error || warning)
  16. ? {
  17. ...props,
  18. ...input,
  19. [errorProp]: error || warning,
  20. }
  21. : { ...input, ...props });
  22.  
  23. class SelectAutoComplete extends React.Component {
  24.  
  25. constructor(props) {
  26. super(props);
  27. const searchText = props.input.value;
  28. this.state = { searchText };
  29. }
  30.  
  31. onUpdateInput = (searchText) => {
  32. const { input } = this.props;
  33. const { onChange } = input;
  34. this.setState({ searchText }, () => {
  35. onChange(this.getSelectedIndex());
  36. });
  37. }
  38.  
  39. getSelectedIndex = () => {
  40. const { dataSource, dataSourceConfig, filter } = this.props;
  41. const searchText = this.state.searchText;
  42. const selectedIndex = dataSource.findIndex((item, index) => {
  43. switch (typeof item) {
  44. case 'string':
  45. return searchText === item;
  46. case 'object':
  47. if (item && typeof item[dataSourceConfig.text] === 'string') {
  48. const itemText = item[dataSourceConfig.text];
  49. return searchText === itemText;
  50. }
  51. break;
  52. default: // Do nothing
  53. }
  54. return false;
  55. });
  56. return selectedIndex;
  57. }
  58.  
  59. render() {
  60. const { input, onUpdateInput } = this.props;
  61. const { onChange, onBlur } = input;
  62. return (
  63. <AutoComplete
  64. { ...mapError(this.props) }
  65. searchText={ this.state.searchText }
  66. onNewRequest={ (value, index) => onChange(index) }
  67. onBlur={ () => onBlur(this.getSelectedIndex()) }
  68. onChange={ this.onUpdateInput }
  69. onUpdateInput={ (searchText) => {
  70. this.onUpdateInput(searchText);
  71. if (onUpdateInput) {
  72. onUpdateInput(searchText);
  73. }
  74. } }
  75. />
  76. );
  77. }
  78. }
  79.  
  80. // Real example
  81.  
  82. const mapFetchedItemsToAutoComplete = {
  83. value: 'id', text: 'name',
  84. };
  85.  
  86. const UIForm = props => (
  87. <form
  88. onSubmit={ props.handleSubmit }
  89. >
  90. <Field
  91. name="field1"
  92. component={ SelectAutoComplete }
  93. dataSourceConfig={ mapFetchedItemsToAutoComplete }
  94. dataSource={ props.dataSource }
  95. openOnFocus
  96. filter={ AutoComplete.fuzzyFilter }
  97. />
  98. <FlatButton
  99. primary
  100. label="Submit"
  101. type="submit"
  102. style={ { marginLeft: 15 } }
  103. />
  104. </form>
  105. );
  106.  
  107. const formValidator = (values) => {
  108. const errors = {};
  109. if (typeof values.field1 === 'undefined' || values.field1 === -1) {
  110. errors.field1 = 'None of the items has been selected';
  111. }
  112. return errors;
  113. };
  114.  
  115. const Form = reduxForm({
  116. form: 'form1',
  117. validate: formValidator,
  118. })(UIForm);
  119.  
  120. class Application extends React.Component {
  121. constructor(props) {
  122. super(props);
  123. this.state = {
  124. dataSource: [
  125. { id: 1, name: 'Solution' },
  126. { id: 2, name: 'Problem' },
  127. { id: 3, name: 'Fix' },
  128. ],
  129. };
  130. }
  131.  
  132. componentWillMount() {
  133. const interval = setInterval(() => {
  134. if (this.state.dataSource.length > 10) {
  135. clearInterval(interval);
  136. return;
  137. }
  138. const id = this.state.dataSource.length + 1;
  139. this.setState({
  140. dataSource: [
  141. ...this.state.dataSource,
  142. { id, name: `Solution ${id}` },
  143. ],
  144. });
  145. }, 1000);
  146.  
  147. setTimeout(() => {
  148. this.setState({
  149. dataSource: this.state.dataSource.filter(e => e.name !== 'Fix'),
  150. });
  151. }, 3000);
  152. }
  153.  
  154. render() {
  155. return (
  156. <Form
  157. dataSource={ this.state.dataSource.sort((a, b) => a.name > b.name) }
  158. onSubmit={ (values) => alert(JSON.stringify(values, null, 2)) }
  159. />
  160. );
  161. }
  162. }
  163.  
  164. export default () => (
  165. <Application />
  166. );
Add Comment
Please, Sign In to add comment