CustomInput.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <!-- <BasicTable @register="registerTable" /> -->
  3. <div class="wrap">
  4. <div class="content">
  5. <Input :placeholder="placeholder" disabled v-model:value="value" />
  6. <a-button class="mr-2 add-btn" color="success" @click="openAddPop()"> 添加 </a-button>
  7. </div>
  8. <Popup @register="register" @select="select" />
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. import { defineComponent, onUpdated, reactive, nextTick, toRefs } from 'vue';
  13. import { Input } from 'ant-design-vue';
  14. import Popup from './popup.vue';
  15. import { useModal } from '/@/components/Modal';
  16. import { propTypes } from '/@/utils/propTypes';
  17. const props = {
  18. value: propTypes.oneOfType([
  19. propTypes.object,
  20. propTypes.number,
  21. propTypes.string,
  22. propTypes.array,
  23. ]),
  24. placeholder: { type: String, default: '' },
  25. };
  26. interface State {
  27. value: object | string;
  28. placeholder: string;
  29. disabled: boolean;
  30. ids: number[];
  31. }
  32. export default defineComponent({
  33. name: 'CustomInput',
  34. components: { Input, Popup },
  35. props,
  36. emits: ['change'],
  37. setup(props, { emit }) {
  38. const state = reactive<State>({
  39. value: '',
  40. placeholder: props.placeholder,
  41. disabled: false,
  42. ids: [],
  43. });
  44. const [register, { openModal: openPopup }] = useModal();
  45. // 初始化
  46. function init() {
  47. nextTick(() => {
  48. if (props.value) {
  49. const nameArr: string[] = [];
  50. const idArr: number[] = [];
  51. props.value.map((item) => {
  52. if (item.name && item.id) {
  53. nameArr.push(item.name);
  54. idArr.push(item.id);
  55. }
  56. });
  57. state.ids = idArr;
  58. state.value = nameArr.toString();
  59. }
  60. });
  61. }
  62. onUpdated(() => {
  63. init();
  64. });
  65. function openAddPop() {
  66. openPopup(true, { title: state.placeholder, ids: state.ids });
  67. }
  68. function select(data) {
  69. emit('change', data);
  70. }
  71. return {
  72. openAddPop,
  73. select,
  74. openPopup,
  75. register,
  76. ...toRefs(state),
  77. };
  78. },
  79. });
  80. </script>
  81. <style scoped>
  82. .content {
  83. display: flex;
  84. }
  85. .add-btn {
  86. margin-left: 2px;
  87. }
  88. </style>