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

效果:

相关推荐
Fairy要carry11 分钟前
面试-Agent Loop
前端·chrome
Surmon2 小时前
基于 Cloudflare 生态的 AI Agent 实现
前端·人工智能·架构
六月June June7 小时前
自定义调色盘组件
前端·javascript·调色盘
SY_FC7 小时前
实现一个父组件引入了子组件,跳转到其他页面,其他页面返回回来重新加载子组件函数
java·前端·javascript
糟糕好吃7 小时前
我让 AI 操作网页之后,开始不想点按钮了
前端·javascript·后端
陈天伟教授8 小时前
人工智能应用- 天文学家的助手:08. 星系定位与分类
前端·javascript·数据库·人工智能·机器学习
VaJoy8 小时前
给到夯!前端工具链新标杆 Vite Plus 初探
前端·vite
颜酱9 小时前
BFS 与并查集实战总结:从基础框架到刷题落地
javascript·后端·算法
小彭努力中9 小时前
191.Vue3 + OpenLayers 实战:可控化版权信息(Attribution)详解与完整示例
前端·javascript·vue.js·#地图开发·#cesium
奇舞精选9 小时前
用去年 github 最火的 n8n 快速实现自动化推送工具
前端·agent