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

效果:

相关推荐
晚霞的不甘20 小时前
华为云 DevUI 实战开发指南:构建现代化前端应用的最佳实践
前端·华为云
申阳20 小时前
Day 21:03. 基于 SpringBoot4 开发后台管理系统-整合 SpringSecurity 完成登录功能
前端·后端·程序员
嘴平伊之豬20 小时前
对照typescript学习鸿蒙ArkTS
前端·harmonyos
奋斗猿20 小时前
前端实测:RSC不是银弹,但它真的重构了我的技术栈
前端·react.js
Hilaku20 小时前
为什么永远不要相信前端输入?绕过前端验证,只需一个 cURL 命令!
前端·javascript·安全
玄魂20 小时前
VChart 扩展新功能:一行代码解锁数据回归与趋势分析
前端·echarts·数据可视化
AndyGoWei21 小时前
pnpm 是什么,看这篇文章就够了
前端·javascript
zl0_00_021 小时前
isctf2025 部分wp
linux·前端·javascript
西洼工作室21 小时前
移动开发常见问题
前端·css3·web移动开发
同学8079621 小时前
新版本Chrome谷歌浏览器访问本地网络请求跨域无法正常请求
前端·http