猫妈狗爸伴宠师小程序前端代码
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.

157 lines
3.9 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month 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-else-if="item.type==='select'">
  14. <up-input inputAlign="right" :value="getDesc(formData[item.key], item.options)" 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-else-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="option in item.options" :key="option.value" :label="option.name" :name="option.value">
  25. </up-radio>
  26. </div>
  27. </up-radio-group>
  28. </template>
  29. <template v-else-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-else-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. <slot :name="item.key"></slot>
  40. </div>
  41. <up-button @click="handleSubmit" class="mt20" type="warning" v-if="isFooter">提交</up-button>
  42. </up-form>
  43. <up-action-sheet :show="sheet.show" :actions="sheet.actions" :title="sheet.title" :description="sheet.description"
  44. @close="sheetClose" @select="handleSelect">
  45. </up-action-sheet>
  46. </template>
  47. <script setup>
  48. import {
  49. reactive,
  50. ref,
  51. computed,
  52. watch,
  53. } from "vue"
  54. const {
  55. list,
  56. labelWidth
  57. } = defineProps({
  58. list: {
  59. type: Array,
  60. default: () => []
  61. },
  62. labelWidth: {
  63. type: String,
  64. default: "200rpx"
  65. },
  66. isFooter: true
  67. })
  68. const emit = defineEmits(['submit', 'input'])
  69. const formData = reactive({
  70. name: ""
  71. })
  72. watch(formData, (val) => {
  73. emit('input', val)
  74. })
  75. const sheet = reactive({
  76. show: false,
  77. key: null,
  78. actions: [],
  79. title: "",
  80. description: ""
  81. })
  82. // 设置校验
  83. const rules = computed(() => {
  84. const obj = {}
  85. list.forEach(item => {
  86. if (item.rule && item.rule.length > 0)
  87. obj[item.key] = [...item.rule]
  88. })
  89. return obj
  90. })
  91. // 开启弹框
  92. const open = (item) => {
  93. sheet.show = true
  94. sheet.key = item.key
  95. sheet.actions = item.options
  96. sheet.title = item.placeholder
  97. sheet.description = item.description || ""
  98. }
  99. // 关闭弹框
  100. const sheetClose = () => {
  101. sheet.show = false
  102. }
  103. // 选择
  104. const handleSelect = (val) => {
  105. formData[sheet.key] = val?.value
  106. }
  107. const getDesc = (val, options) => {
  108. return options.find(item => item.value === val)?.name
  109. }
  110. // 上传
  111. const afterRead = () => {}
  112. // 删除上传文件
  113. const deletePic = () => {}
  114. const dFormRef = ref(null)
  115. // 提交
  116. const handleSubmit = () => {
  117. dFormRef.value.validate().then(valid => {
  118. if (valid) {
  119. submit(formData)
  120. }
  121. })
  122. }
  123. const validate = () => {
  124. return dFormRef.value.validate()
  125. }
  126. defineExpose({ validate })
  127. </script>
  128. <style>
  129. .form-item {
  130. min-height: 90rpx;
  131. padding-top: 10rpx;
  132. width: 100%;
  133. border-bottom: 1rpx solid #f5f5f5;
  134. }
  135. .flex-rowr {
  136. display: flex;
  137. justify-content: flex-end;
  138. align-items: center;
  139. }
  140. .ml10 {
  141. margin-left: 10rpx;
  142. }
  143. </style>