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

效果:

相关推荐
SuperEugene2 分钟前
Vue3 配置文件管理:按模块拆分配置,提升配置可维护性|配置驱动开发实战篇
前端·javascript·vue.js·驱动开发
阿凤215 分钟前
后端返回文件二进制流
开发语言·前端·javascript·uniapp
落魄江湖行8 分钟前
进阶篇四 Nuxt4 Server Routes:写后端 API
前端·vue.js·typescript·nuxt4
萧行之8 分钟前
解决Microsoft Edge/Hotmail登录报错(15/25/2603、0x80190001)
前端·microsoft·edge
Eiceblue20 分钟前
C# 删除 PDF 页面:单页 / 多页批量删除技巧
前端·pdf·c#
悟空瞎说20 分钟前
从isMounted到跨页面状态:高级前端如何优雅解决订单场景的“幽灵陷阱”(附React/Vue完整代码)
前端·javascript
C_fashionCat22 分钟前
【2026面试题】前端实际场景去考察原理
前端·vue.js·面试
落魄江湖行23 分钟前
进阶篇三 Nuxt4 Nitro 引擎:Nuxt 的服务端核心
前端·vue.js·typescript·nuxt4
sheeta199825 分钟前
TypeScript references 配置与 emit 要求详解
javascript·ubuntu·typescript
一壶纱25 分钟前
Element Plus 主题构建方案
前端·vue.js