CustomInput.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <!-- <BasicTable @register="registerTable" /> -->
  3. <div class="wrap">
  4. <div class="content">
  5. <Input
  6. :class="placeholder === '交易名称' ? 'input-width' : ''"
  7. :placeholder="placeholder"
  8. disabled
  9. v-model:value="value"
  10. />
  11. <a-button
  12. class="mr-2 add-btn"
  13. color="success"
  14. :disabled="disabled"
  15. @click="openAddPop(placeholder, type)"
  16. >
  17. 添加
  18. </a-button>
  19. <Checkbox
  20. class="checkbox"
  21. v-if="placeholder === '交易名称'"
  22. v-model:checked="checked"
  23. @change="onCheckChange"
  24. :disabled="ck_disabled"
  25. >会费</Checkbox
  26. >
  27. </div>
  28. <Popup @register="register" @select="select" />
  29. </div>
  30. </template>
  31. <script lang="ts">
  32. import { defineComponent, onUpdated, reactive, nextTick, toRefs } from 'vue';
  33. import { Input } from 'ant-design-vue';
  34. import { Checkbox } from 'ant-design-vue';
  35. import Popup from './popup.vue';
  36. import { useModal } from '/@/components/Modal';
  37. const props = {
  38. value: { type: Object, default: '' },
  39. placeholder: { type: String, default: '' },
  40. type: { type: Number, default: 1 },
  41. };
  42. interface State {
  43. value: object | string;
  44. placeholder: string;
  45. disabled: boolean;
  46. type: number;
  47. }
  48. export default defineComponent({
  49. name: 'CustomInput',
  50. components: { Input, Checkbox, Popup },
  51. props,
  52. emits: ['change'],
  53. setup(props, { emit }) {
  54. const state = reactive<State>({
  55. value: '',
  56. placeholder: props.placeholder,
  57. disabled: false,
  58. type: 1,
  59. });
  60. const checkbox = reactive({
  61. checked: false,
  62. ck_disabled: false,
  63. });
  64. const [register, { openModal: openPopup }] = useModal();
  65. // 初始化
  66. function init() {
  67. nextTick(() => {
  68. state.type = props.type;
  69. if (props.value.name) {
  70. state.value = props.value.name;
  71. } else if (props.value.url) {
  72. state.value = props.value.url;
  73. } else {
  74. state.value = props.value;
  75. if (props.value === '会费') {
  76. checkbox.checked = true;
  77. } else {
  78. checkbox.checked = false;
  79. }
  80. }
  81. });
  82. }
  83. onUpdated(() => {
  84. init();
  85. if (state.value !== '会费' || !state.disabled) {
  86. if (
  87. state.value !== '' &&
  88. (typeof props.value === 'string' || typeof props.value === 'number')
  89. ) {
  90. state.disabled = true;
  91. checkbox.ck_disabled = true;
  92. } else {
  93. state.disabled = false;
  94. checkbox.ck_disabled = false;
  95. if (state.value === '会费') {
  96. checkbox.checked = true;
  97. state.disabled = true;
  98. }
  99. }
  100. }
  101. if (props.value == '' && state.disabled) {
  102. checkbox.checked = false;
  103. state.disabled = false;
  104. state.value = '';
  105. } else if (props.value === '会费' && state.disabled) {
  106. // checkbox.checked = true;
  107. checkbox.ck_disabled = true;
  108. }
  109. });
  110. function onCheckChange(evt) {
  111. console.log(`evt`, evt);
  112. console.log(`evt.target.checked`, evt.target.checked);
  113. const checked = evt.target.checked;
  114. if (checked) {
  115. state.disabled = true;
  116. emit('change', { id: 0, name: '会费' });
  117. } else {
  118. state.disabled = false;
  119. emit('change', '');
  120. }
  121. }
  122. function openAddPop(title, type) {
  123. openPopup(true, { title, type });
  124. }
  125. function select(data) {
  126. emit('change', data);
  127. }
  128. return {
  129. openAddPop,
  130. onCheckChange,
  131. select,
  132. openPopup,
  133. register,
  134. ...toRefs(state),
  135. ...toRefs(checkbox),
  136. };
  137. },
  138. });
  139. </script>
  140. <style scoped>
  141. .content {
  142. display: flex;
  143. }
  144. .add-btn {
  145. margin-left: 2px;
  146. }
  147. .input-width {
  148. width: 70%;
  149. }
  150. .checkbox {
  151. height: 30px;
  152. }
  153. @media screen and (max-width: 450px) {
  154. .input-width {
  155. width: 39%;
  156. }
  157. }
  158. @media screen and (max-width: 380px) {
  159. .input-width {
  160. width: 35%;
  161. }
  162. }
  163. @media screen and (max-width: 330px) {
  164. .input-width {
  165. width: 23%;
  166. }
  167. }
  168. </style>