Checkbox.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="checkbox-component">
  3. <input
  4. :id="id"
  5. type="checkbox"
  6. :name="name"
  7. :checked="myValue"
  8. :class="className"
  9. :required="required"
  10. :disabled="disabled"
  11. @change="onChange"
  12. />
  13. <label :for="id">
  14. <slot name="input-box">
  15. <span class="input-box">
  16. <svg
  17. class="input-box-tick"
  18. viewBox="0 0 16 16"
  19. >
  20. <path
  21. fill="none"
  22. d="M1.7,7.0l3.8,3.4l9-8.8"
  23. />
  24. </svg>
  25. </span>
  26. </slot>
  27. <slot />
  28. </label>
  29. </div>
  30. </template>
  31. <script>
  32. import Uuid from '../../common/Uuid.js';
  33. export default {
  34. // eslint-disable-next-line
  35. name: 'Checkbox',
  36. props: {
  37. id: {
  38. type: String,
  39. default: function () {
  40. return 'checkbox-id-' + Uuid.createUUID();
  41. },
  42. },
  43. name: {
  44. type: String,
  45. default: null,
  46. },
  47. value: {
  48. type: Boolean,
  49. default: undefined,
  50. },
  51. className: {
  52. type: String,
  53. default: null,
  54. },
  55. required: {
  56. type: Boolean,
  57. default: false,
  58. },
  59. disabled: {
  60. type: Boolean,
  61. default: false,
  62. },
  63. },
  64. emits: ['update:value'],
  65. data: function () {
  66. return {
  67. 'myValue': false,
  68. };
  69. },
  70. computed: {
  71. },
  72. watch: {
  73. value: {
  74. handler(newValue, oldValue) {
  75. this.myValue = newValue;
  76. },
  77. immediate: true,
  78. },
  79. },
  80. methods: {
  81. onChange() {
  82. // this.myValue = !this.myValue;
  83. this.$emit('update:value', !this.myValue);
  84. },
  85. },
  86. };
  87. </script>
  88. <style scoped>
  89. /**
  90. .checkbox-component {
  91. > input {
  92. opacity: 0;
  93. position: absolute;
  94. + label > .input-box {
  95. display: inline-block;
  96. border: 1px solid #000;
  97. border-radius: 14%;
  98. margin: 0;
  99. padding: 0;
  100. width: 1em;
  101. height: 1em;
  102. background: #fff;
  103. overflow: hidden;
  104. vertical-align: -5%;
  105. user-select: none;
  106. > .input-box-tick {
  107. width: 100%;
  108. height: 100%;
  109. > path {
  110. opacity: 0;
  111. stroke: #000;
  112. stroke-width: 2.3px;
  113. stroke-dashoffset: 20;
  114. stroke-dasharray: 20;
  115. transition: stroke-dashoffset 0.15s ease-in;
  116. }
  117. }
  118. }
  119. &:checked + label > .input-box > .input-box-tick > path {
  120. opacity: 1;
  121. stroke-dashoffset: 0;
  122. }
  123. &:focus + label > .input-box {
  124. box-shadow: 0 0 2px 3px rgba(115, 185, 255, 0.69);
  125. }
  126. &:disabled + label {
  127. opacity: 0.7;
  128. }
  129. }
  130. }
  131. */
  132. .checkbox-component > input {
  133. opacity: 0;
  134. position: absolute;
  135. }
  136. .checkbox-component > input + label > .input-box {
  137. display: inline-block;
  138. border: 1px solid #000;
  139. border-radius: 14%;
  140. margin: 0;
  141. padding: 0;
  142. width: 1em;
  143. height: 1em;
  144. background: #fff;
  145. overflow: hidden;
  146. vertical-align: -5%;
  147. user-select: none;
  148. }
  149. .checkbox-component > input + label > .input-box > .input-box-tick {
  150. width: 100%;
  151. height: 100%;
  152. }
  153. .checkbox-component > input + label > .input-box > .input-box-tick > path {
  154. opacity: 0;
  155. stroke: #000;
  156. stroke-width: 2.3px;
  157. stroke-dashoffset: 20;
  158. stroke-dasharray: 20;
  159. transition: stroke-dashoffset 0.15s ease-in;
  160. }
  161. .checkbox-component
  162. > input:checked
  163. + label
  164. > .input-box
  165. > .input-box-tick
  166. > path {
  167. opacity: 1;
  168. stroke-dashoffset: 0;
  169. }
  170. .checkbox-component > input:focus + label > .input-box {
  171. box-shadow: 0 0 2px 3px rgba(115, 185, 255, 0.69);
  172. }
  173. .checkbox-component > input:disabled + label {
  174. opacity: 0.7;
  175. }
  176. </style>