cl-form-item.uvue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view class="cl-form-item" :class="[pt.className]">
  3. <view class="cl-form-item__inner" :class="[`is-${labelPosition}`, pt.inner?.className]">
  4. <view
  5. class="cl-form-item__label"
  6. :class="[`is-${labelPosition}`, pt.label?.className]"
  7. :style="{
  8. width: labelPosition != 'top' ? labelWidth : 'auto'
  9. }"
  10. v-if="label != ''"
  11. >
  12. <cl-text>{{ label }}</cl-text>
  13. <cl-text
  14. color="error"
  15. :pt="{
  16. className: 'ml-1'
  17. }"
  18. v-if="showAsterisk"
  19. >
  20. *
  21. </cl-text>
  22. </view>
  23. <view class="cl-form-item__content" :class="[pt.content?.className]">
  24. <slot></slot>
  25. </view>
  26. </view>
  27. <slot name="error" :error="errorText" v-if="hasError && showMessage">
  28. <cl-text
  29. color="error"
  30. :pt="{
  31. className: parseClass(['mt-2 !text-sm', pt.error?.className])
  32. }"
  33. >
  34. {{ errorText }}
  35. </cl-text>
  36. </slot>
  37. </view>
  38. </template>
  39. <script setup lang="ts">
  40. import { computed, onMounted, onUnmounted, watch, type PropType } from "vue";
  41. import { isEqual, parseClass, parsePt } from "@/cool";
  42. import type { ClFormLabelPosition, PassThroughProps } from "../../types";
  43. import { useForm } from "../../hooks";
  44. defineOptions({
  45. name: "cl-form-item"
  46. });
  47. // 组件属性定义
  48. const props = defineProps({
  49. // 透传样式
  50. pt: {
  51. type: Object,
  52. default: () => ({})
  53. },
  54. // 字段标签
  55. label: {
  56. type: String,
  57. default: ""
  58. },
  59. // 字段名称
  60. prop: {
  61. type: String,
  62. default: ""
  63. },
  64. // 标签位置
  65. labelPosition: {
  66. type: String as PropType<ClFormLabelPosition>,
  67. default: null
  68. },
  69. // 标签宽度
  70. labelWidth: {
  71. type: String as PropType<string | null>,
  72. default: null
  73. },
  74. // 是否显示必填星号
  75. showAsterisk: {
  76. type: Boolean as PropType<boolean | null>,
  77. default: null
  78. },
  79. // 是否显示错误信息
  80. showMessage: {
  81. type: Boolean as PropType<boolean | null>,
  82. default: null
  83. },
  84. // 是否必填
  85. required: {
  86. type: Boolean,
  87. default: false
  88. }
  89. });
  90. // cl-form 上下文
  91. const { formRef, getError, getValue, validateField, addField, removeField } = useForm();
  92. // 透传样式类型
  93. type PassThrough = {
  94. className?: string;
  95. inner?: PassThroughProps;
  96. label?: PassThroughProps;
  97. content?: PassThroughProps;
  98. error?: PassThroughProps;
  99. };
  100. // 解析透传样式
  101. const pt = computed(() => parsePt<PassThrough>(props.pt));
  102. // 当前错误信息
  103. const errorText = computed<string>(() => {
  104. return getError(props.prop);
  105. });
  106. // 是否有错误
  107. const hasError = computed<boolean>(() => {
  108. return errorText.value != "";
  109. });
  110. // 当前标签位置
  111. const labelPosition = computed<ClFormLabelPosition>(() => {
  112. return props.labelPosition ?? formRef.value?.labelPosition ?? "left";
  113. });
  114. // 标签宽度
  115. const labelWidth = computed<string>(() => {
  116. return props.labelWidth ?? formRef.value?.labelWidth ?? "120rpx";
  117. });
  118. // 是否显示必填星号
  119. const showAsterisk = computed<boolean>(() => {
  120. if (!props.required) {
  121. return false;
  122. }
  123. return props.showAsterisk ?? formRef.value?.showAsterisk ?? true;
  124. });
  125. // 是否显示错误信息
  126. const showMessage = computed<boolean>(() => {
  127. if (!props.required) {
  128. return false;
  129. }
  130. return props.showMessage ?? formRef.value?.showMessage ?? true;
  131. });
  132. watch(
  133. computed(() => props.required),
  134. (val: boolean) => {
  135. if (val) {
  136. addField(props.prop);
  137. } else {
  138. removeField(props.prop);
  139. }
  140. },
  141. {
  142. immediate: true
  143. }
  144. );
  145. onMounted(() => {
  146. watch(
  147. computed(() => {
  148. const value = getValue(props.prop);
  149. if (value == null) {
  150. return "";
  151. }
  152. return value;
  153. }),
  154. (val: any, val2: any) => {
  155. if (props.required) {
  156. if (!isEqual(val, val2)) {
  157. validateField(props.prop);
  158. }
  159. }
  160. },
  161. {
  162. deep: true
  163. }
  164. );
  165. });
  166. onUnmounted(() => {
  167. removeField(props.prop);
  168. });
  169. </script>
  170. <style lang="scss" scoped>
  171. .cl-form-item {
  172. @apply w-full mb-6;
  173. &__inner {
  174. @apply w-full;
  175. &.is-top {
  176. @apply flex flex-col;
  177. }
  178. &.is-left {
  179. @apply flex flex-row;
  180. }
  181. &.is-right {
  182. @apply flex flex-row;
  183. }
  184. }
  185. &__label {
  186. @apply flex flex-row items-center;
  187. &.is-top {
  188. @apply w-full mb-2;
  189. }
  190. &.is-left {
  191. @apply mr-3;
  192. }
  193. &.is-right {
  194. @apply mr-3 justify-end;
  195. }
  196. }
  197. &__content {
  198. @apply relative flex-1 w-full;
  199. }
  200. }
  201. </style>