Skip to content

Commit

Permalink
fix(Radio): fix radio button disabled issue when inside of group (#1477)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Jun 22, 2022
1 parent d0756e2 commit e775792
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/dnb-eufemia/src/components/radio/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class Radio extends React.PureComponent {
label_sr_only: null,
label_position: null,
checked: null,
disabled: false,
disabled: null,
id: null,
size: null,
element: 'input',
Expand Down Expand Up @@ -316,7 +316,9 @@ export default class Radio extends React.PureComponent {
checked = this.context.value === value
}
group = this.context.name
disabled = isTrue(this.context.disabled)
if (isTrue(this.context.disabled) && disabled !== false) {
disabled = true
}
} else if (typeof rest.name !== 'undefined') {
group = rest.name
}
Expand Down
54 changes: 50 additions & 4 deletions packages/dnb-eufemia/src/components/radio/__tests__/Radio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,19 @@ describe('Radio component', () => {
)
})

it('has a disabled attribute, once we set disabled to true', () => {
const Comp = mount(<Component />)
it('will disable a single button', () => {
const Comp = mount(<Component disabled />)

expect(Comp.find('input').instance().hasAttribute('disabled')).toBe(
true
)

Comp.setProps({
disabled: true,
disabled: false,
})

expect(Comp.find('input').instance().hasAttribute('disabled')).toBe(
true
false
)
})

Expand Down Expand Up @@ -183,6 +189,46 @@ describe('Radio group component', () => {
expect(my_event.mock.calls[1][0].value).toBe('second')
})

it('will disable a single button within a group', () => {
const Comp = mount(
<Component.Group>
<Component disabled />
</Component.Group>
)

expect(Comp.find('input').instance().hasAttribute('disabled')).toBe(
true
)
})

it('will disable a single button, defined in the group', () => {
const Comp = mount(
<Component.Group disabled>
<Component />
</Component.Group>
)

expect(Comp.find('input').instance().hasAttribute('disabled')).toBe(
true
)
})

it('will overwrite "disable" state, defined in the group', () => {
const Comp = mount(
<Component.Group disabled>
<Component disabled={false} />
<Component disabled />
</Component.Group>
)

expect(
Comp.find('input').first().instance().hasAttribute('disabled')
).toBe(false)
expect(
Comp.find('input').last().instance().hasAttribute('disabled')
).toBe(true)
})

// mount compare the snapshot
it('have to match group snapshot', () => {
expect(toJson(Comp)).toMatchSnapshot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ exports[`Radio group component have to match group snapshot 1`] = `
className={null}
custom_element={null}
custom_method={null}
disabled={false}
disabled={null}
element="input"
global_status_id={null}
group={null}
Expand Down Expand Up @@ -426,7 +426,7 @@ exports[`Radio group component have to match group snapshot 1`] = `
<FormLabel
class={null}
className={null}
disabled={false}
disabled={null}
element="label"
for_id="radio-1"
id="radio-1-label"
Expand Down Expand Up @@ -458,7 +458,7 @@ exports[`Radio group component have to match group snapshot 1`] = `
className={null}
custom_element={null}
custom_method={null}
disabled={false}
disabled={null}
element="input"
global_status_id={null}
group={null}
Expand Down Expand Up @@ -555,7 +555,7 @@ exports[`Radio group component have to match group snapshot 1`] = `
<FormLabel
class={null}
className={null}
disabled={false}
disabled={null}
element="label"
for_id="radio-2"
id="radio-2-label"
Expand Down

0 comments on commit e775792

Please sign in to comment.