覆盖微信原生组件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;
      }
    }
  }
}

效果:

相关推荐
巴巴_羊4 分钟前
AJAX 使用 和 HTTP
前端·http·ajax
刺客-Andy11 分钟前
React 第四十一节Router 中 useActionData 使用方法案例以及注意事项
前端·react.js·前端框架
岁岁岁平安16 分钟前
Vue3学习(组合式API——reactive()和ref()函数详解)
前端·javascript·vue.js·学习·vue3·reactive·ref
肠胃炎18 分钟前
React事件机制
前端·javascript·react.js
CUIYD_198925 分钟前
javascript —— ! 和 !! 的区别与作用
前端·javascript·vue.js
帅帅哥的兜兜2 小时前
next.js实现项目搭建
前端·react.js·next.js
筱歌儿2 小时前
css 左右布局
前端·css
GISer_Jing2 小时前
编译原理AST&以Babel为例进行解读、Webpack中自定义loader与plugin
前端·webpack·node.js
GISer_Jing2 小时前
Webpack中Compiler详解以及自定义loader和plugin详解
前端·webpack·node.js
浩~~3 小时前
CSS常用选择器
前端·css