cl-form-item.uvue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. defineSlots<{
  48. error(props: { error: string }): any;
  49. }>();
  50. // 组件属性定义
  51. const props = defineProps({
  52. // 透传样式
  53. pt: {
  54. type: Object,
  55. default: () => ({})
  56. },
  57. // 字段标签
  58. label: {
  59. type: String,
  60. default: ""
  61. },
  62. // 字段名称
  63. prop: {
  64. type: String,
  65. default: ""
  66. },
  67. // 标签位置
  68. labelPosition: {
  69. type: String as PropType<ClFormLabelPosition>,
  70. default: null
  71. },
  72. // 标签宽度
  73. labelWidth: {
  74. type: String as PropType<string | null>,
  75. default: null
  76. },
  77. // 是否显示必填星号
  78. showAsterisk: {
  79. type: Boolean as PropType<boolean | null>,
  80. default: null
  81. },
  82. // 是否显示错误信息
  83. showMessage: {
  84. type: Boolean as PropType<boolean | null>,
  85. default: null
  86. },
  87. // 是否必填
  88. required: {
  89. type: Boolean,
  90. default: false
  91. }
  92. });
  93. // cl-form 上下文
  94. const { formRef, getError, getValue, validateField, addField, removeField } = useForm();
  95. // 透传样式类型
  96. type PassThrough = {
  97. className?: string;
  98. inner?: PassThroughProps;
  99. label?: PassThroughProps;
  100. content?: PassThroughProps;
  101. error?: PassThroughProps;
  102. };
  103. // 解析透传样式
  104. const pt = computed(() => parsePt<PassThrough>(props.pt));
  105. // 当前错误信息
  106. const errorText = computed<string>(() => {
  107. return getError(props.prop);
  108. });
  109. // 是否有错误
  110. const hasError = computed<boolean>(() => {
  111. return errorText.value != "";
  112. });
  113. // 当前标签位置
  114. const labelPosition = computed<ClFormLabelPosition>(() => {
  115. return props.labelPosition ?? formRef.value?.labelPosition ?? "left";
  116. });
  117. // 标签宽度
  118. const labelWidth = computed<string>(() => {
  119. return props.labelWidth ?? formRef.value?.labelWidth ?? "120rpx";
  120. });
  121. // 是否显示必填星号
  122. const showAsterisk = computed<boolean>(() => {
  123. if (!props.required) {
  124. return false;
  125. }
  126. return props.showAsterisk ?? formRef.value?.showAsterisk ?? true;
  127. });
  128. // 是否显示错误信息
  129. const showMessage = computed<boolean>(() => {
  130. if (!props.required) {
  131. return false;
  132. }
  133. return props.showMessage ?? formRef.value?.showMessage ?? true;
  134. });
  135. watch(
  136. computed(() => props.required),
  137. (val: boolean) => {
  138. if (val) {
  139. addField(props.prop);
  140. } else {
  141. removeField(props.prop);
  142. }
  143. },
  144. {
  145. immediate: true
  146. }
  147. );
  148. onMounted(() => {
  149. watch(
  150. computed(() => {
  151. const value = getValue(props.prop);
  152. if (value == null) {
  153. return "";
  154. }
  155. return value;
  156. }),
  157. (a: any, b: any) => {
  158. if (props.required) {
  159. if (!isEqual(a, b)) {
  160. validateField(props.prop);
  161. }
  162. }
  163. },
  164. {
  165. deep: true
  166. }
  167. );
  168. });
  169. onUnmounted(() => {
  170. removeField(props.prop);
  171. });
  172. </script>
  173. <style lang="scss" scoped>
  174. .cl-form-item {
  175. @apply w-full mb-6;
  176. &__inner {
  177. @apply w-full;
  178. &.is-top {
  179. @apply flex flex-col;
  180. }
  181. &.is-left {
  182. @apply flex flex-row;
  183. }
  184. &.is-right {
  185. @apply flex flex-row;
  186. }
  187. }
  188. &__label {
  189. @apply flex flex-row items-center;
  190. &.is-top {
  191. @apply w-full mb-2;
  192. }
  193. &.is-left {
  194. @apply mr-3;
  195. }
  196. &.is-right {
  197. @apply mr-3 justify-end;
  198. }
  199. }
  200. &__content {
  201. @apply relative flex-1 w-full;
  202. }
  203. }
  204. </style>