gpt4 book ai didi

css - 初学者 : Styling ReactJS Checkbox using CSS Pseudo Elements

转载 作者:行者123 更新时间:2023-11-28 16:08:22 25 4
gpt4 key购买 nike

我正在尝试使用 CSS 设置 reactjs 复选框组件的样式。它使用伪元素 :after 和 :before。

在 html 和 css 上模拟它并且成功了! Fiddle Link

input[type="checkbox"]#checkbox + label::before {
content: "";
display: inline-block;
vertical-align: -25%;
height: 2ex;
width: 2ex;
background-color: white;
border: 1px solid #c3c4c6;
border-radius: 4px;
margin-right: 0.5em;
}

input[type="checkbox"]#checkbox:checked + label::after {
content: '';
position: absolute;
width: 1.2ex;
height: 0.4ex;
background: rgba(0, 0, 0, 0);
top: 0.5ex;
left: 0.4ex;
border: 3px solid #60cd18;
border-top: none;
border-right: none;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
transform: rotate(-45deg);
}

但是当我尝试在显示复选框的 reactjs 组件上实现它时,也许 :after 不起作用。 Fiddle ReactJS Component Link

我应该如何在 ReactJS 复选框组件上实现相同的样式?

最佳答案

有几种方法可以在 React 上使用伪元素,例如 Radium,但据我所知,它不支持 :after/:before,而是根据 thisthis ,它建议创建一个元素来代替伪元素。我没有找到这方面的任何 React 示例,我认为伪元素的可用性是为了避免创建不必要的 DOM 元素。

由于所有这些限制,我目前在 React 中自定义复选框以使其适用于大多数浏览器的解决方案是创建一个元素,其行为、外观和感觉像一个复选框(冒名顶替者),但本身不是一个复选框(输入类型=“复选框”)。我通过使用 ReactJS 触发 span 元素和超赞字体图标的可见性来实现这一点。

Example

/*  html   */
<div id="container">
</div>

/* JS */
var Checkbox = React.createClass({
getDefaultProps: function() {
return {
value: true,
name: '',
borderWidth: '1px',
borderStyle: 'solid',
borderColor: '#c3c4c6',
borderRadius: '4px',
checkColor: '#60cd18',
height: '1px',
width: '',
namePaddingLeft: '10px',
namePaddingRight: ''
};
},
render: function() {
var style = {
boxStyle: {
borderWidth: this.props.borderWidth,
borderStyle: this.props.borderStyle,
borderColor: this.props.borderColor,
borderRadius: this.props.borderRadius,
paddingLeft: this.props.width,
paddingRight: this.props.width,
paddingTop: this.props.height,
paddingBottom: this.props.height
},
show: {
visibility: 'visible',
color: this.props.checkColor
},
hidden: {
visibility: 'hidden',
color: this.props.checkColor
},
name: {
paddingLeft: this.props.namePaddingLeft,
paddingRight: this.props.namePaddingRight
}
};
return (
<div>
<span style={style.boxStyle}>
<i className="fa fa-check fa-lg" style={(this.props.value) ? style.show : style.hidden}></i>
</span>
<span style={style.name}>{this.props.name}</span>
</div>
);
}
});

var WrapperCheckbox = React.createClass({
getInitialState: function(){
return {value: false}
},
handleClick: function(){
console.log('Click Fires!');
this.setState({value: !this.state.value});
},
render: function(){
return (
<div onClick={this.handleClick}>
<Checkbox name='Reserve Guarantee' value={this.state.value}/>
</div>
);
}
});

React.render(<WrapperCheckbox />, document.getElementById('container'));

关于css - 初学者 : Styling ReactJS Checkbox using CSS Pseudo Elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38757493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com