cl-form-item.uvue 4.6 KB

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