CSS实现单选按钮

import React from 'react'
import PropTypes from 'prop-types'
import CX from 'classnames'
import _ from 'lodash'

import './index.less'

function RadioButton(props) {
  const {
    style, title, isChecked, onClick,
  } = props

  const wrapperStyle = _.assign({}, style)

  return (
    <div
      className="checkbox-wrap"
      style={wrapperStyle}
      onClick={onClick}
      role="button"
      tabIndex={0}
    >
      <span
        className={CX({
          checkbox: true,
          checked: isChecked === true,
        })}
      />
      <span className="tip-text">{title}</span>
    </div>
  )
}

RadioButton.propTypes = {
  style: PropTypes.object,
  title: PropTypes.string,
  isChecked: PropTypes.bool,
  onClick: PropTypes.func,
}

RadioButton.defaultProps = {
  style: {},
  title: '',
  isChecked: false,
  onClick: _.noop,
}

export default RadioButton

下面是组件样式

.checkbox-wrap {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 3px 0;
  margin-right: 24px;
  cursor: pointer;

  .checkbox {
    display: inline-block;
    box-sizing: border-box;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    border: 2px solid rgba(79, 159, 255, 1);
    position: relative;

    &.checked {
      &::before {
        content: '';
        display: block;
        width: 4px;
        height: 4px;
        border-radius: 50%;
        background-color: #56afff;
        position: absolute;
        top: 1px;
        left: 1px;
      }
    }
  }
  .tip-text {
    opacity: 0.4;
    line-height: 18px;
    margin-left: 3px;
  }
}

猜你喜欢

转载自www.cnblogs.com/chenbeibei520/p/10898736.html