Checkbox.vue 4.0 KB

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