Skip to content

Commit fb6bb37

Browse files
author
vikasrohit
authored
Merge pull request #292 from maxceem/feature/text-input-help-tooltip
Implemented ability to add help tooltip to the TextInput next to label and next to read-only value.
2 parents ed3018e + 799f68e commit fb6bb37

File tree

5 files changed

+113
-5
lines changed

5 files changed

+113
-5
lines changed

components/Formsy/TextInput.jsx

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React, { Component } from 'react'
2+
import PT from 'prop-types'
23
import { HOC as hoc } from 'formsy-react'
34
import classNames from 'classnames'
45

6+
import HelpIcon from '../HelpIcon/HelpIcon'
7+
8+
import styles from './TextInput.scss'
9+
510
class 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+
5185
export default hoc(TextInput)

components/Formsy/TextInput.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@import '~tc-ui/src/styles/tc-includes';
2+
3+
.readonly-wrapper {
4+
:global(input.tc-file-field__inputs) {
5+
display: none;
6+
}
7+
}
8+
9+
.readonly-value {
10+
color: $tc-gray-60;
11+
display: block;
12+
height: 40px;
13+
line-height: 40px;
14+
margin-bottom: 2 * $base-unit;
15+
padding: 0 2 * $base-unit;
16+
width: 100%;
17+
}

components/HelpIcon/HelpIcon.jsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Help icon with tooltip
3+
*/
4+
import React from 'react'
5+
import _ from 'lodash'
6+
import PT from 'prop-types'
7+
import cn from 'classnames'
8+
9+
import HelpIconSvg from '../icons/round-e-help.svg'
10+
import Tooltip from '../Tooltip/Tooltip'
11+
12+
import styles from './HelpIcon.scss'
13+
14+
const HELP_TOOLTIP_SHOW_DELAY = 300
15+
16+
const HelpIcon = ({
17+
className,
18+
showTooltipDelay,
19+
tooltip
20+
}) => {
21+
const delay = !_.isNumber(showTooltipDelay) ? showTooltipDelay : HELP_TOOLTIP_SHOW_DELAY
22+
23+
return (
24+
<Tooltip
25+
theme={cn('light', styles['label-help-icon'], className)}
26+
tooltipDelay={delay}
27+
>
28+
<div className="tooltip-target"><HelpIconSvg /></div>
29+
<div className="tooltip-body">{tooltip}</div>
30+
</Tooltip>
31+
)
32+
}
33+
34+
HelpIcon.propTypes = {
35+
className: PT.string,
36+
showTooltipDelay: PT.number,
37+
tooltip: PT.node
38+
}
39+
40+
export default HelpIcon

components/HelpIcon/HelpIcon.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@import '~tc-ui/src/styles/tc-includes';
2+
3+
.label-help-icon {
4+
display: inline-block;
5+
line-height: normal;
6+
margin-left: $base-unit;
7+
position: relative;
8+
top: 3px;
9+
text-transform: none;
10+
11+
svg {
12+
g {
13+
fill: $tc-gray-50;
14+
}
15+
}
16+
}

components/Icons/round-e-help.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)