index.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view
  3. @click="onClick"
  4. :style="style"
  5. class="nut-switch"
  6. :class="[isOpen ? 'switch-open' : 'switch-close']"
  7. >
  8. <view class="switch-button">
  9. <view v-show="!isOpen" class="close-line"></view>
  10. </view>
  11. </view>
  12. </template>
  13. <script lang="ts">
  14. import { toRefs, computed, reactive, onMounted } from 'vue';
  15. import { createComponent } from '@/utils/create';
  16. const { componentName, create } = createComponent('switch');
  17. export default create({
  18. props: {
  19. status: {
  20. type: Boolean,
  21. default: true
  22. },
  23. activeColor: {
  24. type: String,
  25. default: ''
  26. },
  27. inactiveColor: {
  28. type: String,
  29. default: ''
  30. }
  31. },
  32. setup(props, { emit }) {
  33. const { status, activeColor, inactiveColor } = toRefs(props);
  34. const actColor = activeColor.value;
  35. const inaColor = inactiveColor.value;
  36. const response = reactive({
  37. isOpen: status.value ? status.value : true,
  38. style: {}
  39. });
  40. const styleCheck = status => {
  41. if (status) {
  42. if (actColor) {
  43. response.style = {
  44. backgroundColor: `${actColor}`
  45. };
  46. } else {
  47. response.style = {
  48. backgroundColor: 'rgb(250,63,25,1)'
  49. };
  50. }
  51. }
  52. if (!status) {
  53. if (inaColor) {
  54. response.style = {
  55. backgroundColor: `${inaColor}`
  56. };
  57. } else {
  58. response.style = {
  59. backgroundColor: 'rgba(235,235,235,1)'
  60. };
  61. }
  62. }
  63. };
  64. styleCheck(status.value);
  65. const onClick = () => {
  66. response.isOpen = !response.isOpen;
  67. styleCheck(response.isOpen);
  68. emit('switch-change', event, response.isOpen);
  69. };
  70. return {
  71. ...toRefs(response),
  72. onClick
  73. };
  74. }
  75. });
  76. </script>
  77. <style lang="scss">
  78. @import 'index.scss';
  79. </style>