IcInput.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2015 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react'
  19. import PropTypes from 'prop-types'
  20. import _ from 'underscore'
  21. import classnames from 'classnames'
  22. const { string, any, bool } = PropTypes
  23. let idCount = 0
  24. const IcInputPropTypes = {
  25. error: string,
  26. label: string,
  27. hint: string,
  28. elementType: any,
  29. controlClassName: string,
  30. appendLabel: bool,
  31. noClassName: bool,
  32. type: string
  33. }
  34. /**
  35. * An input wrapped with appropriate ic-Form-* elements and classes,
  36. * with support for a label, error message and extra classes on the
  37. * wrapping div.
  38. *
  39. * All other props are passed through to the <input />
  40. */
  41. class IcInput extends React.Component {
  42. static propTypes = IcInputPropTypes
  43. static defaultProps = {
  44. elementType: 'input'
  45. }
  46. componentWillMount () {
  47. this.id = `ic_input_${idCount++}`;
  48. }
  49. render () {
  50. const { error, label, hint, elementType, appendLabel, controlClassName, noClassName } = this.props
  51. const inputProps = Object.assign({}, _.omit(this.props, Object.keys(IcInputPropTypes)), {id: this.id})
  52. if (elementType === "input" && !this.props.type) {
  53. inputProps.type = "text";
  54. }
  55. if (this.props.type) {
  56. inputProps.type = this.props.type
  57. }
  58. if (!noClassName) {
  59. inputProps.className = classnames(inputProps.className, "ic-Input");
  60. }
  61. const labelElement = label &&
  62. <label htmlFor={this.id} className="ic-Label">{label}</label>;
  63. const hintElement = !!hint && <div className="ic-Form-help-text">{hint}</div>
  64. return (
  65. <div className={classnames('ic-Form-control', controlClassName, {'ic-Form-control--has-error': error})}>
  66. {!!label && !appendLabel && labelElement}
  67. {React.createElement(elementType, inputProps)}
  68. {!!label && appendLabel && labelElement}
  69. {!!error &&
  70. <div className="ic-Form-message ic-Form-message--error">
  71. <div className="ic-Form-message__Layout">
  72. <i className="icon-warning" role="presentation" />
  73. {error}
  74. </div>
  75. </div>
  76. }
  77. {!!hint && hintElement}
  78. </div>
  79. );
  80. }
  81. }
  82. export default IcInput