11import React , { Component } from 'react'
2+ import PT from 'prop-types'
23import { HOC as hoc } from 'formsy-react'
34import classNames from 'classnames'
45
6+ import HelpIcon from '../HelpIcon/HelpIcon'
7+
8+ import styles from './TextInput.scss'
9+
510class TextInput extends Component {
611
712 constructor ( props ) {
@@ -16,16 +21,20 @@ class TextInput extends Component {
1621 }
1722
1823 render ( ) {
19- const { label, name, type, minValue, maxValue, placeholder, wrapperClass, maxLength, theme } = this . props
24+ const { label, name, type, minValue, maxValue, placeholder, wrapperClass, maxLength, theme,
25+ labelHelpTooltip, readonly, readonlyValueTooltip } = this . props
2026 const hasError = ! this . props . isPristine ( ) && ! this . props . isValid ( )
21- const wrapperClasses = classNames ( wrapperClass , theme )
22- const classes = classNames ( 'tc-file-field__inputs' , { error : hasError } , { empty : this . props . getValue ( ) === '' } )
2327 const disabled = this . props . isFormDisabled ( ) || this . props . disabled
28+ const wrapperClasses = classNames ( wrapperClass , theme , { [ styles [ 'readonly-wrapper' ] ] : readonly } )
29+ const classes = classNames ( 'tc-file-field__inputs' , { error : hasError } , { empty : this . props . getValue ( ) === '' } )
2430 const errorMessage = this . props . getErrorMessage ( ) || this . props . validationError
2531
2632 return (
2733 < div className = { wrapperClasses } >
28- < label className = "tc-label" > { label } </ label >
34+ < label className = "tc-label" >
35+ { label }
36+ { labelHelpTooltip && < HelpIcon tooltip = { labelHelpTooltip } /> }
37+ </ label >
2938 < input
3039 name = { name }
3140 className = { classes }
@@ -38,7 +47,13 @@ class TextInput extends Component {
3847 min = { minValue }
3948 max = { maxValue }
4049 />
41- { hasError ? ( < p className = "error-message" > { errorMessage } </ p > ) : null }
50+ { readonly && (
51+ < div styleName = "readonly-value" >
52+ { this . props . getValue ( ) }
53+ { readonlyValueTooltip && < HelpIcon tooltip = { readonlyValueTooltip } /> }
54+ </ div >
55+ ) }
56+ { hasError ? ( < p className = "error-message" > { errorMessage } </ p > ) : null }
4257 </ div >
4358 )
4459 }
@@ -48,4 +63,23 @@ TextInput.defaultProps = {
4863 onChange : ( ) => { }
4964}
5065
66+ TextInput . propTypes = {
67+ /**
68+ * The difference from `disabled` is that instead of showing disabled input
69+ * we show value using <div> which let us position something immediately after the value
70+ */
71+ readonly : PT . bool ,
72+
73+ /**
74+ * Show help icon next to the label with the tooltip defined by this prop
75+ */
76+ labelHelpTooltip : PT . node ,
77+
78+ /**
79+ * Show help icon next to the value with the tooltip defined by this prop
80+ * This only has any effect if `readonly` is set to `true`
81+ */
82+ readonlyValueTooltip : PT . node
83+ }
84+
5185export default hoc ( TextInput )
0 commit comments