React + TypeScript + Antd 使用单选框Radio实现纯文字点击效果(已封装好组件)--传值即用

本文是关于使用react、ts和antd组件实现的一个效果,当前组件已封装好,直接传值就可以使用了,基本原理是把antd组件里面的radio的组件进行一个修改,直接把原来的样式去掉,让他成纯文字的,点击就变颜色。也可以在此基础上进行二次开发,有需自取!

效果如图所示:

代码如下:

index.tsx

// RadioButtonGroup.tsx
import React, { useState } from 'react';
import { Radio } from 'antd';
import './index.scss';
/* 
  Radio封装组件:
    必须传的参数:options,defaultValue,size
    可选参数:onSelect(),activeColor,buttonWidth

  参数的含义:
    options:选项,包含label和value属性,用法 options = {option},option =[{label:全部; value:'全部'}]
    defaultValue: 默认值,加载时展示什么,
    size:大小,只允许'small' || 'default' || 'large'
    onSelect():回调函数,用于处理单选按钮被选中时的逻辑。该函数接收一个参数 value,表示被选中的按钮的值。
                在 handleButtonClick 函数中,当单选按钮被点击时,会调用 onSelect 函数,并传递当前按钮的值 value 给它。
                这样,父组件可以通过传递一个 onSelect 回调函数来监听单选按钮的选中事件,并在选中时执行特定的业务逻辑。
    activeColor:激活时的颜色,默认#3B89FF
    buttonWidth:宽度,默认120px
 */

interface RadioButtonGroupProps {
  options: { label: string; value: string }[];
  defaultValue: string;
  size: string; // 字符串字面量联合类型
  onSelect?: (value: any) => void;
  activeColor?: string;
  buttonWidth?: string;
}

const RadioButtonGroup: React.FC<RadioButtonGroupProps> = ({
  options,
  defaultValue,
  onSelect,
  activeColor = '#3B89FF ',
  buttonWidth = '120px',
}) => {
  const [activeButton, setActiveButton] = useState(defaultValue);

  const handleButtonClick = (value: string) => {
    setActiveButton(value);
    if (onSelect) {
      onSelect(value);
    }
  };

  const getButtonStyle = (value: string) => ({
    border: 'none',
    background: '#fff',
    color: activeButton === value ? activeColor : 'black',
    width: buttonWidth,
    outline: 'none',
    boxShadow: 'none',
    borderRadius: '0',
    borderRight: 'none',
  });

  return (
    <div>
      <Radio.Group defaultValue={defaultValue} size={'small' || 'default' || 'large'} style={{ border: 'none' }}>
        {options.map((option) => (
          <Radio.Button
            key={option.value}
            value={option.value}
            style={getButtonStyle(option.value)}
            onClick={() => handleButtonClick(option.value)}
          >
            {option.label}
          </Radio.Button>
        ))}
      </Radio.Group>
    </div>
  );
};

export default RadioButtonGroup;

index.scss

.ant-radio-button-wrapper:not(:first-child)::before {
  background-color: transparent;
}

.skin-blue .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)::before {
  background: "none" !important;
  opacity: 0;
}

.ant-radio-button-wrapper::before {
  background-color: "none" !important;
  padding: 0 !important;
}

.skin-blue .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)::before {
  background: "none" !important;
  opacity: 0 !important;
}

.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled) {

   border-color: none !important;
}
.skin-blue .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)::before {
  background: none !important;
}
.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)::before {
  background-color:  none !important;
}

.ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)::before {
  background-color: none !important;
}
.skin-blue .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled) {
  background-color: none !important;
  border-color:  none !important;

}

文件路径如下:

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React-barrage是一个React组件库,提供了在页面上展示弹幕的功能。在使用react-barrage之前,你需要先在项目中安装它: ``` npm install react-barrage --save ``` 然后在你的组件使用它: ```tsx import React, { useCallback, useEffect, useState } from 'react'; import { Barrage, BarrageItem } from 'react-barrage'; interface Message { text: string; time: number; } const BarrageComponent: React.FC = () => { const [messages, setMessages] = useState<Message[]>([]); useEffect(() => { // 模拟异步获取弹幕数据 const timer = setInterval(() => { const now = Date.now(); setMessages(prevMessages => [ ...prevMessages, { text: `弹幕${now}`, time: now } ]); }, 1000); return () => clearInterval(timer); }, []); const renderItem = useCallback(({ text }) => { return <div>{text}</div>; }, []); return ( <Barrage> {messages.map(message => ( <BarrageItem key={message.time} text={message.text} render={renderItem} /> ))} </Barrage> ); }; export default BarrageComponent; ``` 这里的Barrage组件是弹幕的容器,而BarrageItem则是每一条弹幕。你需要将需要展示的弹幕数据传递给BarrageItem,并通过render函数来渲染每一条弹幕的内容。在上面的例子中,我们使用了useState来存储弹幕数据,并使用useEffect模拟异步获取数据的过程。当组件挂载后,我们每隔一秒钟就会向弹幕数据中添加一条新的弹幕数据,并将其展示在页面上。 需要注意的是,由于react-barrage是基于canvas实现的,因此在使用时需要注意一些性能问题。如果弹幕太多或太长,可能会导致页面卡顿或者崩溃。因此,在使用时需要根据实际情况来控制弹幕的数量和长度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值