Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: form表单新增errorType错误提示类型 #487

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions docs/component/form.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,78 @@ function handleSubmit1() {
}
```

:::

## 校验错误提示方式

1. `message`:默认为输入框下方用文字进行提示
2. `toast`:以"toast"提示的方式弹出错误信息,每次只弹出最前面的那个表单域的错误信息
3. `none`:不会进行任何提示

::: details 错误提示方式
::: code-group

```html [vue]
<wd-form ref="form" :model="model" :errorType="errorType">
<wd-cell-group border>
<wd-input
label="用户名"
label-width="100px"
prop="value1"
clearable
v-model="model.value1"
placeholder="请输入用户名"
:rules="[{ required: true, message: '请填写用户名' }]"
/>
<wd-input
label="密码"
label-width="100px"
prop="value2"
show-password
clearable
v-model="model.value2"
placeholder="请输入密码"
:rules="[{ required: true, message: '请填写密码' }]"
/>
</wd-cell-group>
<view class="footer">
<wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
</view>
</wd-form>
```

```typescript [typescript]
<script lang="ts" setup>
const { success: showSuccess } = useToast()
const errorType = ref<string>('message')
const model = reactive<{
value1: string
value2: string
}>({
value1: '',
value2: ''
})

const form = ref()

function handleSubmit1() {
form.value
.validate()
.then(({ valid, errors }) => {
if (valid) {
showSuccess({
msg: '校验通过'
})
}
})
.catch((error) => {
console.log(error, 'error')
})
}
</script>
```


:::

## 校验规则
Expand Down Expand Up @@ -849,6 +921,7 @@ function handleIconClick() {
| model | 表单数据对象 | `Record<string, any>` | - | - | 0.2.0 |
| rules | 表单验证规则 | `FormRules` | - | - | 0.2.0 |
| resetOnChange | 表单数据变化时是否重置表单提示信息(设置为false时需要开发者单独对变更项进行校验) | `boolean` | - | `true` | 0.2.16 |
| errorType | 校验错误提示方式 | `toast/message/none` | - | `message` | 1.3.6+ |

### FormItemRule 数据结构

Expand Down
9 changes: 8 additions & 1 deletion src/uni_modules/wot-design-uni/components/wd-form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ export const formProps = {
/**
* 是否在输入时重置表单校验信息
*/
resetOnChange: makeBooleanProp(true)
resetOnChange: makeBooleanProp(true),
/**
* 错误提示类型
*/
errorType: {
type: String as PropType<'toast' | 'message' | 'none'>,
default: 'message'
}
}
export type FormProps = ExtractPropTypes<typeof formProps>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<view :class="`wd-form ${customClass}`" :style="customStyle">
<slot></slot>
<wd-toast v-if="props.errorType === 'toast'" />
</view>
</template>

Expand All @@ -19,8 +20,10 @@ export default {
import { reactive, watch } from 'vue'
import { deepClone, getPropByPath, isDef, isPromise } from '../common/util'
import { useChildren } from '../composables/useChildren'
import { useToast } from '../wd-toast'
import { type FormRules, FORM_KEY, type ErrorMessage, formProps, type FormExpose } from './types'

const toast = useToast()
const props = defineProps(formProps)

const { children, linkChildren } = useChildren(FORM_KEY)
Expand Down Expand Up @@ -148,7 +151,11 @@ function getMergeRules() {
}

function showMessage(errorMsg: ErrorMessage) {
if (errorMsg.message) {
if (!errorMsg.message) return

if (props.errorType === 'toast') {
toast.show(errorMsg.message)
} else if (props.errorType === 'message') {
errorMessages[errorMsg.prop] = errorMsg.message
}
}
Expand Down