Skip to content

Commit

Permalink
Enhance handling of composition fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Nov 17, 2024
1 parent abce831 commit d90aa74
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,43 @@ export const InlineHelpButtonHorizontalLabel = () => {
</ComponentBox>
)
}

export const InlineHelpButtonCompositionFields = () => {
return (
<ComponentBox data-visual-test="forms-field-block-help-button-composition-fields">
<Card stack>
<Field.Composition label="Field.Composition" width="large">
<Field.String
width="medium"
label="Label"
help={{
title: 'Hva betyr lånebeløp? ',
content: 'Dette er hvor mye du har tenkt å låne totalt.',
}}
/>
<Field.String
width="stretch"
label="Label"
help={{
title: 'Hva betyr lånebeløp? ',
content: 'Dette er hvor mye du har tenkt å låne totalt.',
}}
/>
</Field.Composition>
<Field.PostalCodeAndCity
help={{
title: 'Hva betyr lånebeløp? ',
content: 'Dette er hvor mye du har tenkt å låne totalt.',
}}
/>
<Field.PhoneNumber
help={{
open: true,
title: 'Hva betyr lånebeløp? ',
content: 'Dette er hvor mye du har tenkt å låne totalt.',
}}
/>
</Card>
</ComponentBox>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Error messages from all fields inside the FieldBlock are combined as one message

<Examples.InlineHelpButtonHorizontalLabel />

### Inline help button (composition fields)

<Examples.InlineHelpButtonCompositionFields />

### Inline help button with HTML

<Examples.InlineHelpButtonHTML />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import { question as QuestionIcon, close as CloseIcon } from '../../icons'
export type HelpProps = {
title?: React.ReactNode
content?: React.ReactNode
open?: boolean
renderAs?: 'inline' | 'dialog'
/** Only for the "inline" variant */
open?: boolean
/** Only for the "inline" variant */
breakout?: boolean
}

export type HelpButtonInlineProps = HelpButtonProps & {
Expand Down Expand Up @@ -110,11 +113,18 @@ export function HelpButtonInlineContent(
const { data, update } =
useSharedState<HelpButtonInlineSharedStateDataProps>(contentId)
const { isOpen, isUserIntent, buttonRef } = data || {}
const { open, title, content, renderAs } = helpProp || {}
const {
open,
title,
content,
renderAs,
breakout: breakoutProp = true,
} = helpProp || {}

const innerRef = useRef<HTMLDivElement>(null)
const cardContext = useContext(CardContext)
const isInsideCard = Boolean(cardContext) && breakout
const breakoutFromLayout =
Boolean(cardContext) && breakout && breakoutProp

useEffect(() => {
if (isOpen && isUserIntent) {
Expand Down Expand Up @@ -166,10 +176,10 @@ export function HelpButtonInlineContent(
tabIndex={-1}
innerRef={innerRef}
onKeyDown={keydownHandler}
breakout={isInsideCard}
roundedCorner={!isInsideCard}
breakout={breakoutFromLayout}
roundedCorner={!breakoutFromLayout}
innerSpace={
isInsideCard
breakoutFromLayout
? { top: 'small', bottom: 'medium' }
: {
top: 'small',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ function PhoneNumber(props: Props) {
width={
omitCountryCodeField ? 'medium' : props.width ?? 'stretch'
}
help={help}
help={{ ...help, breakout: false }}
required={required}
errorMessages={errorMessages}
validateInitially={validateInitially}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,10 @@ function FieldBlock(props: Props) {
contentId={`${id}-help`}
className="dnb-forms-field-block__help"
help={help}
breakout={layout !== 'horizontal'}
breakout={
layout === 'vertical' &&
!nestedFieldBlockContext?.composition
}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const FieldBlockSharedProperties: PropertiesTableProps = {
status: 'optional',
},
help: {
doc: 'Provide help content for the field using `title` and `content` as a string or React.Node. Additionally, you can set `open` to `true` to display the inline help or use `renderAs` set to `dialog` to render the content in a [Dialog](/uilib/components/dialog/) (recommended for larger amounts of content).',
doc: 'Provide help content for the field using `title` and `content` as a string or React.Node. Additionally, you can set `open` to `true` to display the inline help, set the `breakout` property to `false` to disable the breakout of the inline help content, or use `renderAs` set to `dialog` to render the content in a [Dialog](/uilib/components/dialog/) (recommended for larger amounts of content).',
type: 'object',
status: 'optional',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ describe.each(['ui', 'sbanken'])('FieldBlock for %s', (themeName) => {
expect(screenshot).toMatchImageSnapshot()
})

it('have to match help-button in composition fields', async () => {
const screenshot = await makeScreenshot({
selector:
'[data-visual-test="forms-field-block-help-button-composition-fields"]',
})
expect(screenshot).toMatchImageSnapshot()
})

it('have to match label description help-button', async () => {
const screenshot = await makeScreenshot({
selector:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d90aa74

Please sign in to comment.