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. console.log('---------------set---up----------------');
  55. const state = reactive<State>({
  56. value: '',
  57. placeholder: props.placeholder,
  58. disabled: false,
  59. type: 1,
  60. });
  61. const checkbox = reactive({
  62. checked: false,
  63. ck_disabled: false,
  64. });
  65. const [register, { openModal: openPopup }] = useModal();
  66. // 初始化
  67. function init() {
  68. nextTick(() => {
  69. state.type = props.type;
  70. if (props.value.name) {
  71. state.value = props.value.name;
  72. } else if (props.value.url) {
  73. state.value = props.value.url;
  74. } else {
  75. state.value = props.value;
  76. if (props.value === '会费') {
  77. checkbox.checked = true;
  78. } else {
  79. checkbox.checked = false;
  80. }
  81. }
  82. });
  83. }
  84. onUpdated(() => {
  85. init();
  86. if (state.value !== '会费' || !state.disabled) {
  87. if (
  88. state.value !== '' &&
  89. (typeof props.value === 'string' || typeof props.value === 'number')
  90. ) {
  91. state.disabled = true;
  92. checkbox.ck_disabled = true;
  93. } else {
  94. state.disabled = false;
  95. checkbox.ck_disabled = false;
  96. if (state.value === '会费') {
  97. checkbox.checked = true;
  98. state.disabled = true;
  99. }
  100. }
  101. }
  102. if (props.value == '' && state.disabled) {
  103. checkbox.checked = false;
  104. state.disabled = false;
  105. state.value = '';
  106. } else if (props.value === '会费' && state.disabled) {
  107. // checkbox.checked = true;
  108. checkbox.ck_disabled = true;
  109. }
  110. });
  111. function onCheckChange(evt) {
  112. console.log(`evt`, evt);
  113. console.log(`evt.target.checked`, evt.target.checked);
  114. const checked = evt.target.checked;
  115. if (checked) {
  116. state.disabled = true;
  117. emit('change', { id: 0, name: '会费' });
  118. } else {
  119. state.disabled = false;
  120. emit('change', '');
  121. }
  122. }
  123. function openAddPop(title, type) {
  124. openPopup(true, { title, type });
  125. }
  126. function select(data) {
  127. emit('change', data);
  128. }
  129. return {
  130. openAddPop,
  131. onCheckChange,
  132. select,
  133. openPopup,
  134. register,
  135. ...toRefs(state),
  136. ...toRefs(checkbox),
  137. };
  138. },
  139. });
  140. </script>
  141. <style scoped>
  142. .content {
  143. display: flex;
  144. }
  145. .add-btn {
  146. margin-left: 2px;
  147. }
  148. .input-width {
  149. width: 70%;
  150. }
  151. .checkbox {
  152. height: 30px;
  153. }
  154. @media screen and (max-width: 450px) {
  155. .input-width {
  156. width: 39%;
  157. }
  158. }
  159. @media screen and (max-width: 380px) {
  160. .input-width {
  161. width: 35%;
  162. }
  163. }
  164. @media screen and (max-width: 330px) {
  165. .input-width {
  166. width: 23%;
  167. }
  168. }
  169. </style>