猫妈狗爸伴宠师小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.5 KiB

3 months ago
  1. <template>
  2. <up-form label-position="left" :model="formData" :rules="rules" ref="dFormRef">
  3. <div class="form-item" v-for="item in list" :key="item.key">
  4. <up-form-item :label="item.label" :prop="item.key" @click="item.type==='select'&&open(item)"
  5. :labelStyle="{fontSize:'28rpx'}" :labelWidth="labelWidth" class="item">
  6. <template v-if="item.type==='input'">
  7. <div class="flex-rowr">
  8. <up-input inputAlign="right" v-model="formData[item.key]" :placeholder="item.placeholder"
  9. border="none" fontSize='28rpx'></up-input><text
  10. class="ml10">{{item.unit?item.unit:""}}</text>
  11. </div>
  12. </template>
  13. <template v-if="item.type==='select'">
  14. <up-input inputAlign="right" v-model="formData[item.key]" disabled disabledColor="#ffffff"
  15. :placeholder="item.placeholder" border="none"></up-input>
  16. <template #right>
  17. <up-icon name="arrow-right"></up-icon>
  18. </template>
  19. </template>
  20. <template v-if="item.type==='radio'">
  21. <up-radio-group v-model="formData[item.key]" placement="column">
  22. <div class="flex-rowr">
  23. <up-radio class="mr16" activeColor="#FFBF60" :customStyle="{marginBottom: '8px'}"
  24. v-for="key in item.options" :key="key.name" :label="key.name" :name="key.name">
  25. </up-radio>
  26. </div>
  27. </up-radio-group>
  28. </template>
  29. <template v-if="item.type==='textarea'">
  30. <up-textarea v-model="formData[item.key]" :placeholder="item.placeholder" count autoHeight
  31. :maxlength="item.maxlength"></up-textarea>
  32. </template>
  33. <template v-if="item.type==='upload'">
  34. <up-upload :fileList="item.fileList" @afterRead="afterRead" @delete="deletePic" name="1"
  35. :multiple="item.multiple" :maxCount="item.maxCount" :accept='item.accept'
  36. :previewFullImage="item.previewFullImage"></up-upload>
  37. </template>
  38. </up-form-item>
  39. </div>
  40. <up-button @click="handleSubmit" class="mt20" type="warning" v-if="isFooter">提交</up-button>
  41. </up-form>
  42. <up-action-sheet :show="sheet.show" :actions="sheet.actions" :title="sheet.title" :description="sheet.description"
  43. @close="sheetClose" @select="handleSelect">
  44. </up-action-sheet>
  45. </template>
  46. <script setup>
  47. import {
  48. reactive,
  49. ref,
  50. computed
  51. } from "vue"
  52. const {
  53. list,
  54. labelWidth
  55. } = defineProps({
  56. list: {
  57. type: Array,
  58. default: () => []
  59. },
  60. labelWidth: {
  61. type: String,
  62. default: "200rpx"
  63. },
  64. isFooter: true
  65. })
  66. const emit = defineEmits(['submit'])
  67. const formData = reactive({
  68. name: ""
  69. })
  70. const sheet = reactive({
  71. show: false,
  72. actions: [],
  73. title: "",
  74. description: ""
  75. })
  76. // 设置校验
  77. const rules = computed(() => {
  78. const obj = {}
  79. list.forEach(item => {
  80. if (item.rule && item.rule.length > 0)
  81. obj[item.key] = [...item.rule]
  82. })
  83. return obj
  84. })
  85. // 开启弹框
  86. const open = (item) => {
  87. sheet.show = true
  88. sheet.actions = item.options
  89. sheet.title = item.placeholder
  90. sheet.description = item.description || ""
  91. }
  92. // 关闭弹框
  93. const sheetClose = () => {
  94. sheet.show = false
  95. }
  96. // 选择
  97. const handleSelect = (val) => {
  98. console.log(val)
  99. }
  100. // 上传
  101. const afterRead = () => {}
  102. // 删除上传文件
  103. const deletePic = () => {}
  104. const dFormRef = ref(null)
  105. // 提交
  106. const handleSubmit = () => {
  107. dFormRef.value.validate().then(valid => {
  108. if (valid) {
  109. submit(formData)
  110. }
  111. })
  112. }
  113. </script>
  114. <style>
  115. .form-item {
  116. height: 90rpx;
  117. padding-top: 10rpx;
  118. width: 100%;
  119. border-bottom: 1rpx solid #f5f5f5;
  120. }
  121. .flex-rowr {
  122. display: flex;
  123. justify-content: flex-end;
  124. align-items: center;
  125. }
  126. .ml10 {
  127. margin-left: 10rpx;
  128. }
  129. </style>