覆盖微信原生组件Switch的样式

index.tsx

TypeScript 复制代码
import { FC, memo, PropsWithChildren } from 'react'
import { View, Switch } from '@tarojs/components'
import classNames from 'classnames'
import styles from './index.module.less'
import { AccurateEmsState } from '../../const'

interface EmsProps {
  /** ems 开 | 关 */
  value: AccurateEmsState
  onChange: (value: AccurateEmsState) => void
}

/**
 * Ems
 * @param props 
 * @returns 
 */
const Component: FC<PropsWithChildren<EmsProps>> = ({ value, onChange }) => {
  const getChecked = () => value === AccurateEmsState.On
  return (
    <View>
      <View className={classNames(styles.ems)} >
        <View className={styles.emsText}>{`EMS${getChecked() ? '打开' : '关闭'}`}</View>
        <Switch 
          checked={getChecked()} 
          onChange={(event) => {
            onChange(event.detail.value ? AccurateEmsState.On : AccurateEmsState.Off)
          }}
          color='#39BFAD'
          className={classNames(styles.customSwitch, {
            [styles.customSwitchOff]: !getChecked()
          })}
        />
      </View>

    </View>
  )
}

const Ems = memo(Component)
export default Ems

index.less

css 复制代码
.ems {
  display: flex;
  flex-direction: column;

  .emsText {
    font-size: 12px;
    min-width: 50px;
    width: auto;
  }

  .customSwitch {
    :global {
      .wx-switch-input {
        height: 14px;
        width: 34px;
        background-color: #393B3B;
        border-color: #393B3B;
      }

      /* false的样式 */
      .wx-switch-input::before {
        height: 12px;
        width: 12px;
        background-color: #39BFAD;
      }

      /* true的样式 */
      .wx-switch-input::after {
        height: 12px;
        width: 12px;
      }
    }
  }

  .customSwitchOff {
    :global {
      /* true的样式 */
      .wx-switch-input::after {
        height: 12px;
        width: 12px;
        background-color: #808080;
      }
    }
  }
}

效果:

相关推荐
zhaoyin19942 分钟前
Fiddler弱网实战
前端·测试工具·fiddler
换日线°1 小时前
前端炫酷展开效果
前端·javascript·vue
夏幻灵2 小时前
过来人的经验-前端学习路线
前端
CappuccinoRose2 小时前
React框架学习文档(七)
开发语言·前端·javascript·react.js·前端框架·reactjs·react router
FFF-X2 小时前
前端字符串模糊匹配实现:精准匹配 + Levenshtein 编辑距离兜底
前端
Hi_kenyon3 小时前
Ref和Reactive都是什么时候使用?
前端·javascript·vue.js
止观止3 小时前
深入理解 interface vs type:终结之争
前端·typescript
css趣多多3 小时前
vue环境变量
前端
RFCEO3 小时前
前端编程 课程十五、:CSS核心基础3:文字+段落样式
前端·css·文字+段落样式·css文本样式·美化页面文本内容·演示动画说明·单行文字垂直居中技
摇滚侠3 小时前
【程序员入门系列】jQuery 零基础入门到精通!Jquery 选择器 API
前端·javascript·jquery