diff --git a/app/scripts/modules/core/src/help/HelpContext.tsx b/app/scripts/modules/core/src/help/HelpContext.tsx deleted file mode 100644 index 0642e5338fe..00000000000 --- a/app/scripts/modules/core/src/help/HelpContext.tsx +++ /dev/null @@ -1,3 +0,0 @@ -import * as React from 'react'; - -export const { Provider: HelpContextProvider, Consumer: HelpContextConsumer } = React.createContext(false); diff --git a/app/scripts/modules/core/src/help/HelpField.tsx b/app/scripts/modules/core/src/help/HelpField.tsx index bc82825d1d6..32b7026832f 100644 --- a/app/scripts/modules/core/src/help/HelpField.tsx +++ b/app/scripts/modules/core/src/help/HelpField.tsx @@ -1,10 +1,10 @@ import * as React from 'react'; import * as ReactGA from 'react-ga'; import * as DOMPurify from 'dompurify'; +import { isUndefined } from 'lodash'; -import { HelpContentsRegistry } from 'core/help'; +import { HelpContentsRegistry, HelpTextExpandedContext } from 'core/help'; import { HoverablePopover, Placement } from 'core/presentation'; -import { HelpContextConsumer } from './HelpContext'; export interface IHelpFieldProps { id?: string; @@ -15,73 +15,50 @@ export interface IHelpFieldProps { label?: string; } -export class HelpField extends React.PureComponent { - public static defaultProps: IHelpFieldProps = { - placement: 'top', - }; - - private popoverShownStart: number; - - private renderContents(id: string, fallback: string, content: string): JSX.Element { - let contentString = content; - if (id && !contentString) { - contentString = HelpContentsRegistry.getHelpField(id) || fallback; - } +function HelpFieldContents(props: Pick): JSX.Element { + const { id, fallback, content } = props; - const config = { ADD_ATTR: ['target'] }; // allow: target="_blank" - return
; + let contentString = content; + if (id && !contentString) { + contentString = HelpContentsRegistry.getHelpField(id) || fallback; } - private onShow = (): void => { - this.popoverShownStart = Date.now(); - }; + const config = { ADD_ATTR: ['target'] }; // allow: target="_blank" + return
; +} - private onHide = (): void => { - if (Date.now() - this.popoverShownStart > 500) { - ReactGA.event({ action: 'Help contents viewed', category: 'Help', label: this.props.id || this.props.content }); +export function HelpField(props: IHelpFieldProps) { + const { content, expand, fallback, id, label, placement } = props; + + const [popoverShownStart, setPopoverShownStart] = React.useState(); + const onShow = (): void => setPopoverShownStart(Date.now()); + const onHide = (): void => { + if (Date.now() - popoverShownStart > 500) { + ReactGA.event({ action: 'Help contents viewed', category: 'Help', label: props.id || props.content }); } }; - private shouldExpandHelpText(expandFromContext: any, expandFromProps: any) { - if (expandFromProps !== undefined) { - return expandFromProps; - } - return expandFromContext; - } + const icon = ; + const shouldExpandFromContext = React.useContext(HelpTextExpandedContext); + const expandHelpText = isUndefined(expand) ? shouldExpandFromContext : expand; - public render() { - const { placement, label, expand, id, fallback, content } = this.props; - const contents = this.renderContents(id, fallback, content); + const contents = ; + const popover = ( + + {label || icon} + + ); - const icon = ; + if (label) { + return
{!expandHelpText && contents && popover}
; + } else { + const expanded =
{contents}
; - const popover = ( - - {label || icon} - + return ( +
+ {!expandHelpText && contents && popover} + {expandHelpText && contents && expanded} +
); - - if (label) { - return ( - - {context => ( -
{!this.shouldExpandHelpText(context, expand) && contents && popover}
- )} -
- ); - } else { - const expanded =
{contents}
; - - return ( - - {context => ( -
- {!this.shouldExpandHelpText(context, expand) && contents && popover} - {this.shouldExpandHelpText(context, expand) && contents && expanded} -
- )} -
- ); - } } } diff --git a/app/scripts/modules/core/src/help/HelpTextExpandedContext.tsx b/app/scripts/modules/core/src/help/HelpTextExpandedContext.tsx new file mode 100644 index 00000000000..8eb4442e92b --- /dev/null +++ b/app/scripts/modules/core/src/help/HelpTextExpandedContext.tsx @@ -0,0 +1,3 @@ +import * as React from 'react'; + +export const HelpTextExpandedContext = React.createContext(false); diff --git a/app/scripts/modules/core/src/help/index.ts b/app/scripts/modules/core/src/help/index.ts index 5a43727a9d4..ae18b0fb4bc 100644 --- a/app/scripts/modules/core/src/help/index.ts +++ b/app/scripts/modules/core/src/help/index.ts @@ -1,5 +1,5 @@ -export * from './helpContents.registry'; -export * from './help.contents'; export * from './HelpField'; export * from './HelpMenu'; -export * from './HelpContext'; +export * from './HelpTextExpandedContext'; +export * from './help.contents'; +export * from './helpContents.registry'; diff --git a/app/scripts/modules/core/src/presentation/forms/layouts/ResponsiveFieldLayout.tsx b/app/scripts/modules/core/src/presentation/forms/layouts/ResponsiveFieldLayout.tsx index 3896d66a7f5..28c5086ec5b 100644 --- a/app/scripts/modules/core/src/presentation/forms/layouts/ResponsiveFieldLayout.tsx +++ b/app/scripts/modules/core/src/presentation/forms/layouts/ResponsiveFieldLayout.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; -import { HelpContextProvider } from 'core/help'; +import { HelpTextExpandedContext } from 'core/help'; import { ValidationMessage } from 'core/validation'; import { IFieldLayoutProps } from '../interface'; @@ -15,7 +15,7 @@ export class ResponsiveFieldLayout extends React.Component { const helpUnder = false; return ( - +
{showLabel && ( @@ -34,7 +34,7 @@ export class ResponsiveFieldLayout extends React.Component { {validationMessage && }
-
+ ); } }