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

效果:

相关推荐
FYKJ_20106 小时前
express绿叶横店短剧推荐与影评分享平台34219-计算机课程设计、毕业设计
java·javascript·vue.js·spring boot·后端·课程设计·express
weishuangyun16 小时前
小程序制作平台怎么选?三大平台对比!
大数据·开发语言·javascript
一颗小芹菜的日常6 小时前
vue-crack插件是干什么的
前端·javascript·vue.js
小比特-combat6 小时前
LVGL_1(使用示例讲解父子关系)
开发语言·前端·javascript
IT_陈寒6 小时前
Java线程池的坑把我埋了,踩出来的血泪教训
前端·人工智能·后端
开开心心_Every6 小时前
电脑OCR识别软件支持表格识别图片转Excel
java·开发语言·微信·计算机外设·ocr·intellij-idea·excel
弯_弯6 小时前
企业知识库 RAG 落地:从 Demo 到生产,四条工程链路与参数级自查
android·java·javascript
月夏6 小时前
Node.js 读 Excel 我踩了 3 个坑,最后一个查遍 Stack Overflow 没答案
前端·javascript
亿元程序员6 小时前
PSD都不用了!Codex直接生成Cocos预制体
前端
柳杉6 小时前
Codex + 可视化大屏工作流实践:15 个行业场景的设计产出合集
前端·openai·数据可视化