From 6192e542a57c953d44249c706b97086e2594379f Mon Sep 17 00:00:00 2001 From: Maksym Mykhailenko Date: Tue, 28 Aug 2018 14:31:33 +0800 Subject: [PATCH 1/2] Implemented ability to add help tooltip to the TextInput next to label and next to read-only value. --- components/Formsy/TextInput.jsx | 46 +++++++++++++++++++++++++++---- components/Formsy/TextInput.scss | 17 ++++++++++++ components/HelpIcon/HelpIcon.jsx | 40 +++++++++++++++++++++++++++ components/HelpIcon/HelpIcon.scss | 16 +++++++++++ components/Icons/round-e-help.svg | 1 + 5 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 components/Formsy/TextInput.scss create mode 100644 components/HelpIcon/HelpIcon.jsx create mode 100644 components/HelpIcon/HelpIcon.scss create mode 100644 components/Icons/round-e-help.svg diff --git a/components/Formsy/TextInput.jsx b/components/Formsy/TextInput.jsx index 64ed864a5..775a7749c 100644 --- a/components/Formsy/TextInput.jsx +++ b/components/Formsy/TextInput.jsx @@ -1,7 +1,12 @@ import React, { Component } from 'react' +import PT from 'prop-types' import { HOC as hoc } from 'formsy-react' import classNames from 'classnames' +import HelpIcon from '../HelpIcon/HelpIcon' + +import styles from './TextInput.scss' + class TextInput extends Component { constructor(props) { @@ -16,16 +21,20 @@ class TextInput extends Component { } render() { - const { label, name, type, minValue, maxValue, placeholder, wrapperClass, maxLength, theme } = this.props + const { label, name, type, minValue, maxValue, placeholder, wrapperClass, maxLength, theme, + labelHelpTooltip, readonly, readonlyValueTooltip } = this.props const hasError = !this.props.isPristine() && !this.props.isValid() - const wrapperClasses = classNames(wrapperClass, theme) - const classes = classNames('tc-file-field__inputs', {error: hasError}, {empty: this.props.getValue() === ''}) const disabled = this.props.isFormDisabled() || this.props.disabled + const wrapperClasses = classNames(wrapperClass, theme, { [styles['readonly-wrapper']]: readonly }) + const classes = classNames('tc-file-field__inputs', {error: hasError}, {empty: this.props.getValue() === ''}) const errorMessage = this.props.getErrorMessage() || this.props.validationError return (
- + - { hasError ? (

{errorMessage}

) : null} + {readonly && ( +
+ {this.props.getValue()} + {readonlyValueTooltip && } +
+ )} + { hasError ? (

{errorMessage}

) : null}
) } } TextInput.defaultProps = { - onChange: () => {} + onChange: () => {}, +} + +TextInput.propTypes = { + /** + * The difference from `disabled` is that instead of showing disabled input + * we show value using
which let us position something immediately after the value + */ + readonly: PT.bool, + + /** + * Show help icon next to the label with the tooltip defined by this prop + */ + labelHelpTooltip: PT.node, + + /** + * Show help icon next to the value with the tooltip defined by this prop + * This only has any effect if `readonly` is set to `true` + */ + readonlyValueTooltip: PT.node, } export default hoc(TextInput) diff --git a/components/Formsy/TextInput.scss b/components/Formsy/TextInput.scss new file mode 100644 index 000000000..4e21c403e --- /dev/null +++ b/components/Formsy/TextInput.scss @@ -0,0 +1,17 @@ +@import '~tc-ui/src/styles/tc-includes'; + +.readonly-wrapper { + :global(input.tc-file-field__inputs) { + display: none; + } +} + +.readonly-value { + color: $tc-gray-60; + display: block; + height: 40px; + line-height: 40px; + margin-bottom: 2 * $base-unit; + padding: 0 2 * $base-unit; + width: 100%; +} \ No newline at end of file diff --git a/components/HelpIcon/HelpIcon.jsx b/components/HelpIcon/HelpIcon.jsx new file mode 100644 index 000000000..5d5394f78 --- /dev/null +++ b/components/HelpIcon/HelpIcon.jsx @@ -0,0 +1,40 @@ +/** + * Help icon with tooltip + */ +import React from 'react' +import _ from 'lodash' +import PT from 'prop-types' +import cn from 'classnames' + +import HelpIconSvg from '../icons/round-e-help.svg' +import Tooltip from '../Tooltip/Tooltip' + +import styles from './HelpIcon.scss' + +const HELP_TOOLTIP_SHOW_DELAY = 300 + +const HelpIcon = ({ + className, + showTooltipDelay, + tooltip, +}) => { + const delay = !_.isNumber(showTooltipDelay) ? showTooltipDelay : HELP_TOOLTIP_SHOW_DELAY + + return ( + +
+
{tooltip}
+
+ ) +} + +HelpIcon.propTypes = { + className: PT.string, + showTooltipDelay: PT.number, + tooltip: PT.node, +} + +export default HelpIcon \ No newline at end of file diff --git a/components/HelpIcon/HelpIcon.scss b/components/HelpIcon/HelpIcon.scss new file mode 100644 index 000000000..0eb45a69b --- /dev/null +++ b/components/HelpIcon/HelpIcon.scss @@ -0,0 +1,16 @@ +@import '~tc-ui/src/styles/tc-includes'; + +.label-help-icon { + display: inline-block; + line-height: normal; + margin-left: $base-unit; + position: relative; + top: 3px; + text-transform: none; + + svg { + g { + fill: $tc-gray-50; + } + } +} \ No newline at end of file diff --git a/components/Icons/round-e-help.svg b/components/Icons/round-e-help.svg new file mode 100644 index 000000000..27c35df4c --- /dev/null +++ b/components/Icons/round-e-help.svg @@ -0,0 +1 @@ + \ No newline at end of file From 799f68eed76efb1034444af8957a116c214d5a25 Mon Sep 17 00:00:00 2001 From: Maksym Mykhailenko Date: Tue, 28 Aug 2018 14:40:22 +0800 Subject: [PATCH 2/2] fix lint --- components/Formsy/TextInput.jsx | 4 ++-- components/HelpIcon/HelpIcon.jsx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/Formsy/TextInput.jsx b/components/Formsy/TextInput.jsx index 775a7749c..3abc198b3 100644 --- a/components/Formsy/TextInput.jsx +++ b/components/Formsy/TextInput.jsx @@ -60,7 +60,7 @@ class TextInput extends Component { } TextInput.defaultProps = { - onChange: () => {}, + onChange: () => {} } TextInput.propTypes = { @@ -79,7 +79,7 @@ TextInput.propTypes = { * Show help icon next to the value with the tooltip defined by this prop * This only has any effect if `readonly` is set to `true` */ - readonlyValueTooltip: PT.node, + readonlyValueTooltip: PT.node } export default hoc(TextInput) diff --git a/components/HelpIcon/HelpIcon.jsx b/components/HelpIcon/HelpIcon.jsx index 5d5394f78..633e0cbc4 100644 --- a/components/HelpIcon/HelpIcon.jsx +++ b/components/HelpIcon/HelpIcon.jsx @@ -16,7 +16,7 @@ const HELP_TOOLTIP_SHOW_DELAY = 300 const HelpIcon = ({ className, showTooltipDelay, - tooltip, + tooltip }) => { const delay = !_.isNumber(showTooltipDelay) ? showTooltipDelay : HELP_TOOLTIP_SHOW_DELAY @@ -34,7 +34,7 @@ const HelpIcon = ({ HelpIcon.propTypes = { className: PT.string, showTooltipDelay: PT.number, - tooltip: PT.node, + tooltip: PT.node } export default HelpIcon \ No newline at end of file