@puresnake/snake-ui 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/changelog.md +1 -0
- package/components/snake-activity-list/snake-activity-list.vue +365 -0
- package/components/snake-activity-list/types.ts +29 -0
- package/components/snake-auxiliary-segmentation/snake-auxiliary-segmentation.vue +44 -0
- package/components/snake-auxiliary-segmentation/types.ts +5 -0
- package/components/snake-button/snake-button.vue +357 -0
- package/components/snake-button/types.ts +103 -0
- package/components/snake-button-sort/snake-button-sort.vue +72 -0
- package/components/snake-button-sort/types.ts +22 -0
- package/components/snake-calendar/snake-calendar.vue +998 -0
- package/components/snake-calendar/types.ts +189 -0
- package/components/snake-cell-group/snake-cell-group.vue +55 -0
- package/components/snake-cell-group/types.ts +19 -0
- package/components/snake-cell-item/snake-cell-item.vue +159 -0
- package/components/snake-cell-item/types.ts +66 -0
- package/components/snake-checkbox/snake-checkbox.vue +264 -0
- package/components/snake-checkbox/types.ts +70 -0
- package/components/snake-checkbox-group/snake-checkbox-group.vue +154 -0
- package/components/snake-checkbox-group/types.ts +52 -0
- package/components/snake-cms/blocks/v2/cms-v2-carousel.vue +87 -0
- package/components/snake-cms/blocks/v2/cms-v2-coupon.vue +25 -0
- package/components/snake-cms/blocks/v2/cms-v2-custom.vue +12 -0
- package/components/snake-cms/blocks/v2/cms-v2-divider.vue +33 -0
- package/components/snake-cms/blocks/v2/cms-v2-floatbutton.vue +28 -0
- package/components/snake-cms/blocks/v2/cms-v2-goods-list.vue +150 -0
- package/components/snake-cms/blocks/v2/cms-v2-hotzone.vue +41 -0
- package/components/snake-cms/blocks/v2/cms-v2-images.vue +41 -0
- package/components/snake-cms/blocks/v2/cms-v2-magiccube.vue +93 -0
- package/components/snake-cms/blocks/v2/cms-v2-notice.vue +80 -0
- package/components/snake-cms/blocks/v2/cms-v2-popup.vue +46 -0
- package/components/snake-cms/blocks/v2/cms-v2-richtext.vue +18 -0
- package/components/snake-cms/blocks/v2/cms-v2-search.vue +115 -0
- package/components/snake-cms/blocks/v2/cms-v2-video.vue +25 -0
- package/components/snake-cms/blocks/v2/shared.ts +84 -0
- package/components/snake-cms/snake-cms.vue +409 -0
- package/components/snake-cms/types.ts +50 -0
- package/components/snake-commodity-search/snake-commodity-search.vue +234 -0
- package/components/snake-commodity-search/types.ts +11 -0
- package/components/snake-config-provider/snake-config-provider.vue +61 -0
- package/components/snake-config-provider/types.ts +22 -0
- package/components/snake-count-down/snake-count-down.vue +231 -0
- package/components/snake-count-down/types.ts +103 -0
- package/components/snake-coupon/snake-coupon.vue +209 -0
- package/components/snake-coupon/types.ts +21 -0
- package/components/snake-custom-module/snake-custom-module.vue +25 -0
- package/components/snake-custom-module/types.ts +11 -0
- package/components/snake-drop-menu/snake-drop-menu.vue +123 -0
- package/components/snake-drop-menu/types.ts +95 -0
- package/components/snake-drop-menu-item/snake-drop-menu-item.vue +181 -0
- package/components/snake-drop-menu-item/types.ts +48 -0
- package/components/snake-empty/snake-empty.vue +100 -0
- package/components/snake-empty/types.ts +48 -0
- package/components/snake-filter-sorts/filter-page.vue +695 -0
- package/components/snake-filter-sorts/snake-filter-sorts.vue +185 -0
- package/components/snake-filter-sorts/types.ts +57 -0
- package/components/snake-floating-bubble/snake-floating-bubble.vue +157 -0
- package/components/snake-floating-bubble/types.ts +43 -0
- package/components/snake-follow/snake-follow.vue +18 -0
- package/components/snake-follow/types.ts +2 -0
- package/components/snake-form/snake-form.vue +124 -0
- package/components/snake-form/types.ts +96 -0
- package/components/snake-form-item/snake-form-item.vue +331 -0
- package/components/snake-form-item/types.ts +70 -0
- package/components/snake-goods-list/snake-goods-list.vue +792 -0
- package/components/snake-goods-list/types.ts +55 -0
- package/components/snake-graphic-navigation/snake-graphic-navigation.vue +198 -0
- package/components/snake-graphic-navigation/types.ts +43 -0
- package/components/snake-icon/icon-names.ts +222 -0
- package/components/snake-icon/snake-icon.vue +220 -0
- package/components/snake-icon/types.ts +45 -0
- package/components/snake-image/snake-image.vue +230 -0
- package/components/snake-image/types.ts +70 -0
- package/components/snake-images/snake-images.vue +80 -0
- package/components/snake-images/types.ts +5 -0
- package/components/snake-index-anchor/snake-index-anchor.vue +83 -0
- package/components/snake-index-anchor/types.ts +14 -0
- package/components/snake-index-list/anchor-registry.ts +80 -0
- package/components/snake-index-list/snake-index-list.vue +259 -0
- package/components/snake-index-list/types.ts +115 -0
- package/components/snake-index-list-panel/snake-index-list-panel.vue +277 -0
- package/components/snake-index-list-panel/types.ts +10 -0
- package/components/snake-input/snake-input.vue +225 -0
- package/components/snake-input/types.ts +99 -0
- package/components/snake-jump-applet/snake-jump-applet.vue +99 -0
- package/components/snake-jump-applet/types.ts +17 -0
- package/components/snake-line/snake-line.vue +42 -0
- package/components/snake-line/types.ts +30 -0
- package/components/snake-line-progress/snake-line-progress.vue +105 -0
- package/components/snake-line-progress/types.ts +26 -0
- package/components/snake-link/snake-link.vue +74 -0
- package/components/snake-link/types.ts +30 -0
- package/components/snake-list-switching/components/tree-select/index.vue +156 -0
- package/components/snake-list-switching/snake-list-switching.vue +1285 -0
- package/components/snake-list-switching/types.ts +11 -0
- package/components/snake-loading/snake-loading.vue +241 -0
- package/components/snake-loading/types.ts +34 -0
- package/components/snake-loadmore/snake-loadmore.vue +103 -0
- package/components/snake-loadmore/types.ts +56 -0
- package/components/snake-magic-cube/snake-magic-cube.vue +622 -0
- package/components/snake-magic-cube/types.ts +5 -0
- package/components/snake-modal/snake-modal.vue +434 -0
- package/components/snake-modal/types.ts +71 -0
- package/components/snake-navbar/snake-navbar.vue +194 -0
- package/components/snake-navbar/types.ts +54 -0
- package/components/snake-no-network/snake-no-network.vue +125 -0
- package/components/snake-no-network/types.ts +27 -0
- package/components/snake-notice/snake-notice.vue +44 -0
- package/components/snake-notice/types.ts +5 -0
- package/components/snake-notice-bar/snake-notice-bar.vue +336 -0
- package/components/snake-notice-bar/types.ts +82 -0
- package/components/snake-overlay/snake-overlay.vue +73 -0
- package/components/snake-overlay/types.ts +35 -0
- package/components/snake-page-popup/snake-page-popup.vue +146 -0
- package/components/snake-page-popup/types.ts +28 -0
- package/components/snake-parse/audio/audio.vue +286 -0
- package/components/snake-parse/audio/context.js +11 -0
- package/components/snake-parse/audio/index.js +34 -0
- package/components/snake-parse/node/node.vue +830 -0
- package/components/snake-parse/parser.js +1480 -0
- package/components/snake-parse/snake-parse.vue +548 -0
- package/components/snake-parse/static/app-plus/snake-parse/js/handler.js +109 -0
- package/components/snake-parse/static/app-plus/snake-parse/js/uni.webview.js +0 -0
- package/components/snake-parse/static/app-plus/snake-parse/js/uni.webview.min.js +1 -0
- package/components/snake-parse/static/app-plus/snake-parse/local.html +3 -0
- package/components/snake-parse/style/index.js +132 -0
- package/components/snake-parse/style/parser.js +175 -0
- package/components/snake-parse/types.ts +49 -0
- package/components/snake-picture-ads/snake-picture-ads.vue +292 -0
- package/components/snake-picture-ads/types.ts +5 -0
- package/components/snake-popup/snake-popup.vue +348 -0
- package/components/snake-popup/types.ts +88 -0
- package/components/snake-qr-code/qrcode.ts +1214 -0
- package/components/snake-qr-code/snake-qr-code.vue +408 -0
- package/components/snake-qr-code/types.ts +78 -0
- package/components/snake-radio/snake-radio.vue +271 -0
- package/components/snake-radio/types.ts +64 -0
- package/components/snake-radio-group/snake-radio-group.vue +98 -0
- package/components/snake-radio-group/types.ts +48 -0
- package/components/snake-read-more/snake-read-more.vue +140 -0
- package/components/snake-read-more/types.ts +53 -0
- package/components/snake-rich-text/snake-rich-text.vue +24 -0
- package/components/snake-rich-text/types.ts +5 -0
- package/components/snake-root-portal/snake-root-portal.vue +92 -0
- package/components/snake-root-portal/types.ts +9 -0
- package/components/snake-search/snake-search.vue +202 -0
- package/components/snake-search/types.ts +84 -0
- package/components/snake-select/snake-select.vue +371 -0
- package/components/snake-select/types.ts +75 -0
- package/components/snake-skeleton/snake-skeleton.vue +104 -0
- package/components/snake-skeleton/types.ts +32 -0
- package/components/snake-skeleton-item/snake-skeleton-item.vue +90 -0
- package/components/snake-skeleton-item/types.ts +30 -0
- package/components/snake-slider/snake-slider.vue +249 -0
- package/components/snake-slider/types.ts +59 -0
- package/components/snake-sticky/snake-sticky.vue +178 -0
- package/components/snake-sticky/types.ts +48 -0
- package/components/snake-subsection/snake-subsection.vue +188 -0
- package/components/snake-subsection/types.ts +54 -0
- package/components/snake-swiper/snake-swiper.vue +229 -0
- package/components/snake-swiper/types.ts +79 -0
- package/components/snake-switch/snake-switch.vue +100 -0
- package/components/snake-switch/types.ts +41 -0
- package/components/snake-tabbar/snake-tabbar.vue +306 -0
- package/components/snake-tabbar/types.ts +93 -0
- package/components/snake-tabs/snake-tabs.vue +305 -0
- package/components/snake-tabs/types.ts +79 -0
- package/components/snake-text/snake-text.vue +292 -0
- package/components/snake-text/types.ts +100 -0
- package/components/snake-textarea/snake-textarea.vue +171 -0
- package/components/snake-textarea/types.ts +79 -0
- package/components/snake-toast/snake-toast.vue +330 -0
- package/components/snake-toast/types.ts +55 -0
- package/components/snake-tooltip/position.ts +33 -0
- package/components/snake-tooltip/snake-tooltip.vue +339 -0
- package/components/snake-tooltip/types.ts +80 -0
- package/components/snake-transition/snake-transition.vue +315 -0
- package/components/snake-transition/types.ts +65 -0
- package/components/snake-upload/snake-upload.vue +1801 -0
- package/components/snake-upload/types.ts +256 -0
- package/components/snake-videos/snake-videos.vue +122 -0
- package/components/snake-videos/types.ts +7 -0
- package/iconfont.css +881 -0
- package/index.d.ts +7 -0
- package/index.scss +26 -0
- package/index.ts +94 -0
- package/libs/cms/blocks.ts +103 -0
- package/libs/cms/config.ts +25 -0
- package/libs/cms/index.ts +7 -0
- package/libs/cms/normalize.ts +135 -0
- package/libs/cms/resolve.ts +12 -0
- package/libs/cms/types.ts +164 -0
- package/libs/cms/use-commerce.ts +385 -0
- package/libs/cms/v2.ts +193 -0
- package/libs/config/index.ts +4 -0
- package/libs/config/modal.ts +93 -0
- package/libs/config/state.ts +33 -0
- package/libs/config/theme.ts +351 -0
- package/libs/config/toast.ts +71 -0
- package/libs/config/types.ts +135 -0
- package/libs/config/zIndex.ts +44 -0
- package/libs/css/color.scss +105 -0
- package/libs/css/common.scss +171 -0
- package/libs/css/style.h5.scss +7 -0
- package/libs/css/style.mp.scss +7 -0
- package/libs/css/style.theme.scss +70 -0
- package/libs/css/style.vue.scss +140 -0
- package/libs/form/async-validator.min.d.ts +75 -0
- package/libs/form/async-validator.min.js +3 -0
- package/libs/form/index.ts +3 -0
- package/libs/form/path.ts +40 -0
- package/libs/form/types.ts +51 -0
- package/libs/form/validate.ts +149 -0
- package/libs/foundation.ts +96 -0
- package/libs/function/$parent.ts +33 -0
- package/libs/function/clipboard.ts +13 -0
- package/libs/function/color.ts +55 -0
- package/libs/function/debounce.ts +34 -0
- package/libs/function/deepClone.ts +30 -0
- package/libs/function/deepMerge.ts +25 -0
- package/libs/function/emitter.ts +30 -0
- package/libs/function/format.ts +27 -0
- package/libs/function/getParent.ts +53 -0
- package/libs/function/getRect.ts +29 -0
- package/libs/function/guid.ts +11 -0
- package/libs/function/index.ts +31 -0
- package/libs/function/queryParams.ts +73 -0
- package/libs/function/random.ts +6 -0
- package/libs/function/randomArray.ts +14 -0
- package/libs/function/route.ts +155 -0
- package/libs/function/sleep.ts +7 -0
- package/libs/function/style.ts +58 -0
- package/libs/function/styleUtils.ts +83 -0
- package/libs/function/system.ts +14 -0
- package/libs/function/test.ts +202 -0
- package/libs/function/throttle.ts +36 -0
- package/libs/function/timeFormat.ts +27 -0
- package/libs/function/timeFrom.ts +27 -0
- package/libs/function/toast.ts +10 -0
- package/libs/function/trim.ts +11 -0
- package/libs/function/type2icon.ts +11 -0
- package/libs/function/unit.ts +45 -0
- package/libs/hooks/index.ts +11 -0
- package/libs/hooks/useChildren.ts +125 -0
- package/libs/hooks/useColor.ts +5 -0
- package/libs/hooks/useDebounce.ts +5 -0
- package/libs/hooks/useLocale.ts +13 -0
- package/libs/hooks/useModal.ts +131 -0
- package/libs/hooks/useParent.ts +62 -0
- package/libs/hooks/useRect.ts +44 -0
- package/libs/hooks/useRouter.ts +5 -0
- package/libs/hooks/useTheme.ts +31 -0
- package/libs/hooks/useThrottle.ts +5 -0
- package/libs/hooks/useToast.ts +116 -0
- package/libs/index.ts +18 -0
- package/libs/request/auto-http.ts +1 -0
- package/libs/request/index.ts +343 -0
- package/libs/runtime/accessors.ts +55 -0
- package/libs/runtime/context.ts +40 -0
- package/libs/runtime/index.ts +18 -0
- package/libs/runtime/types.ts +49 -0
- package/libs/util/calendar.ts +6 -0
- package/locale/index.ts +130 -0
- package/locale/lang/en-US.ts +67 -0
- package/locale/lang/zh-CN.ts +67 -0
- package/package.json +130 -5
- package/plugins/index.ts +1 -0
- package/plugins/root/README.md +86 -0
- package/plugins/root/index.ts +123 -0
- package/plugins/root/page.ts +77 -0
- package/plugins/root/root.ts +53 -0
- package/plugins/root/utils.ts +137 -0
- package/readme.md +79 -0
- package/theme.scss +118 -0
- package/types/components.d.ts +236 -0
- package/types/global.d.ts +21 -0
- package/types/index.d.ts +6 -0
- package/types/uni-app.d.ts +16 -0
- package/README.md +0 -3
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<view
|
|
3
|
+
class="snake-form-item"
|
|
4
|
+
:class="[
|
|
5
|
+
{
|
|
6
|
+
'snake-form-item--border-bottom': elBorderBottom,
|
|
7
|
+
'snake-form-item--border-bottom-error': validateState === 'error' && showError('border-bottom'),
|
|
8
|
+
'snake-form-item--border-error': validateState === 'error' && showError('border')
|
|
9
|
+
},
|
|
10
|
+
customClass
|
|
11
|
+
]"
|
|
12
|
+
:style="customStyle"
|
|
13
|
+
>
|
|
14
|
+
<view class="snake-form-item__body" :style="{ flexDirection: elLabelPosition === 'left' ? 'row' : 'column' }">
|
|
15
|
+
<view
|
|
16
|
+
class="snake-form-item__left"
|
|
17
|
+
:style="{
|
|
18
|
+
width: labelWidth,
|
|
19
|
+
flex: `0 0 ${labelWidth}`,
|
|
20
|
+
marginBottom: elLabelPosition === 'left' ? 0 : '10rpx'
|
|
21
|
+
}"
|
|
22
|
+
>
|
|
23
|
+
<view
|
|
24
|
+
v-if="required || leftIcon || label || $slots.label || $slots.leftIcon"
|
|
25
|
+
class="snake-form-item__left-content"
|
|
26
|
+
>
|
|
27
|
+
<text v-if="required" class="snake-form-item__required">*</text>
|
|
28
|
+
<view v-if="leftIcon || $slots.leftIcon" class="snake-form-item__left-icon">
|
|
29
|
+
<slot name="leftIcon">
|
|
30
|
+
<snake-icon
|
|
31
|
+
:name="leftIcon"
|
|
32
|
+
:size="currentIconSize"
|
|
33
|
+
:custom-prefix="leftIconPrefix"
|
|
34
|
+
:custom-style="leftIconStyle"
|
|
35
|
+
/>
|
|
36
|
+
</slot>
|
|
37
|
+
</view>
|
|
38
|
+
<view
|
|
39
|
+
class="snake-form-item__label"
|
|
40
|
+
:style="[
|
|
41
|
+
elLabelStyle,
|
|
42
|
+
{ fontSize: actualFontSize },
|
|
43
|
+
{
|
|
44
|
+
justifyContent:
|
|
45
|
+
elLabelAlign === 'left'
|
|
46
|
+
? 'flex-start'
|
|
47
|
+
: elLabelAlign === 'center'
|
|
48
|
+
? 'center'
|
|
49
|
+
: 'flex-end'
|
|
50
|
+
}
|
|
51
|
+
]"
|
|
52
|
+
>
|
|
53
|
+
<slot name="label">{{ label }}</slot>
|
|
54
|
+
</view>
|
|
55
|
+
</view>
|
|
56
|
+
</view>
|
|
57
|
+
<view class="snake-form-item__right">
|
|
58
|
+
<view class="snake-form-item__content">
|
|
59
|
+
<view class="snake-form-item__slot"><slot /></view>
|
|
60
|
+
<view v-if="$slots.right || $slots.rightIcon || rightIcon" class="snake-form-item__right-icon">
|
|
61
|
+
<slot name="rightIcon">
|
|
62
|
+
<snake-icon
|
|
63
|
+
v-if="rightIcon"
|
|
64
|
+
:name="rightIcon"
|
|
65
|
+
:size="currentIconSize"
|
|
66
|
+
:custom-prefix="rightIconPrefix"
|
|
67
|
+
:custom-style="rightIconStyle"
|
|
68
|
+
/>
|
|
69
|
+
</slot>
|
|
70
|
+
<slot name="right" />
|
|
71
|
+
</view>
|
|
72
|
+
</view>
|
|
73
|
+
</view>
|
|
74
|
+
</view>
|
|
75
|
+
<view
|
|
76
|
+
v-if="validateState === 'error' && showError('message')"
|
|
77
|
+
class="snake-form-item__message"
|
|
78
|
+
:style="{
|
|
79
|
+
paddingLeft: elLabelPosition === 'left' && label ? addUnit(elLabelWidth, 'rpx') : '0',
|
|
80
|
+
fontSize: addUnit(currentSizeConfig.messageSize, 'rpx'),
|
|
81
|
+
lineHeight: addUnit(currentSizeConfig.messageSize, 'rpx')
|
|
82
|
+
}"
|
|
83
|
+
>
|
|
84
|
+
{{ validateMessage }}
|
|
85
|
+
</view>
|
|
86
|
+
</view>
|
|
87
|
+
</template>
|
|
88
|
+
|
|
89
|
+
<script setup lang="ts">
|
|
90
|
+
/**
|
|
91
|
+
* @component FormItem
|
|
92
|
+
* @description 表单字段容器,负责布局、尺寸和校验状态。
|
|
93
|
+
* @see /components/form
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
import { computed, inject, nextTick, onBeforeUnmount, onMounted, provide, ref, useSlots } from 'vue';
|
|
97
|
+
import { addUnit } from '../../libs/function/unit';
|
|
98
|
+
import { cloneFormValue, getPathValue, setPathValue, type FormRule, type FormTrigger } from '../../libs/form';
|
|
99
|
+
import Schema from '../../libs/form/async-validator.min';
|
|
100
|
+
import { formKey } from '../snake-form/types';
|
|
101
|
+
import { formItemKey } from './types';
|
|
102
|
+
import Icon from '../snake-icon/snake-icon.vue';
|
|
103
|
+
import type { FormItemProps } from './types';
|
|
104
|
+
|
|
105
|
+
Schema.warning = () => {};
|
|
106
|
+
|
|
107
|
+
defineOptions({ name: 'FormItem', options: { virtualHost: true, styleIsolation: 'shared' } });
|
|
108
|
+
const props = withDefaults(defineProps<FormItemProps>(), {
|
|
109
|
+
customStyle: '',
|
|
110
|
+
customClass: '',
|
|
111
|
+
label: '',
|
|
112
|
+
prop: '',
|
|
113
|
+
borderBottom: '',
|
|
114
|
+
labelPosition: '',
|
|
115
|
+
labelWidth: '',
|
|
116
|
+
labelStyle: () => ({}),
|
|
117
|
+
labelAlign: '',
|
|
118
|
+
rightIcon: '',
|
|
119
|
+
leftIcon: '',
|
|
120
|
+
leftIconPrefix: 'sicon',
|
|
121
|
+
rightIconPrefix: 'sicon',
|
|
122
|
+
leftIconStyle: () => ({}),
|
|
123
|
+
rightIconStyle: () => ({}),
|
|
124
|
+
required: false,
|
|
125
|
+
size: ''
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const slots = useSlots();
|
|
129
|
+
const form = inject(formKey, null);
|
|
130
|
+
|
|
131
|
+
const initialValue = ref<unknown>('');
|
|
132
|
+
const validateState = ref<'success' | 'error' | 'validating' | ''>('');
|
|
133
|
+
const validateMessage = ref('');
|
|
134
|
+
let validationVersion = 0;
|
|
135
|
+
|
|
136
|
+
const sizeConfig = {
|
|
137
|
+
small: { fontSize: 24, iconSize: 28, messageSize: 20 },
|
|
138
|
+
default: { fontSize: 28, iconSize: 32, messageSize: 24 },
|
|
139
|
+
large: { fontSize: 32, iconSize: 36, messageSize: 28 }
|
|
140
|
+
} as const;
|
|
141
|
+
|
|
142
|
+
const actualSize = computed(() => {
|
|
143
|
+
if (props.size !== '') return String(props.size);
|
|
144
|
+
return String(form?.size.value || 'default');
|
|
145
|
+
});
|
|
146
|
+
const presetSize = computed(() =>
|
|
147
|
+
actualSize.value in sizeConfig ? (actualSize.value as keyof typeof sizeConfig) : 'default'
|
|
148
|
+
);
|
|
149
|
+
const currentSizeConfig = computed(() => sizeConfig[presetSize.value]);
|
|
150
|
+
const actualFontSize = computed(() =>
|
|
151
|
+
addUnit(actualSize.value in sizeConfig ? currentSizeConfig.value.fontSize : actualSize.value, 'rpx')
|
|
152
|
+
);
|
|
153
|
+
const currentIconSize = computed(() => currentSizeConfig.value.iconSize);
|
|
154
|
+
const errorType = computed(() => form?.errorType.value || ['message']);
|
|
155
|
+
const showError = (type: string) => !errorType.value.includes('none') && errorType.value.includes(type);
|
|
156
|
+
// Controls only need error state when the form explicitly requests an input border.
|
|
157
|
+
const errorVisible = computed(() => validateState.value === 'error' && showError('border'));
|
|
158
|
+
|
|
159
|
+
const elLabelWidth = computed(() => {
|
|
160
|
+
if (props.labelWidth !== '' && props.labelWidth !== 0) return props.labelWidth;
|
|
161
|
+
return form?.labelWidth.value || 90;
|
|
162
|
+
});
|
|
163
|
+
const elLabelStyle = computed(() =>
|
|
164
|
+
Object.keys(props.labelStyle).length ? props.labelStyle : form?.labelStyle.value || {}
|
|
165
|
+
);
|
|
166
|
+
const elLabelPosition = computed(() => props.labelPosition || form?.labelPosition.value || 'left');
|
|
167
|
+
const elLabelAlign = computed(() => props.labelAlign || form?.labelAlign.value || 'left');
|
|
168
|
+
const elBorderBottom = computed(() =>
|
|
169
|
+
props.borderBottom !== '' ? props.borderBottom : (form?.borderBottom.value ?? false)
|
|
170
|
+
);
|
|
171
|
+
const labelWidth = computed(() =>
|
|
172
|
+
elLabelPosition.value === 'left'
|
|
173
|
+
? (props.label === 'true' || props.label === '') && !slots.label
|
|
174
|
+
? 'auto'
|
|
175
|
+
: addUnit(elLabelWidth.value, 'rpx')
|
|
176
|
+
: '100%'
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
function getRules(): FormRule[] {
|
|
180
|
+
const allRules = form?.rules.value || {};
|
|
181
|
+
const dynamicPath = props.prop
|
|
182
|
+
.replace(/\[(\d+)\]/g, '.$1')
|
|
183
|
+
.replace(/(?:^|\.)(\d+)(?=\.|$)/g, '.defaultField.fields');
|
|
184
|
+
const fieldRules = allRules[props.prop] || getPathValue(allRules, dynamicPath);
|
|
185
|
+
return ([] as FormRule[]).concat((fieldRules || []) as FormRule | FormRule[]);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function getFilteredRules(trigger = ''): FormRule[] {
|
|
189
|
+
const rules = getRules();
|
|
190
|
+
if (!trigger) return rules;
|
|
191
|
+
return rules.filter(rule => rule.trigger && ([] as string[]).concat(rule.trigger).includes(trigger));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function validation(trigger = '', callback: (message: string) => void = () => {}) {
|
|
195
|
+
const rules = getFilteredRules(trigger);
|
|
196
|
+
if (!rules.length) {
|
|
197
|
+
callback('');
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const version = ++validationVersion;
|
|
201
|
+
validateState.value = 'validating';
|
|
202
|
+
const value = getPathValue(form?.model.value, props.prop);
|
|
203
|
+
const validator = new Schema({ [props.prop]: rules });
|
|
204
|
+
void validator.validate({ [props.prop]: value }, { firstFields: true }, errors => {
|
|
205
|
+
if (version !== validationVersion) {
|
|
206
|
+
callback(validateMessage.value);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
validateState.value = errors ? 'error' : 'success';
|
|
210
|
+
validateMessage.value = errors?.[0]?.message || '';
|
|
211
|
+
callback(validateMessage.value);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function onFormBlur() {
|
|
216
|
+
validation('blur');
|
|
217
|
+
}
|
|
218
|
+
function onFormChange() {
|
|
219
|
+
validation('change');
|
|
220
|
+
}
|
|
221
|
+
function resetField() {
|
|
222
|
+
validationVersion += 1;
|
|
223
|
+
const model = form?.model.value;
|
|
224
|
+
if (model && props.prop) {
|
|
225
|
+
setPathValue(model, props.prop, cloneFormValue(initialValue.value));
|
|
226
|
+
}
|
|
227
|
+
validateState.value = '';
|
|
228
|
+
validateMessage.value = '';
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const exposedField = { validation, resetField, prop: props.prop };
|
|
232
|
+
onMounted(() => {
|
|
233
|
+
nextTick(() => {
|
|
234
|
+
if (!props.prop) return;
|
|
235
|
+
form?.register(exposedField as any);
|
|
236
|
+
initialValue.value = cloneFormValue(getPathValue(form?.model.value, props.prop));
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
onBeforeUnmount(() => {
|
|
240
|
+
validationVersion += 1;
|
|
241
|
+
if (!props.prop) return;
|
|
242
|
+
form?.unregister(exposedField as any);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
function notify(trigger: FormTrigger): Promise<void> {
|
|
246
|
+
return nextTick().then(
|
|
247
|
+
() =>
|
|
248
|
+
new Promise(resolve => {
|
|
249
|
+
validation(trigger, () => resolve());
|
|
250
|
+
})
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
provide(formItemKey, {
|
|
254
|
+
error: validateMessage,
|
|
255
|
+
errorVisible,
|
|
256
|
+
size: computed(() => presetSize.value),
|
|
257
|
+
notify
|
|
258
|
+
});
|
|
259
|
+
defineExpose({ validation, resetField, onFormBlur, onFormChange });
|
|
260
|
+
</script>
|
|
261
|
+
|
|
262
|
+
<style scoped>
|
|
263
|
+
.snake-form-item {
|
|
264
|
+
display: flex;
|
|
265
|
+
box-sizing: border-box;
|
|
266
|
+
flex-direction: column;
|
|
267
|
+
padding: 20rpx 0;
|
|
268
|
+
color: var(--snake-color-text, #303133);
|
|
269
|
+
font-size: 28rpx;
|
|
270
|
+
}
|
|
271
|
+
.snake-form-item--border-bottom {
|
|
272
|
+
border-bottom: 1px solid var(--snake-color-divider, #e4e7ed);
|
|
273
|
+
}
|
|
274
|
+
.snake-form-item--border-bottom-error {
|
|
275
|
+
border-color: var(--snake-color-error, #fa3534);
|
|
276
|
+
}
|
|
277
|
+
.snake-form-item--border-error :deep(.snake-input--surround),
|
|
278
|
+
.snake-form-item--border-error :deep(.snake-textarea--surround) {
|
|
279
|
+
border-color: var(--snake-color-error, #fa3534) !important;
|
|
280
|
+
}
|
|
281
|
+
.snake-form-item__body,
|
|
282
|
+
.snake-form-item__left,
|
|
283
|
+
.snake-form-item__left-content,
|
|
284
|
+
.snake-form-item__label,
|
|
285
|
+
.snake-form-item__right,
|
|
286
|
+
.snake-form-item__content,
|
|
287
|
+
.snake-form-item__slot,
|
|
288
|
+
.snake-form-item__right-icon {
|
|
289
|
+
display: flex;
|
|
290
|
+
}
|
|
291
|
+
.snake-form-item__left,
|
|
292
|
+
.snake-form-item__left-content,
|
|
293
|
+
.snake-form-item__label,
|
|
294
|
+
.snake-form-item__content,
|
|
295
|
+
.snake-form-item__slot,
|
|
296
|
+
.snake-form-item__right-icon {
|
|
297
|
+
align-items: center;
|
|
298
|
+
}
|
|
299
|
+
.snake-form-item__left-content,
|
|
300
|
+
.snake-form-item__label,
|
|
301
|
+
.snake-form-item__right,
|
|
302
|
+
.snake-form-item__content,
|
|
303
|
+
.snake-form-item__slot {
|
|
304
|
+
flex: 1;
|
|
305
|
+
}
|
|
306
|
+
.snake-form-item__left-content {
|
|
307
|
+
position: relative;
|
|
308
|
+
padding-right: 10rpx;
|
|
309
|
+
}
|
|
310
|
+
.snake-form-item__required {
|
|
311
|
+
position: absolute;
|
|
312
|
+
left: -16rpx;
|
|
313
|
+
padding-top: 6rpx;
|
|
314
|
+
color: var(--snake-color-error, #fa3534);
|
|
315
|
+
vertical-align: middle;
|
|
316
|
+
}
|
|
317
|
+
.snake-form-item__left-icon {
|
|
318
|
+
display: flex;
|
|
319
|
+
align-items: center;
|
|
320
|
+
margin-right: 8rpx;
|
|
321
|
+
}
|
|
322
|
+
.snake-form-item__right-icon {
|
|
323
|
+
margin-left: 10rpx;
|
|
324
|
+
color: var(--snake-color-text-light, #c0c4cc);
|
|
325
|
+
font-size: 30rpx;
|
|
326
|
+
}
|
|
327
|
+
.snake-form-item__message {
|
|
328
|
+
margin-top: 12rpx;
|
|
329
|
+
color: var(--snake-color-error, #fa3534);
|
|
330
|
+
}
|
|
331
|
+
</style>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { InjectionKey, Ref } from 'vue';
|
|
2
|
+
import type { UnitValue } from '../../libs/function/unit';
|
|
3
|
+
import type { StyleValue } from '../../types/global';
|
|
4
|
+
import type { FormTrigger } from '../../libs/form';
|
|
5
|
+
import type { FormLabelAlign, FormLabelPosition, FormSize } from '../snake-form/types';
|
|
6
|
+
|
|
7
|
+
/** FormItem 组件的公开属性。 */
|
|
8
|
+
export interface FormItemProps {
|
|
9
|
+
/** 标签文本。 */
|
|
10
|
+
label?: string;
|
|
11
|
+
/** 表单模型中的字段路径。 */
|
|
12
|
+
prop?: string;
|
|
13
|
+
/** 是否显示底边,空字符串表示继承 Form。 */
|
|
14
|
+
borderBottom?: string | boolean;
|
|
15
|
+
/** 标签位置。 */
|
|
16
|
+
labelPosition?: FormLabelPosition | '';
|
|
17
|
+
/** 标签宽度。 */
|
|
18
|
+
labelWidth?: UnitValue | '';
|
|
19
|
+
/** 标签自定义样式。 */
|
|
20
|
+
labelStyle?: Record<string, any>;
|
|
21
|
+
/** 标签对齐方式。 */
|
|
22
|
+
labelAlign?: FormLabelAlign | '';
|
|
23
|
+
/** 右侧图标。 */
|
|
24
|
+
rightIcon?: string;
|
|
25
|
+
/** 左侧图标。 */
|
|
26
|
+
leftIcon?: string;
|
|
27
|
+
/** 左侧图标字体前缀。 */
|
|
28
|
+
leftIconPrefix?: string;
|
|
29
|
+
/** 右侧图标字体前缀。 */
|
|
30
|
+
rightIconPrefix?: string;
|
|
31
|
+
/** 左侧图标样式。 */
|
|
32
|
+
leftIconStyle?: StyleValue;
|
|
33
|
+
/** 右侧图标样式。 */
|
|
34
|
+
rightIconStyle?: StyleValue;
|
|
35
|
+
/** 是否展示必填星号。 */
|
|
36
|
+
required?: boolean;
|
|
37
|
+
/** 预设尺寸或自定义字号。 */
|
|
38
|
+
size?: FormSize | string;
|
|
39
|
+
/** 根节点自定义类名。 */
|
|
40
|
+
customClass?: string;
|
|
41
|
+
/** 根节点自定义样式。 */
|
|
42
|
+
customStyle?: StyleValue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** FormItem 通过模板 ref 暴露的接口。 */
|
|
46
|
+
export interface FormItemExpose {
|
|
47
|
+
/** 校验当前字段。 */
|
|
48
|
+
validation(trigger?: string, callback?: (message: string) => void): void;
|
|
49
|
+
/** 将字段恢复为初始值。 */
|
|
50
|
+
resetField(): void;
|
|
51
|
+
/** 触发 blur 规则校验。 */
|
|
52
|
+
onFormBlur(): void;
|
|
53
|
+
/** 触发 change 规则校验。 */
|
|
54
|
+
onFormChange(): void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** FormItem 向叶子控件(input / textarea / switch / slider / radio-group / checkbox-group)注入的配对上下文。 */
|
|
58
|
+
export interface FormItemContext {
|
|
59
|
+
/** 当前校验错误文案。 */
|
|
60
|
+
error: Ref<string>;
|
|
61
|
+
/** 是否展示错误。 */
|
|
62
|
+
errorVisible: Ref<boolean>;
|
|
63
|
+
/** 控件尺寸。 */
|
|
64
|
+
size: Ref<FormSize>;
|
|
65
|
+
/** 触发指定方式的校验。 */
|
|
66
|
+
notify(trigger: FormTrigger): Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** FormItem ↔ 叶子控件配对注入键。 */
|
|
70
|
+
export const formItemKey: InjectionKey<FormItemContext> = Symbol('snake-form-item');
|