Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import React, { PropTypes } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { FormGroup, ControlLabel, FormControl, HelpBlock } from 'react-bootstrap';
  4.  
  5. class Input extends React.Component {
  6. static propTypes = {
  7. children: PropTypes.any,
  8. help: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  9. id: PropTypes.string,
  10. label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  11. type: PropTypes.string.isRequired,
  12. };
  13.  
  14. getValue() {
  15. const inputNode = ReactDOM.findDOMNode(this.refs.formControl);
  16.  
  17. if (this.props.type === 'select' && inputNode.multiple) {
  18. return this.getMultipleSelectValues(inputNode);
  19. }
  20.  
  21. return inputNode.value;
  22. }
  23.  
  24. getMultipleSelectValues(selectNode) {
  25. const values = [];
  26. const options = selectNode.options;
  27.  
  28. for (let i = 0; i < options.length; i++) {
  29. const opt = options[i];
  30.  
  31. if (opt.selected) {
  32. values.push(opt.value || opt.text);
  33. }
  34. }
  35.  
  36. return values;
  37. }
  38.  
  39. render() {
  40. const { id, label, help, children, ...props } = this.props;
  41.  
  42. if (props.type === 'select' || props.type === 'textarea') {
  43. props.componentClass = props.type;
  44. delete props.type;
  45. }
  46.  
  47. return (
  48. <FormGroup controlId={id}>
  49. {label && <ControlLabel>{label}</ControlLabel>}
  50. <FormControl ref="formControl" {...props}>{children}</FormControl>
  51. {help && <HelpBlock>{help}</HelpBlock>}
  52. </FormGroup>
  53. );
  54. }
  55. }
  56.  
  57. export default Input;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement