app-form-item.vue 6.5 KB
Newer Older
zcdtk's avatar
zcdtk committed
1
<template>
2
  <ion-item :class="[classes,labelPos.toLowerCase()]" :disabled="disabled">
zcdtk's avatar
zcdtk committed
3
        <template v-if="uiStyle == 'STYLE2'">
4
            <ion-label :class="classes" :style="{minWidth:labelWidth+'px'}" position="floating" v-if="isShowCaption && labelWidth > 0">{{isEmptyCaption ? '' : caption}}</ion-label>
zcdtk's avatar
zcdtk committed
5 6 7 8
            <slot></slot>
        </template>
        <template v-else>
            <template v-if="labelPos == 'LEFT'">
9
                <ion-label  :class="classes" :style="{minWidth:labelWidth+'px'}" v-if="isShowCaption && labelWidth > 0">{{isEmptyCaption ? '' : caption}}</ion-label>
KK's avatar
KK committed
10
                <div :style="contentStyle" style="display: flex;align-items: center;">
zcdtk's avatar
zcdtk committed
11 12
                    <slot></slot>
                </div>
KK's avatar
KK committed
13
                <div class="prompt_text">{{error}}</div>
zcdtk's avatar
zcdtk committed
14 15 16
            </template>
            <template v-if="labelPos == 'TOP'">
                <ion-label
17
                :class="classes"
18 19 20 21 22
                :style="{minWidth:labelWidth+'px'}"
                position="floating"
                v-if="isShowCaption && labelWidth > 0">
                    {{isEmptyCaption ? '' : caption}}
                </ion-label>
zcdtk's avatar
zcdtk committed
23
                <slot></slot>
KK's avatar
KK committed
24
                <div class="prompt_text">{{error}}</div>
zcdtk's avatar
zcdtk committed
25 26 27
            </template>
            <template v-if="labelPos == 'RIGHT' ">
                <slot></slot>
KK's avatar
KK committed
28
                <div class="prompt_text_right">{{error}}</div>
29
                <ion-label :class="classes" :style="{minWidth:labelWidth+'px'}" v-if="isShowCaption && labelWidth > 0">{{isEmptyCaption ? '' : caption}}</ion-label>
zcdtk's avatar
zcdtk committed
30 31 32 33 34 35 36 37 38 39
            </template>
            <template v-if="labelPos == 'NONE'" >
                <slot></slot>
            </template>
      </template>
  </ion-item>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
40
import { Util } from '@/ibiz-core/utils';
zcdtk's avatar
zcdtk committed
41 42 43 44 45 46 47 48 49 50
@Component({})
export default class AppFormItem extends Vue {
    /**
     * 名称
     *
     * @type {string}
     * @memberof AppFormItem
     */
    @Prop() public caption!: string;

51 52 53 54 55 56 57 58
    /**
     * 是否禁用
     *
     * @type {boolean}
     * @memberof AppFormItem
     */
    @Prop() public disabled?: boolean;

zcdtk's avatar
zcdtk committed
59 60 61 62 63 64 65 66
    /**
     * 错误信息
     *
     * @type {string}
     * @memberof AppFormItem
     */
    @Prop() public error?: string;

KK's avatar
KK committed
67 68 69
    /**
     * 表单项值
     *
70
     * @type {any}
KK's avatar
KK committed
71 72
     * @memberof AppFormItem
     */
zcdtk's avatar
zcdtk committed
73
    @Prop() public itemValue?: any;
KK's avatar
KK committed
74 75 76 77 78 79 80

    /**
     * 错误信息
     *
     * @type {string}
     * @memberof AppFormItem
     */
zcdtk's avatar
zcdtk committed
81
    public errorText: string = '';
KK's avatar
KK committed
82

zcdtk's avatar
zcdtk committed
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
    /**
     * label样式
     *
     * @type {string}
     * @memberof AppFormItem
     */
    @Prop() public labelStyle?: string;

    /**
     * 标签位置
     *
     * @type {(string | 'BOTTOM' | 'LEFT' | 'NONE' | 'RIGHT' | 'TOP')}
     * @memberof AppFormItem
     */
    @Prop() public labelPos?: string | 'BOTTOM' | 'LEFT' | 'NONE' | 'RIGHT' | 'TOP';

    /**
     * 标签宽度
     *
     * @type {number}
     * @memberof AppFormItem
     */
    @Prop({}) public labelWidth!: number;

    /**
     * 是否显示标题
     *
     * @type {boolean}
     * @memberof AppFormItem
     */
    @Prop() public isShowCaption?: boolean;

