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

效果:

相关推荐
一丝晨光17 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
Front思19 分钟前
vue使用高德地图
javascript·vue.js·ecmascript
zqx_71 小时前
随记 前端框架React的初步认识
前端·react.js·前端框架
惜.己1 小时前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5
什么鬼昵称2 小时前
Pikachu-csrf-CSRF(get)
前端·csrf
长天一色2 小时前
【ECMAScript 从入门到进阶教程】第三部分:高级主题(高级函数与范式,元编程,正则表达式,性能优化)
服务器·开发语言·前端·javascript·性能优化·ecmascript
NiNg_1_2342 小时前
npm、yarn、pnpm之间的区别
前端·npm·node.js
秋殇与星河2 小时前
CSS总结
前端·css
NiNg_1_2342 小时前
Vue3 Pinia持久化存储
开发语言·javascript·ecmascript
读心悦2 小时前
如何在 Axios 中封装事件中心EventEmitter
javascript·http