question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Button] Composability with Checkbox and Radio

See original GitHub issue

Searching for alternatives to regular checkboxes and radio buttons, I found out that composing a Button with a Checkbox or a Radio might be a good idea.

At first, I wanted to use the as prop for the job, but it turns out that Button does not accept a value or name prop (although they are valid button attributes according to MDN). Also, the isDisabled prop only has an effect on the button’s style, but not the checkbox element:

const choice = 'Answer to a quiz question';

<Button
  as={Radio}
  key={choice}
  value={choice}
  borderWidth={2}
  size="lg"
  isDisabled={selectedChoice != null}
  isFullWidth
>
  {choice}
</Button>

My second try was adding Radio as a child of Button, but that approach is semantically incorrect and makes the button’s click area greater than its radio child:

const choice = 'Answer to a quiz question';

<Button variant="outline" isFullWidth>
  <Radio
    key={choice}
    value={choice}
    borderWidth={3}
    size="lg"
    isFullWidth
  >
    {choice}
  </Radio>
</Button>

image

I think that the first approach should be fine-tuned to work without hassle. I tried using as={props => <Radio {...props} isDisabled />} instead of as={Radio}, but it also resulted in buggy behavior.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
segunadebayocommented, Nov 22, 2019

@kripod, It’s a bot you know 😉.

I’m aware of this issue and looking to solve it in the dev-ts branch.

1reaction
segunadebayocommented, Sep 8, 2019

This is a good idea @kripod and one that Chakra UI can solve.

At the moment, you can can’t compose the Button and Checkbox. You might need to create this component yourself. The basic structure for a walkaround would be:

function RadioButton(props){
  return (
   <PseudoBox role="radio" aria-checked={props.isChecked} aria-disabled={props.isDisabled} tabIndex="0">
        {isChecked ? <RadioIcon/> : <EmptyRadioIcon/>}
       <Box>Button Label </Box>
    </PseudoBox>
    )
}

Then you can style the states of the PseudoBox using _disabled, _checked, etc.

I’ll look into a composable and accessible way to achieve these components. I guess they’ll be called RadioButton and CheckboxButton. They’ll end up being part of the next patch or just a “composition” example in the Radio and Checkbox docs.

Thanks for this suggestion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jetpack Compose: RadioButton
The RadioButton component can be used for creating a single item (selection indicator with label), or multiple items in a group. Let's explore ......
Read more >
Jetpack Compose Components (Part 2) - Better Programming
RadioButton is a Jetpack Compose component to display radio buttons with a Text component to display the label. RadioButton has six parameters in...
Read more >
How to Implement Radio Button in Jetpack Compose?
We can customize the radio button colors using the RadioButtonDefaults object. It has colors() method that returns the color based on the state ......
Read more >
RadioButton - Jetpack Compose Playground
Radio buttons allow users to select one option from a set. Example¶. @Composable fun RadioButtonSample() ...
Read more >
Test Jetpack Compose UI easily with the Role SemanticProperty
Now when testing our Compose UI how do we access the radio button with the ... enum class Role { Button, Checkbox, Switch,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found