123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- <template>
- <view class="uni-forms-item"
- :class="['is-direction-' + localLabelPos ,border?'uni-forms-item--border':'' ,border && isFirstBorder?'is-first-border':'']">
- <slot name="label">
- <view class="uni-forms-item__label" :class="{'no-label':!label && !isRequired}"
- :style="{width:localLabelWidth,justifyContent: localLabelAlign}">
- <text v-if="isRequired" class="is-required">*</text>
- <text>{{label}}</text>
- </view>
- </slot>
- <!-- #ifndef APP-NVUE -->
- <view class="uni-forms-item__content">
- <slot></slot>
- <view class="uni-forms-item__error" :class="{'msg--active':msg}">
- <text>{{msg}}</text>
- </view>
- </view>
- <!-- #endif -->
- <!-- #ifdef APP-NVUE -->
- <view class="uni-forms-item__nuve-content">
- <view class="uni-forms-item__content">
- <slot></slot>
- </view>
- <view class="uni-forms-item__error" :class="{'msg--active':msg}">
- <text class="error-text">{{msg}}</text>
- </view>
- </view>
- <!-- #endif -->
- </view>
- </template>
- <script>
-
- export default {
- name: 'uniFormsItem',
- options: {
- virtualHost: true
- },
- provide() {
- return {
- uniFormItem: this
- }
- },
- inject: {
- form: {
- from: 'uniForm',
- default: null
- },
- },
- props: {
-
- rules: {
- type: Array,
- default () {
- return null;
- }
- },
-
- name: {
- type: [String, Array],
- default: ''
- },
- required: {
- type: Boolean,
- default: false
- },
- label: {
- type: String,
- default: ''
- },
-
- labelWidth: {
- type: [String, Number],
- default: ''
- },
-
- labelAlign: {
- type: String,
- default: ''
- },
-
- errorMessage: {
- type: [String, Boolean],
- default: ''
- },
-
-
-
-
-
-
-
-
-
-
-
- leftIcon: String,
- iconColor: {
- type: String,
- default: '#606266'
- },
- },
- data() {
- return {
- errMsg: '',
- isRequired: false,
- userRules: null,
- localLabelAlign: 'left',
- localLabelWidth: '65px',
- localLabelPos: 'left',
- border: false,
- isFirstBorder: false,
- };
- },
- computed: {
-
- msg() {
- return this.errorMessage || this.errMsg;
- }
- },
- watch: {
-
- 'form.formRules'(val) {
-
-
- this.init()
-
- },
- 'form.labelWidth'(val) {
-
- this.localLabelWidth = this._labelWidthUnit(val)
- },
- 'form.labelPosition'(val) {
-
- this.localLabelPos = this._labelPosition()
- },
- 'form.labelAlign'(val) {
- }
- },
- created() {
- this.init(true)
- if (this.name && this.form) {
-
-
- this.$watch('form.formRules', () => {
- this.init()
- })
-
-
- this.$watch(
- () => {
- const val = this.form._getDataValue(this.name, this.form.localData)
- return val
- },
- (value, oldVal) => {
- const isEqual = this.form._isEqual(value, oldVal)
-
-
-
- if (!isEqual) {
- const val = this.itemSetValue(value)
- this.onFieldChange(val, false)
- }
- }, {
- immediate: false
- }
- );
- }
- },
-
- destroyed() {
- if (this.__isUnmounted) return
- this.unInit()
- },
-
-
- unmounted() {
- this.__isUnmounted = true
- this.unInit()
- },
-
- methods: {
-
- setRules(rules = null) {
- this.userRules = rules
- this.init(false)
- },
-
- setValue() {
-
- },
-
- async onFieldChange(value, formtrigger = true) {
- const {
- formData,
- localData,
- errShowType,
- validateCheck,
- validateTrigger,
- _isRequiredField,
- _realName
- } = this.form
- const name = _realName(this.name)
- if (!value) {
- value = this.form.formData[name]
- }
-
-
-
- const ruleLen = this.itemRules.rules && this.itemRules.rules.length
- if (!this.validator || !ruleLen || ruleLen === 0) return;
-
-
- const isRequiredField = _isRequiredField(this.itemRules.rules || []);
- let result = null;
-
- if (validateTrigger === 'bind' || formtrigger) {
-
- result = await this.validator.validateUpdate({
- [name]: value
- },
- formData
- );
-
- if (!isRequiredField && (value === undefined || value === '')) {
- result = null;
- }
-
- if (result && result.errorMessage) {
- if (errShowType === 'undertext') {
-
- this.errMsg = !result ? '' : result.errorMessage;
- }
- if (errShowType === 'toast') {
- uni.showToast({
- title: result.errorMessage || '校验错误',
- icon: 'none'
- });
- }
- if (errShowType === 'modal') {
- uni.showModal({
- title: '提示',
- content: result.errorMessage || '校验错误'
- });
- }
- } else {
- this.errMsg = ''
- }
-
- validateCheck(result ? result : null)
- } else {
- this.errMsg = ''
- }
- return result ? result : null;
- },
-
- init(type = false) {
- const {
- validator,
- formRules,
- childrens,
- formData,
- localData,
- _realName,
- labelWidth,
- _getDataValue,
- _setDataValue
- } = this.form || {}
-
- this.localLabelAlign = this._justifyContent()
-
- this.localLabelWidth = this._labelWidthUnit(labelWidth)
-
- this.localLabelPos = this._labelPosition()
- this.isRequired = this.required
-
- this.form && type && childrens.push(this)
- if (!validator || !formRules) return
-
- if (!this.form.isFirstBorder) {
- this.form.isFirstBorder = true;
- this.isFirstBorder = true;
- }
-
- if (this.group) {
- if (!this.group.isFirstBorder) {
- this.group.isFirstBorder = true;
- this.isFirstBorder = true;
- }
- }
- this.border = this.form.border;
-
- const name = _realName(this.name)
- const itemRule = this.userRules || this.rules
- if (typeof formRules === 'object' && itemRule) {
-
- formRules[name] = {
- rules: itemRule
- }
- validator.updateSchema(formRules);
- }
-
- const itemRules = formRules[name] || {}
- this.itemRules = itemRules
-
- this.validator = validator
-
- this.itemSetValue(_getDataValue(this.name, localData))
- this.isRequired = this._isRequired()
- },
- unInit() {
- if (this.form) {
- const {
- childrens,
- formData,
- _realName
- } = this.form
- childrens.forEach((item, index) => {
- if (item === this) {
- this.form.childrens.splice(index, 1)
- delete formData[_realName(item.name)]
- }
- })
- }
- },
-
- itemSetValue(value) {
- const name = this.form._realName(this.name)
- const rules = this.itemRules.rules || []
- const val = this.form._getValue(name, value, rules)
- this.form._setDataValue(name, this.form.formData, val)
- return val
- },
-
- clearValidate() {
- this.errMsg = '';
- },
-
- _isRequired() {
-
-
-
-
-
-
-
- return this.required
- },
-
- _justifyContent() {
- if (this.form) {
- const {
- labelAlign
- } = this.form
- let labelAli = this.labelAlign ? this.labelAlign : labelAlign;
- if (labelAli === 'left') return 'flex-start';
- if (labelAli === 'center') return 'center';
- if (labelAli === 'right') return 'flex-end';
- }
- return 'flex-start';
- },
-
- _labelWidthUnit(labelWidth) {
-
-
-
-
- return this.num2px(this.labelWidth ? this.labelWidth : (labelWidth || (this.label ? 65 : 'auto')))
-
-
- },
-
- _labelPosition() {
- if (this.form) return this.form.labelPosition || 'left'
- return 'left'
- },
-
- isTrigger(rule, itemRlue, parentRule) {
-
- if (rule === 'submit' || !rule) {
- if (rule === undefined) {
- if (itemRlue !== 'bind') {
- if (!itemRlue) {
- return parentRule === '' ? 'bind' : 'submit';
- }
- return 'submit';
- }
- return 'bind';
- }
- return 'submit';
- }
- return 'bind';
- },
- num2px(num) {
- if (typeof num === 'number') {
- return `${num}px`
- }
- return num
- }
- }
- };
- </script>
- <style lang="scss">
- .uni-forms-item {
- position: relative;
- display: flex;
-
-
- padding-bottom: 22px;
-
-
- margin-bottom: 22px;
-
- flex-direction: row;
- &__label {
- display: flex;
- flex-direction: row;
- align-items: center;
- text-align: left;
- font-size: 14px;
- color: #606266;
- height: 36px;
- padding: 0 12px 0 0;
-
- vertical-align: middle;
- flex-shrink: 0;
-
-
- box-sizing: border-box;
-
- &.no-label {
- padding: 0;
- }
- }
- &__content {
-
-
-
-
- position: relative;
- font-size: 14px;
- flex: 1;
-
- box-sizing: border-box;
-
- flex-direction: row;
-
-
- &>uni-easyinput,
- &>uni-data-picker {
- width: 100%;
- }
-
- }
- & .uni-forms-item__nuve-content {
- display: flex;
- flex-direction: column;
- flex: 1;
- }
- &__error {
- color: #f56c6c;
- font-size: 12px;
- line-height: 1;
- padding-top: 4px;
- position: absolute;
-
- top: 100%;
- left: 0;
- transition: transform 0.3s;
- transform: translateY(-100%);
-
-
- bottom: 5px;
-
- opacity: 0;
- .error-text {
-
- color: #f56c6c;
- font-size: 12px;
- }
- &.msg--active {
- opacity: 1;
- transform: translateY(0%);
- }
- }
-
- &.is-direction-left {
- flex-direction: row;
- }
- &.is-direction-top {
- flex-direction: column;
- .uni-forms-item__label {
- padding: 0 0 8px;
- line-height: 1.5715;
- text-align: left;
-
- white-space: initial;
-
- }
- }
- .is-required {
-
- color: #dd524d;
- font-weight: bold;
- }
- }
- .uni-forms-item--border {
- margin-bottom: 0;
- padding: 10px 0;
-
- border-top: 1px #eee solid;
-
- .uni-forms-item__content {
- flex-direction: column;
- justify-content: flex-start;
- align-items: flex-start;
- .uni-forms-item__error {
- position: relative;
- top: 5px;
- left: 0;
- padding-top: 0;
- }
- }
-
-
- display: flex;
- flex-direction: column;
- .uni-forms-item__error {
- position: relative;
- top: 0px;
- left: 0;
- padding-top: 0;
- margin-top: 5px;
- }
-
- }
- .is-first-border {
-
- border: none;
-
-
- border-width: 0;
-
- }
- </style>
|