-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38ade84
commit 939ea61
Showing
8 changed files
with
164 additions
and
394 deletions.
There are no files selected for viewing
85 changes: 0 additions & 85 deletions
85
packages/dnb-eufemia/src/components/help-button/HelpButton.js
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
packages/dnb-eufemia/src/components/help-button/HelpButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<Modal trigger_attributes={params} {...modal_props}> | ||
{content} | ||
</Modal> | ||
) | ||
} | ||
|
||
return <HelpButtonInstance {...params} /> | ||
} |
116 changes: 0 additions & 116 deletions
116
packages/dnb-eufemia/src/components/help-button/HelpButtonInstance.js
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
packages/dnb-eufemia/src/components/help-button/HelpButtonInstance.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Web HelpButton Component | ||
* | ||
*/ | ||
|
||
import React from 'react' | ||
import classnames from 'classnames' | ||
import { | ||
convertJsxToString, | ||
usePropsWithContext, | ||
} from '../../shared/component-helper' | ||
import Context from '../../shared/Context' | ||
import { createSpacingClasses } from '../space/SpacingHelper' | ||
import Button, { ButtonProps } from '../button/Button' | ||
|
||
const defaultProps = { | ||
variant: 'secondary', | ||
icon_position: 'left', | ||
} | ||
|
||
export default function HelpButtonInstance(localProps: ButtonProps) { | ||
const context = React.useContext(Context) | ||
|
||
// use only the props from context, who are available here anyway | ||
const props = usePropsWithContext( | ||
localProps, | ||
defaultProps, | ||
context.FormRow, | ||
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 isPotentialHelpButton = | ||
!params.text || params.variant === 'tertiary' | ||
|
||
if (isPotentialHelpButton && !params.icon && params.icon !== false) { | ||
params.icon = 'question' | ||
} | ||
|
||
const isHelpButton = | ||
isPotentialHelpButton && | ||
['question', 'information'].includes(String(params.icon)) | ||
|
||
if (isHelpButton) { | ||
if (!params['aria-roledescription']) { | ||
params['aria-roledescription'] = | ||
context.getTranslation(props).HelpButton.aria_role | ||
} | ||
} | ||
|
||
if (!params.text && !params['aria-label']) { | ||
let ariaLabel = convertJsxToString(props.title || props.children) | ||
if (!ariaLabel) { | ||
ariaLabel = context.getTranslation(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 <Button on_click={on_click} {...params} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.