Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Hooke
/
custom_form
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
hookehuyr
2023-04-06 16:54:42 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
01aebc0165934eae3d0f11b86305df1765efacb6
01aebc01
1 parent
3b455eb8
✨ feat(手机号控件): 样式和功能调整
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
138 additions
and
88 deletions
components.d.ts
src/components/PhoneField/index.vue
src/hooks/useComponentType.js
src/pages/table/index.vue
components.d.ts
View file @
01aebc0
...
...
@@ -31,6 +31,7 @@ declare module '@vue/runtime-core' {
NutCheckboxGroup
:
typeof
import
(
'@nutui/nutui-taro'
)[
'CheckboxGroup'
]
NutConfigProvider
:
typeof
import
(
'@nutui/nutui-taro'
)[
'ConfigProvider'
]
NutDialog
:
typeof
import
(
'@nutui/nutui-taro'
)[
'Dialog'
]
NutField
:
typeof
import
(
'@nutui/nutui-taro'
)[
'Field'
]
NutForm
:
typeof
import
(
'@nutui/nutui-taro'
)[
'Form'
]
NutFormItem
:
typeof
import
(
'@nutui/nutui-taro'
)[
'FormItem'
]
NutInput
:
typeof
import
(
'@nutui/nutui-taro'
)[
'Input'
]
...
...
src/components/PhoneField/index.vue
View file @
01aebc0
<!--
* @Date: 2022-09-02 10:46:03
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2023-0
3-20 07:45:28
* @FilePath: /
data-table
/src/components/PhoneField/index.vue
* @LastEditTime: 2023-0
4-06 16:52:13
* @FilePath: /
custom_form
/src/components/PhoneField/index.vue
* @Description: 手机输入框
-->
<template>
<div v-if="HideShow" class="phone-field-page">
<div class="label">
<
span v-if="item.component_props.required"> *</span
>
<
text v-if="item.component_props.required"> *</text
>
{{ item.component_props.label }}
</div>
<!-- <van-field
...
...
@@ -25,20 +25,26 @@
@touchstart.stop="openKeyboard($event)"
:border="false"
> -->
<
van-field
<
nut-input
:id="item.name"
v-model="i
tem.
value"
:
name
="item.name"
v-model="i
nput_
value"
:
label
="item.name"
type="digit"
maxlength="11"
max
-
length="11"
:placeholder="item.component_props.placeholder ? item.component_props.placeholder : '请输入手机号码'"
:rules="rules"
:required="item.component_props.required"
:disabled="item.component_props.readonly"
:border="false"
@blur="onBlur"
style="border: 1px solid #eaeaea; border-radius: 0.25rem; padding: 0.25rem 0.5rem;"
>
</van-field>
<van-field
</nut-input>
<div
v-if="show_error"
style="padding-left: 20px; color: red; font-size: 12px;"
>
{{ error_msg }}
</div>
<!-- <van-field
v-if="is_sms"
name="ignore"
v-model="sms"
...
...
@@ -58,46 +64,65 @@
:maxlength="11"
@blur="blurKeyboard()"
safe-area-inset-bottom
/>
/>
-->
</div>
</template>
<script setup>
import { wxInfo } from "@/utils/tools";
import $ from "jquery";
import { storeToRefs, mainStore } from "@/utils/generatePackage";
import Taro, { useLoad } from '@tarojs/taro'
import { ref, computed, watch, onMounted, nextTick } from "vue";
import { $ } from '@tarojs/extend';
import { storeToRefs } from "pinia";
import { mainStore } from '@/stores'
const props = defineProps({
item: Object,
});
const emit = defineEmits(["active"]);
// 隐藏显示
const HideShow = computed(() => {
return !props.item.component_props.disabled
})
// web端判断
const readonly = computed(() => wxInfo().isMobile);
// 打开短信验证模式
const is_sms = ref(false);
// 校验函数返回 true 表示校验通过,false 表示不通过
const validator = (val) => {
if (props.item.component_props.required && !val) {
return false;
} else if (val && !/1\d{10}/.test(val)) {
return false;
} else {
return true;
}
};
const input_value = ref('');
const show_error = ref(false);
const error_msg = ref('');
// 错误提示文案
const validatorMessage = (val, rule) => {
if (props.item.component_props.required && !val) {
return "手机号码不能为空";
} else if (val && !/1\d{10}/.test(val)) {
return "请输入正确手机号码";
const validPhone = () => {
if (props.item.component_props.required && !input_value.value) {
show_error.value = true;
error_msg.value = '必填项不能为空';
} else if (input_value.value && !/1\d{10}/.test(input_value.value)) {
show_error.value = true;
error_msg.value = '请输入正确手机号码';
} else {
show_error.value = false;
error_msg.value = '';
}
return !show_error.value;
};
const rules = [{ validator, message: validatorMessage }];
defineExpose({ validPhone, id: props.item.key });
const onBlur = () => {
// 发送自定义回调
props.item.value = {
key: "phone",
filed_name: props.item.key,
value: input_value.value,
};
emit("active", props.item.value);
validPhone()
}
const show = ref(false);
let content = "";
...
...
@@ -106,67 +131,71 @@ const store = mainStore();
const { fieldName } = storeToRefs(store);
// 监听字段变化
watch(
() => fieldName.value,
(v) => {
// 如果不是点击本输入框
if (v !== props.item.name) {
// 还原border颜色
$(`#${props.item.name}`).parent().css("border-color", "#eaeaea");
show.value = false;
document.getElementById("app").style.paddingBottom = "0";
}
}
);
const openKeyboard = (e) => {
if (props.item.component_props.readonly) return false; // 如果为只读,不能设置
// 键盘上移动
const target_to_view_height =
window.innerHeight - e.target.getBoundingClientRect().bottom; // 元素到适口高度
const target_top = document.body.scrollHeight - $(e.target).offset().top; // 元素到正文高度
let scroll_height = "";
if (target_to_view_height <= 250) {
document.getElementById("app").style.paddingBottom = "250px";
// 向上滚动位置
document.documentElement.scrollTop = $(e.target).offset().top - 244;
}
// 选中添加border颜色
content = $(e.target).parent();
// TAG: 自定义主题颜色
content.css("border-color", "#c2915f");
setTimeout(() => {
show.value = true;
}, 300);
// 记录点击field名
store.changeFieldName(props.item.name);
};
const blurKeyboard = () => {
show.value = false;
document.getElementById("app").style.paddingBottom = "0";
// 还原border颜色
content.css("border-color", "#eaeaea");
};
//
watch(
//
() => fieldName.value,
//
(v) => {
//
// 如果不是点击本输入框
//
if (v !== props.item.name) {
//
// 还原border颜色
//
$(`#${props.item.name}`).parent().css("border-color", "#eaeaea");
//
show.value = false;
//
document.getElementById("app").style.paddingBottom = "0";
//
}
//
}
//
);
//
const openKeyboard = (e) => {
//
if (props.item.component_props.readonly) return false; // 如果为只读,不能设置
//
// 键盘上移动
//
const target_to_view_height =
//
window.innerHeight - e.target.getBoundingClientRect().bottom; // 元素到适口高度
//
const target_top = document.body.scrollHeight - $(e.target).offset().top; // 元素到正文高度
//
let scroll_height = "";
//
if (target_to_view_height <= 250) {
//
document.getElementById("app").style.paddingBottom = "250px";
//
// 向上滚动位置
//
document.documentElement.scrollTop = $(e.target).offset().top - 244;
//
}
//
// 选中添加border颜色
//
content = $(e.target).parent();
//
// TAG: 自定义主题颜色
//
content.css("border-color", "#c2915f");
//
setTimeout(() => {
//
show.value = true;
//
}, 300);
//
// 记录点击field名
//
store.changeFieldName(props.item.name);
//
};
//
const blurKeyboard = () => {
//
show.value = false;
//
document.getElementById("app").style.paddingBottom = "0";
//
// 还原border颜色
//
content.css("border-color", "#eaeaea");
//
};
</script>
<style lang="less"
scoped
>
<style lang="less">
.phone-field-page {
.label {
padding:
1rem 1rem 0 1rem
;
font-size:
0.9rem
;
padding:
30px 30px 0 30px
;
font-size:
26px
;
font-weight: bold;
span
{
text
{
color: red;
}
}
:deep(.van-field__body) {
border: 1px solid #eaeaea;
border-radius: 0.25rem;
padding: 0.25rem 0.5rem;
input {
color: #323233;
}
.nut-input {
padding: 20px 25px;
}
// :deep(.van-field__body) {
// border: 1px solid #eaeaea;
// border-radius: 0.25rem;
// padding: 0.25rem 0.5rem;
// input {
// color: #323233;
// }
// }
}
</style>
...
...
src/hooks/useComponentType.js
View file @
01aebc0
...
...
@@ -10,7 +10,7 @@ import PickerField from '@/components/PickerField/index.vue'
// import DateTimePickerField from '@/components/DateTimePickerField/index.vue'
// import ImageUploaderField from '@/components/ImageUploaderField/index.vue'
// import FileUploaderField from '@/components/FileUploaderField/index.vue'
//
import PhoneField from '@/components/PhoneField/index.vue'
import
PhoneField
from
'@/components/PhoneField/index.vue'
// import EmailField from '@/components/EmailField/index.vue'
// import SignField from '@/components/SignField/index.vue'
// import RatePickerField from '@/components/RatePickerField/index.vue'
...
...
@@ -111,10 +111,10 @@ export function createComponentType(data) {
// if (item.component_props.tag === 'file_uploader') {
// item.component = FileUploaderField
// }
//
if (item.component_props.tag === 'phone') {
//
item.name = item.key
//
item.component = PhoneField
//
}
if
(
item
.
component_props
.
tag
===
'phone'
)
{
item
.
name
=
item
.
key
item
.
component
=
PhoneField
}
// if (item.component_props.tag === 'email') {
// item.name = item.key
// item.component = EmailField
...
...
src/pages/table/index.vue
View file @
01aebc0
<!--
* @Date: 2023-03-24 09:19:27
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2023-04-06 1
4:35:26
* @LastEditTime: 2023-04-06 1
6:38:28
* @FilePath: /custom_form/src/pages/table/index.vue
* @Description: 文件描述
-->
...
...
@@ -133,6 +133,7 @@ const checkbox = ref([]);
const multi_rule = ref([]);
const picker = ref([]);
const number = ref([]);
const phone = ref([]);
const area_picker = ref([]);
const image_uploader = ref([]);
const file_uploader = ref([]);
...
...
@@ -162,6 +163,9 @@ const setRefMap = (el, item) => {
if (item.component_props.tag === "number") {
number.value.push(el);
}
if (item.component_props.tag === "phone") {
phone.value.push(el);
}
if (item.component_props.tag === "area_picker") {
area_picker.value.push(el);
}
...
...
@@ -424,6 +428,9 @@ const onActive = (item) => {
if (item.key === "number") {
postData.value[item.filed_name] = item.value;
}
if (item.key === "phone") {
postData.value[item.filed_name] = item.value;
}
if (item.key === "image_uploader") {
postData.value[item.filed_name] = item.value;
}
...
...
@@ -541,6 +548,19 @@ const validOther = () => {
}
});
}
if (phone.value) {
// 手机号
phone.value.forEach((item, index) => {
if (!phone.value[index].validPhone()) {
valid = {
status: phone.value[index].validPhone(),
key: "phone",
id: phone.value[index]?.id
};
return false;
}
});
}
if (area_picker.value) {
// 省市区地址
area_picker.value.forEach((item, index) => {
...
...
Please
register
or
login
to post a comment