    /**
     * 标签是否空白
     *
     * @type {boolean}
     * @memberof AppFormItem
     */
    @Prop() public isEmptyCaption?: boolean;

    /**
     * 表单项名称
     *
     * @type {string}
     * @memberof AppFormItem
     */
    @Prop() public name!: string;

    /**
     * 内置样式
     *
     * @type {string}
     * @memberof AppFormItem
     */
    @Prop() public uiStyle?: string;

    /**
     * 表单项值规则
     *
142
     * @type {any}
zcdtk's avatar
zcdtk committed
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
     * @memberof AppFormItem
     */
    @Prop() public itemRules!: any;

    /**
     * 值规则数组
     *
     * @type {any[]}
     * @memberof AppFormItem
     */
    public rules: any[] = [];

    /**
     * 是否必填
     *
     * @type {boolean}
     * @memberof AppFormItem
     */
    public required: boolean = false;

    /**
     * 表单项值规则监控
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppFormItem
     */
zcdtk's avatar
zcdtk committed
170
    @Watch('itemRules', { deep: true })
zcdtk's avatar
zcdtk committed
171
    onItemRulesChange(newVal: any, oldVal: any) {
zcdtk's avatar
zcdtk committed
172 173
        if (!newVal) {
            return;
zcdtk's avatar
zcdtk committed
174
        }
zcdtk's avatar
zcdtk committed
175
        this.computeRequired(newVal);
zcdtk's avatar
zcdtk committed
176 177 178 179 180 181 182 183 184 185 186 187
    }

    /**
     * 计算样式
     *
     * @readonly
     * @type {string []}
     * @memberof AppFormItem
     */
    get classes(): string[] {
        return [
            'app-form-item',
KK's avatar
KK committed
188
            Object.is(this.labelPos, 'TOP') ? 'app-form-item-label-top item-label-floating' : '', this.required ? 'app-form-item-label-required' : 'app-form-item-label-notRequired', this.itemValue ? "item-has-value" : "", 'sc-ion-label-ios-h', 'sc-ion-label-ios-s ', 'ios', ' hydrated'
zcdtk's avatar
zcdtk committed
189 190 191 192
        ];
    }

    /**
193
     * 内容样式
zcdtk's avatar
zcdtk committed
194 195 196 197
     *
     * @readonly
     * @memberof AppFormItem
     */
198 199 200
    get contentStyle() {
        return {
            width: this.isShowCaption && this.labelWidth > 0 ? `calc(100% - ${this.labelWidth}px)` : '100%',
zcdtk's avatar
zcdtk committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        }
    }

    /**
     * 计算是否必填
     *
     * @private
     * @param {*} rules
     * @memberof AppFormItem
     */
    private computeRequired(rules: any): void {
        try {
            const _rules: any[] = rules;
            this.rules = [..._rules];
            this.rules.some((rule: any) => {
                if (rule.hasOwnProperty('required')) {
                    this.required = rule.required;
                    return true;
                }
                return false;
            });
        } catch (error) {
zcdtk's avatar
zcdtk committed
223 224 225
        }
    }

zcdtk's avatar
zcdtk committed
226 227 228 229 230 231
    /**
     * 校验值规则
     *
     * @returns {boolean}
     * @memberof AppFormItem
     */
KK's avatar
KK committed
232
    public async validateRules(): Promise<boolean> {
233
        return await this.validate(name, this.itemValue);
KK's avatar
KK committed
234 235 236 237 238 239 240 241
    }

    /**
     * 校验值规则
     *
     * @returns {boolean}
     * @memberof AppFormItem
     */
242
    public validate(property: string, data: any): Promise<any> {
KK's avatar
KK committed
243
        return new Promise((resolve, reject) => {
244
            Util.validateItem(property, data, this.rules).then(() => {
KK's avatar
KK committed
245 246 247 248 249 250
                this.errorText = "";
                resolve(true);
            }).catch(({ errors, fields }) => {
                this.errorText = errors[0].message;
                resolve(false);
            });
zcdtk's avatar
zcdtk committed
251
        });
252 253 254 255 256 257 258 259 260 261 262 263

    }

    /**
     * vue 生命周期
     *
     * @memberof AppFormItem
     */
    public mounted() {
        if (this.itemRules) {
            this.computeRequired(this.itemRules);
        }
zcdtk's avatar
zcdtk committed
264 265
    }

zcdtk's avatar
zcdtk committed
266 267
}
</script>
zcdtk's avatar
zcdtk committed
268

zcdtk's avatar
zcdtk committed
269 270 271
<style lang='less'>
@import './app-form-item.less';
</style>