Skip to content

Commit

Permalink
fix(Forms): ensure error shows correct border and text colors (#2717)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Oct 3, 2023
1 parent 5bcfdd9 commit f1bc34d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/dnb-eufemia/src/extensions/forms/Field/Number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function NumberComponent(props: Props) {
on_blur={handleBlur}
on_change={handleChange}
disabled={disabled}
status={error ? 'error' : undefined}
stretch={width !== undefined}
suffix={
help ? (
Expand Down
3 changes: 3 additions & 0 deletions packages/dnb-eufemia/src/extensions/forms/Field/String.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ function StringComponent(props: Props) {
disabled={disabled}
stretch={width !== undefined}
inner_ref={innerRef}
status={error ? 'error' : undefined}
/>
) : mask ? (
<InputMasked
Expand All @@ -173,6 +174,7 @@ function StringComponent(props: Props) {
disabled={disabled}
stretch={width !== undefined}
inner_ref={innerRef}
status={error ? 'error' : undefined}
/>
) : (
<Input
Expand All @@ -195,6 +197,7 @@ function StringComponent(props: Props) {
disabled={disabled}
stretch={width !== undefined}
inner_ref={innerRef}
status={error ? 'error' : undefined}
/>
)}
</FieldBlock>
Expand Down
5 changes: 4 additions & 1 deletion packages/dnb-eufemia/src/extensions/forms/Field/Toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function Toggle(props: Props) {
label={label}
checked={isOn}
disabled={disabled}
status={error ? 'error' : undefined}
on_change={handleCheckboxChange}
{...pickSpacingProps(props)}
/>
Expand All @@ -105,6 +106,7 @@ function Toggle(props: Props) {
}
checked={isOn}
disabled={disabled}
status={error ? 'error' : undefined}
value={value ? 'true' : 'false'}
on_change={handleCheckboxChange}
/>
Expand All @@ -127,8 +129,8 @@ function Toggle(props: Props) {
text={textOff ?? sharedContext?.translation.Forms.booleanNo}
on_click={setOff}
variant={isOn ? 'secondary' : undefined}
status={error ? 'error' : undefined}
disabled={disabled}
status={error ? 'error' : undefined}
/>
</ButtonRow>
<Space bottom="x-small" />
Expand All @@ -147,6 +149,7 @@ function Toggle(props: Props) {
}
checked={isOn}
disabled={disabled}
status={error ? 'error' : undefined}
value={value ? 'true' : 'false'}
on_change={handleCheckboxChange}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ describe('Field.Boolean', () => {
).toBeInTheDocument()
})

it('shows error border', () => {
render(
<Field.Boolean
variant="checkbox"
error={new Error('This is what went wrong')}
/>
)
const element = document.querySelector('.dnb-checkbox')
expect(element.className).toContain('dnb-checkbox__status--error')
})

it('should toggle when clicking', async () => {
const onChange = jest.fn()
render(
Expand Down Expand Up @@ -93,6 +104,19 @@ describe('Field.Boolean', () => {
).toBeInTheDocument()
})

it('shows error border', () => {
render(
<Field.Boolean
variant="button"
error={new Error('This is what went wrong')}
/>
)
const element = document.querySelector('.dnb-toggle-button')
expect(element.className).toContain(
'dnb-toggle-button__status--error'
)
})

it('should show error when no value is given', () => {
render(<Field.Boolean variant="button" required validateInitially />)
expect(screen.getByRole('alert')).toBeInTheDocument()
Expand Down Expand Up @@ -143,6 +167,19 @@ describe('Field.Boolean', () => {
).toBeInTheDocument()
})

it('shows error border', () => {
render(
<Field.Boolean
variant="checkbox-button"
error={new Error('This is what went wrong')}
/>
)
const element = document.querySelector('.dnb-toggle-button')
expect(element.className).toContain(
'dnb-toggle-button__status--error'
)
})

it('should show error when no value is given', () => {
render(
<Field.Boolean
Expand Down Expand Up @@ -197,6 +234,17 @@ describe('Field.Boolean', () => {
).toBeInTheDocument()
})

it('shows error border', () => {
render(
<Field.Boolean
variant="buttons"
error={new Error('This is what went wrong')}
/>
)
const element = document.querySelector('.dnb-button')
expect(element.className).toContain('dnb-button__status--error')
})

it('should show error when no value is given', () => {
render(
<Field.Boolean variant="buttons" required validateInitially />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ describe('Field.Number', () => {
).toBeInTheDocument()
})

it('shows error border', () => {
render(<Field.Number error={new Error('This is what went wrong')} />)
const element = document.querySelector('.dnb-input')
expect(element.className).toContain('dnb-input__status--error')
})

it('formats with given thousandSeparator', () => {
const { rerender } = render(
<Field.Number value={12345} thousandSeparator=" " />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,15 @@ describe('Selection', () => {
})
})
})

it('shows error border', () => {
render(
<Field.Selection error={new Error('This is what went wrong')}>
<Field.Option value="foo">Fooo</Field.Option>
<Field.Option value="bar">Baar</Field.Option>
</Field.Selection>
)
const element = document.querySelector('.dnb-dropdown')
expect(element.className).toContain('dnb-dropdown__status--error')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@ describe('Field.String', () => {
screen.getByText('This is what went wrong')
).toBeInTheDocument()
})

describe('shows error border', () => {
it('for basis input', () => {
render(
<Field.String error={new Error('This is what went wrong')} />
)
const element = document.querySelector('.dnb-input')
expect(element.className).toContain('dnb-input__status--error')
})

it('for masked input', () => {
render(
<Field.String
mask={[/\/d/]}
error={new Error('This is what went wrong')}
/>
)
const element = document.querySelector('.dnb-input-masked')
expect(element.className).toContain('dnb-input__status--error')
})

it('for multiline input', () => {
render(
<Field.String
multiline
error={new Error('This is what went wrong')}
/>
)
const element = document.querySelector('.dnb-textarea')
expect(element.className).toContain('dnb-textarea__status--error')
})
})
})

describe('event handlers', () => {
Expand Down

0 comments on commit f1bc34d

Please sign in to comment.