cl-form-item.uvue 4.2 KB

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