diff --git a/packages/dnb-eufemia/src/components/help-button/HelpButton.js b/packages/dnb-eufemia/src/components/help-button/HelpButton.js deleted file mode 100644 index cac6357a0a5..00000000000 --- a/packages/dnb-eufemia/src/components/help-button/HelpButton.js +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Web HelpButton Component - * - */ - -import React from 'react' -import PropTypes from 'prop-types' -import { registerElement } from '../../shared/component-helper' -import Context from '../../shared/Context' -import Modal from '../modal/Modal' -import HelpButtonInstance from './HelpButtonInstance' -import Button from '../button/Button' - -export default class HelpButton extends React.PureComponent { - static contextType = Context - static tagName = 'dnb-help-button' - - static propTypes = { - ...Button.propTypes, - icon: PropTypes.oneOfType([ - PropTypes.string, - PropTypes.node, - PropTypes.func, - ]), - icon_position: PropTypes.oneOf(['left', 'right']), - modal_content: PropTypes.node, - modal_props: PropTypes.object, - } - - static defaultProps = { - variant: 'secondary', - icon: null, - icon_position: 'left', - modal_content: null, - modal_props: null, - } - - static enableWebComponent() { - registerElement( - HelpButton?.tagName, - HelpButton, - HelpButton.defaultProps - ) - } - - static getContent(props) { - if (props.modal_content) { - return props.modal_content - } - return typeof props.children === 'function' - ? props.children(props) - : props.children - } - - render() { - const { - modal_content, // eslint-disable-line - children, // eslint-disable-line - modal_props, - ...params - } = this.props - - const content = HelpButton.getContent(this.props) - - if (params.icon === null) { - params.icon = 'question' - } - - if (content) { - if (!params.title) { - params.title = this.context.getTranslation( - this.props - ).HelpButton.title - } - - return ( - - {content} - - ) - } - - return - } -} diff --git a/packages/dnb-eufemia/src/components/help-button/HelpButton.tsx b/packages/dnb-eufemia/src/components/help-button/HelpButton.tsx new file mode 100644 index 00000000000..fc044549478 --- /dev/null +++ b/packages/dnb-eufemia/src/components/help-button/HelpButton.tsx @@ -0,0 +1,62 @@ +/** + * Web HelpButton Component + * + */ + +import React from 'react' +import Context from '../../shared/Context' +import Modal from '../modal/Modal' +import HelpButtonInstance from './HelpButtonInstance' +import { ButtonProps } from '../button/Button' +import { ModalProps } from '../modal/types' +import { usePropsWithContext } from '../../shared/hooks' + +const defaultProps = { + variant: 'secondary', + icon_position: 'left', +} + +export type HelpButtonProps = { + modal_content?: React.ReactNode + modal_props?: ModalProps +} & ButtonProps + +export default function HelpButton(localProps: HelpButtonProps) { + const getContent = (props: HelpButtonProps) => { + if (props.modal_content) { + return props.modal_content + } + return typeof props.children === 'function' + ? props.children(props) + : props.children + } + + const context = React.useContext(Context) + const props = usePropsWithContext(localProps, defaultProps) + const content = getContent(props) + + const { + modal_content, // eslint-disable-line + children, // eslint-disable-line + modal_props, + ...params + } = props + + if (params.icon === null) { + params.icon = 'question' + } + + if (content) { + if (!params.title) { + params.title = context.getTranslation(props).HelpButton.title + } + + return ( + + {content} + + ) + } + + return +} diff --git a/packages/dnb-eufemia/src/components/help-button/HelpButtonInstance.js b/packages/dnb-eufemia/src/components/help-button/HelpButtonInstance.js deleted file mode 100644 index 27d7ff3e97b..00000000000 --- a/packages/dnb-eufemia/src/components/help-button/HelpButtonInstance.js +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Web HelpButton Component - * - */ - -import React from 'react' -import PropTypes from 'prop-types' -import classnames from 'classnames' -import { - convertJsxToString, - extendPropsWithContext, -} from '../../shared/component-helper' -import Context from '../../shared/Context' -import { - spacingPropTypes, - createSpacingClasses, -} from '../space/SpacingHelper' -import Button, { buttonVariantPropType } from '../button/Button' - -export default class HelpButtonInstance extends React.PureComponent { - static contextType = Context - - static propTypes = { - id: PropTypes.string, - ...buttonVariantPropType, - icon: PropTypes.oneOfType([ - PropTypes.string, - PropTypes.node, - PropTypes.func, - ]), - icon_position: PropTypes.oneOf(['left', 'right']), - ...spacingPropTypes, - className: PropTypes.string, - class: PropTypes.string, - } - - static defaultProps = { - id: null, - variant: 'secondary', - icon: null, - icon_position: 'left', - className: null, - class: null, - } - - render() { - // use only the props from context, who are available here anyway - const props = extendPropsWithContext( - this.props, - HelpButtonInstance.defaultProps, - this.context.FormRow, - this.context.HelpButton - ) - - const { - size, - icon, - on_click, - className, - class: _className, - ...rest - } = props - - const params = { - className: classnames( - 'dnb-help-button', - createSpacingClasses(props), - className, - _className - ), - size, - icon, - ...rest, - } - - const isPotensialHelpButton = - !params.text || params.variant === 'tertiary' - - if (isPotensialHelpButton && !params.icon && params.icon !== false) { - params.icon = 'question' - } - - const isHelpButton = - isPotensialHelpButton && - ['question', 'information'].includes(String(params.icon)) - - if (isHelpButton) { - if (!params['aria-roledescription']) { - params['aria-roledescription'] = this.context.getTranslation( - this.props - ).HelpButton.aria_role - } - } - - if (!params.text && !params['aria-label']) { - let ariaLabel = convertJsxToString(props.title || props.children) - if (!ariaLabel) { - ariaLabel = this.context.getTranslation(this.props).HelpButton - .title - } - params['aria-label'] = ariaLabel - } - - if (icon === 'information' && !size) { - params.icon_size = 'medium' - } - if (params.title && !params.tooltip && params.tooltip !== false) { - params.tooltip = params.title - } - if (params.tooltip) { - params.title = null - } - - return - - - - -`; - exports[`HelpButton scss have to match default theme snapshot 1`] = ` "/* * Np theme is provided diff --git a/packages/dnb-eufemia/src/components/help-button/stories/HelpButton.stories.js b/packages/dnb-eufemia/src/components/help-button/stories/HelpButton.stories.tsx similarity index 99% rename from packages/dnb-eufemia/src/components/help-button/stories/HelpButton.stories.js rename to packages/dnb-eufemia/src/components/help-button/stories/HelpButton.stories.tsx index 4c6685df5aa..553ec799cda 100644 --- a/packages/dnb-eufemia/src/components/help-button/stories/HelpButton.stories.js +++ b/packages/dnb-eufemia/src/components/help-button/stories/HelpButton.stories.tsx @@ -6,7 +6,7 @@ import React from 'react' import { Wrapper, Box } from 'storybook-utils/helpers' -import { HelpButton, Modal, Button, Section, Input } from '../../' +import { HelpButton, Modal, Button, Section, Input } from '../..' export default { title: 'Eufemia/Components/HelpButton', diff --git a/packages/dnb-eufemia/src/components/modal/Modal.tsx b/packages/dnb-eufemia/src/components/modal/Modal.tsx index 2dacfd3cd96..ff3757fa195 100644 --- a/packages/dnb-eufemia/src/components/modal/Modal.tsx +++ b/packages/dnb-eufemia/src/components/modal/Modal.tsx @@ -32,6 +32,7 @@ import { classWithCamelCaseProps, ToCamelCasePartial, } from '../../shared/helpers/withCamelCaseProps' +import { ButtonProps } from '../button/Button' export const ANIMATION_DURATION = 300 @@ -464,7 +465,7 @@ class Modal extends React.PureComponent< icon_position: trigger_icon_position, class: trigger_class, ...trigger_attributes, - } + } as ButtonProps if (isTrue(disabled)) { triggerAttributes.disabled = true } @@ -487,17 +488,17 @@ class Modal extends React.PureComponent< ? (trigger as React.FC) : HelpButtonInstance + const title = ( + !triggerAttributes.text ? rest.title || fallbackTitle : null + ) as string + return ( <> {TriggerButton && !isTrue(omit_trigger_button) && (