1
YM
2024-07-26 c423765d8a5ff0c58d23bb0a31af7df6c1fad95c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<script setup>
    import { computed, inject, ref } from 'vue'
    const props = defineProps({
        label: [String],
        prop: [String],
        rules: {
            type: Array,
            default: []
        },
        required: { // 是否必填
            type: [Boolean, Number, String],
            default: false
        },
        align: { // left | right
            default: 'right'
        },
        direction: {
            default: 'row'
        },
        border: { // 是否有下边框
            type: Boolean,
            default: true
        },
 
        labelStyle: { // lable样式
            type: Object,
            default () {
                return {}
            }
        },
    })
    const $parent = inject('$parent')
    const formRuleRef = ref(null)
 
    const required = computed(() => props.required || props.rules.length || ($parent.getProps('rules') || {})[props.prop])
    const modelValue = computed(() => $parent.getModel(props.prop))
    const showError = computed(() => $parent.getProps('showError'))
    const itemRules = computed(() => {
        if (!$parent) return []
        const parentRules = $parent.getProps('rules') || {}
        const _rules = parentRules[props.prop] || []
        const rules = _rules ? [..._rules, ...props.rules] : props.rules
        return rules.map(rule => {
            if (!rule.message) {
                rule.message = `请完善${props.label}`
            }
            return rule
        })
    })
    const classes = computed(() => {
        return {
            row: props.direction == 'row',
            col: props.direction == 'col',
            error: (formRuleRef.value && formRuleRef.value.error && $parent.getProps('showError')),
            boldLable: $parent.getProps('boldLable'),
            hasBorder: $parent.getProps('border') && props.border
        }
    })
 
    function check() {
        return formRuleRef.value.check()
    }
 
    function getState(name) {
        return formRuleRef.value.getState(name)
    }
 
    function getValid(name = 'valid') {
        return getState(name)
    }
 
    function reset() {
        formRuleRef.value.reset()
    }
 
    function init() {
        $parent.addChild({ props, check, getValid, getState, reset })
    }
 
    init()
</script>
 
 
<template>
    <view class="jc-form-item" :style="$parent.getProps('itemStyle')" :class="classes">
        <view class="jc-form-item__label" :style="labelStyle" v-if="label">
            <view class="main">
                <text class="main__label">{{label}}</text>
                <text class="main__required" v-if="required">*</text>
                <slot name="label-desc"></slot>
            </view>
            <slot name="label-right"></slot>
        </view>
        <view class="jc-form-item__cont" :class="[align]" :style="$parent.getProps('itemContentStyle')">
            <jc-form-rule ref="formRuleRef" :value="modelValue" :rules="itemRules" :model="$parent.getProps('model')">
                <slot></slot>
                <view :style="$parent.getProps('errorStyle')" v-if="showError && formRuleRef && formRuleRef.error">{{formRuleRef.error}}</view>
            </jc-form-rule>
        </view>
    </view>
</template>
 
<style lang="scss" scoped>
    .jc-form-item {
        display: flex;
        align-items: center;
 
        &.hasBorder {
            border-bottom: 1rpx solid RGB(237, 237, 239);
            // transition: border-color .2s;
        }
 
        &.error {
            border-bottom-color: RGB(234, 88, 11);
        }
 
        &.boldLable {
            .jc-form-item__label {
                font-size: 30rpx;
                font-weight: 500;
            }
        }
 
        &__label {
            display: flex;
            align-items: center;
            padding-right: 20rpx;
 
            .main {
                flex: 1;
 
                &__label {}
 
                &__required {
                    margin-left: .1em;
                    color: RGB(234, 88, 11);
                }
            }
 
        }
 
        &__cont {
            width: 100%;
 
            &.right {
                text-align: right;
            }
        }
 
        &.row {
            flex-direction: row;
 
            .jc-form-item__label {}
 
            .jc-form-item__cont {
                flex: 1;
            }
        }
 
        &.col {
            flex-direction: column;
 
            .jc-form-item__label {
                width: 100%;
                margin: 0 0 20rpx 0;
            }
 
            .jc-form-item__cont {}
        }
    }
</style>