Ad
React Radio Button: Checked Cycle Not Aligned
I tried to convert this radio button to style component.
You can that the checked cycle is off.
App.js
import React, {useState} from "react";
import RadioButton from './RadioButton';
// https://codepen.io/satya164/pen/qdgYaO
function App() {
const [value, setValue] = useState('');
//test
console.log('value', value);
return <div className="App">
<RadioButton
checked={value === 'test1' ? true : false}
onChange={() => {setValue('test1')}}
value={'test1'}
name={'test'}
label={'test radio1'}
></RadioButton>
<RadioButton
checked={value === 'test2' ? true : false}
onChange={() => {setValue('test2')}}
value={'test2'}
name={'test'}
label={'test radio2'}
></RadioButton>
</div>;
}
export default App;
Radio button.js
import React from "react";
import {Label, Span, Input} from './index.style';
const RadioButton = ({checked, value, name, onChange, label}) => {
return (
<div>
<Label>
<Input
type="radio"
checked={checked}
value={value}
name={name}
onChange={onChange}
/>
<Span>{label}</Span>
</Label>
</div>
);
}
export default RadioButton;
style.js
import styled from 'styled-components';
export const Label = styled.label`
display: block;
//padding: .5em;
cursor: pointer;
`;
export const Span = styled.span`
&:before {
content: "";
display: inline-block;
vertical-align: -.25em;
height: 1em;
width: 1em;
border-radius: 50%;
border: 2px solid rgba(0, 0, 0, 0.5);
//margin-right: .5em;
}
`;
export const Input = styled.input`
&:checked + ${Span} {
border-color: #e48;
// bg img cycle
background-image: radial-gradient(
circle closest-side,
#e48 0%,
#e48 50%,
transparent 50%,
transparent 100%);
}
display: none;
`;
full code:
Ad
Answer
Check that : https://codesandbox.io/embed/sparkling-paper-3skk1?fontsize=14
It should fix your problem.
You just missed &:before
inside Input.
export const Input = styled.input`
&:checked + ${Span} {
&:before { // You missed before
background-image: radial-gradient(
circle closest-side,
rgba(0, 0, 0, 0.5) 0%,
rgba(238, 68, 136, 1) 50%,
transparent 50%,
transparent 100%
);
border-color: #e48;
}
}
display: none;
`;
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad