Skip to content

Commit

Permalink
enhance radio button internal checks with dedicated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed May 27, 2019
1 parent 0b22ff2 commit 8a27802
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/dnb-ui-lib/src/components/radio/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default class Radio extends Component {

onKeyDownHandler = event => {
// only have key support if there is only a single radio
if (typeof this.context.value === 'undefined' && !this.props.group) {
if (this.isInNoGroup()) {
switch (keycode(event)) {
case 'enter':
this.onChangeHandler(event)
Expand Down Expand Up @@ -151,32 +151,36 @@ export default class Radio extends Component {
}
const value = event.target.value
const checked = !this.state.checked
// only support on change if there is either:
// 1. context group usage
// 2. or a single, no group usage
const isContextGroupOrSingle =
typeof this.context.value !== 'undefined' && !this.props.group

// delay in case we have a
if (this.isPlainGroup()) {
setTimeout(() => {
this.setState(
{ checked, _listenForPropChanges: false },
() =>
isContextGroupOrSingle && this.callOnChange({ value, checked })
this.isContextGroupOrSingle() &&
this.callOnChange({ value, checked })
)
}, 1) // in case we have a false "hasContext" but a "group", then we have to use a delay, to overwrite the uncrontrolled state
} else {
this.setState(
{ checked, _listenForPropChanges: false },
() =>
isContextGroupOrSingle && this.callOnChange({ value, checked })
this.isContextGroupOrSingle() &&
this.callOnChange({ value, checked })
)
}
}

// only support on change if there is either:
// 1. context group usage
// 2. or a single, no group usage
isContextGroupOrSingle = () =>
typeof this.context.value !== 'undefined' && !this.props.group
isPlainGroup = () =>
typeof this.context.value === 'undefined' && this.props.group
isInNoGroup = () =>
typeof this.context.value === 'undefined' && !this.props.group

onClickHandler = event => {
if (String(this.props.readOnly) === 'true') {
Expand Down

0 comments on commit 8a27802

Please sign in to comment.