当前位置:   article > 正文

覆盖微信原生组件Switch的样式

覆盖微信原生组件Switch的样式

index.tsx

  1. import { FC, memo, PropsWithChildren } from 'react'
  2. import { View, Switch } from '@tarojs/components'
  3. import classNames from 'classnames'
  4. import styles from './index.module.less'
  5. import { AccurateEmsState } from '../../const'
  6. interface EmsProps {
  7. /** ems 开 | 关 */
  8. value: AccurateEmsState
  9. onChange: (value: AccurateEmsState) => void
  10. }
  11. /**
  12. * Ems
  13. * @param props
  14. * @returns
  15. */
  16. const Component: FC<PropsWithChildren<EmsProps>> = ({ value, onChange }) => {
  17. const getChecked = () => value === AccurateEmsState.On
  18. return (
  19. <View>
  20. <View className={classNames(styles.ems)} >
  21. <View className={styles.emsText}>{`EMS${getChecked() ? '打开' : '关闭'}`}</View>
  22. <Switch
  23. checked={getChecked()}
  24. onChange={(event) => {
  25. onChange(event.detail.value ? AccurateEmsState.On : AccurateEmsState.Off)
  26. }}
  27. color='#39BFAD'
  28. className={classNames(styles.customSwitch, {
  29. [styles.customSwitchOff]: !getChecked()
  30. })}
  31. />
  32. </View>
  33. </View>
  34. )
  35. }
  36. const Ems = memo(Component)
  37. export default Ems

index.less

  1. .ems {
  2. display: flex;
  3. flex-direction: column;
  4. .emsText {
  5. font-size: 12px;
  6. min-width: 50px;
  7. width: auto;
  8. }
  9. .customSwitch {
  10. :global {
  11. .wx-switch-input {
  12. height: 14px;
  13. width: 34px;
  14. background-color: #393B3B;
  15. border-color: #393B3B;
  16. }
  17. /* false的样式 */
  18. .wx-switch-input::before {
  19. height: 12px;
  20. width: 12px;
  21. background-color: #39BFAD;
  22. }
  23. /* true的样式 */
  24. .wx-switch-input::after {
  25. height: 12px;
  26. width: 12px;
  27. }
  28. }
  29. }
  30. .customSwitchOff {
  31. :global {
  32. /* true的样式 */
  33. .wx-switch-input::after {
  34. height: 12px;
  35. width: 12px;
  36. background-color: #808080;
  37. }
  38. }
  39. }
  40. }

效果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/430580
推荐阅读
相关标签
  

闽ICP备14008679号