Skip to content

Commit 95735be

Browse files
authored
feat: 组件text新增金额类型,前后插槽,下划线等功能 (#452)
1 parent 364cfbf commit 95735be

File tree

4 files changed

+150
-17
lines changed

4 files changed

+150
-17
lines changed

docs/component/text.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,66 @@
6969
<wd-text text="18888888888" mode="phone" :format="true"></wd-text>
7070
```
7171

72+
## lines
73+
74+
设置 `lines` 属性,文本显示的行数,如果设置,超出此行数,将会显示省略号。最大值为5。
75+
76+
```html
77+
<wd-text :text="text" :lines="2" size="16px"></wd-text>
78+
```
79+
80+
## lineHeight
81+
82+
设置 `lineHeight` 文本行高。
83+
84+
```html
85+
<wd-text :text="text" lineHeight="20px"></wd-text>
86+
```
87+
88+
## 前后插槽
89+
90+
设置 `prefix` `suffix` 插槽。
91+
92+
```html
93+
<wd-text
94+
text="12345678901"
95+
mode="phone"
96+
format
97+
type="primary"
98+
prefix="Prefix"
99+
suffix="Suffix"
100+
/>
101+
102+
<wd-text text="12345678901" mode="phone" format type="primary">
103+
<template #prefix>
104+
<text>Prefix</text>
105+
</template>
106+
<template #suffix>Suffix</template>
107+
</wd-text>
108+
```
109+
110+
## 金额
111+
112+
设置 `mode="price"`
113+
114+
```html
115+
<wd-text
116+
text="16354.156"
117+
mode="price"
118+
type="success"
119+
decoration="line-through"
120+
prefix=""
121+
/>
122+
```
123+
124+
## 文字装饰
125+
126+
设置 `decoration` 文字装饰,下划线,中划线等。
127+
128+
```html
129+
<wd-text :text="text" type="warning" decoration="underline"/>
130+
```
131+
72132
## 事件
73133

74134
```html
@@ -91,12 +151,15 @@ function clickTest() {
91151
| type | 主题类型 | string | 'primary' / 'error' / 'warning' / 'success' | default | 1.3.4 |
92152
| text | 文字 | string | - | | 1.3.4 |
93153
| size | 字体大小 | string | - | - | 1.3.4 |
94-
| mode | 文本处理的匹配模式 | string | 'text-普通文本' / 'date - 日期' / 'phone - 手机号' / 'name - 姓名' | text | 1.3.4 |
154+
| mode | 文本处理的匹配模式 | string | 'text-普通文本' / 'date - 日期' / 'phone - 手机号' / 'name - 姓名' / 'price - 金额' | text | 1.3.4+ |
95155
| bold | 是否粗体,默认 normal | boolean | - | false | 1.3.4 |
96156
| format | 是否脱敏 | boolean | 当 mode 为 phone 和 name 时生效 | false | 1.3.4 |
97157
| color | 文字颜色 | string | - | - | 1.3.4 |
98158
| lines | 文本显示的行数,如果设置,超出此行数,将会显示省略号。最大值为 5。 | Number | - | - | 1.3.4 |
99159
| lineHeight | 文本行高 | string | - | | 1.3.4 |
160+
| decoration | 文字装饰,下划线,中划线等 | string | underline/line-through/overline | | 1.3.4+ |
161+
| prefix | 前置插槽 | string | - | | 1.3.4+ |
162+
| suffix | 后置插槽 | string | - | | 1.3.4+ |
100163

101164
## Events
102165

src/pages/text/Index.vue

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
<demo-block title="脱敏">
3030
<view style="display: flex; align-items: center">
31-
<wd-text text="李四" mode="name" :format="true"></wd-text>
3231
<wd-text text="张长三" mode="name" :format="true"></wd-text>
3332
<wd-text text="18888888888" mode="phone" :format="true"></wd-text>
3433
</view>
@@ -51,6 +50,31 @@
5150
<view><wd-text text="1719976636911" mode="date"></wd-text></view>
5251
</view>
5352
</demo-block>
53+
54+
<demo-block title="前后插槽">
55+
<view>
56+
<wd-text text="12345678901" mode="phone" format type="primary" prefix="Prefix" suffix="Suffix" />
57+
<br />
58+
<wd-text text="12345678901" mode="phone" format type="primary">
59+
<template #prefix>
60+
<text>Prefix</text>
61+
</template>
62+
<template #suffix>Suffix</template>
63+
</wd-text>
64+
</view>
65+
</demo-block>
66+
67+
<demo-block title="金额">
68+
<view>
69+
<wd-text text="16354.156" mode="price" type="success" decoration="line-through" prefix="" />
70+
</view>
71+
</demo-block>
72+
73+
<demo-block title="文字装饰">
74+
<view>
75+
<wd-text :text="text" type="warning" decoration="underline" />
76+
</view>
77+
</demo-block>
5478
</page-wraper>
5579
</template>
5680
<script lang="ts" setup>

src/uni_modules/wot-design-uni/components/wd-text/types.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ export const textProps = {
2626
size: makeStringProp(''),
2727
/**
2828
* 文本处理的匹配模式
29-
* 可选值:'text-普通文本' / 'date - 日期' / 'phone - 手机号' / 'name - 姓名'
29+
* 可选值:'text-普通文本' / 'date - 日期' / 'phone - 手机号' / 'name - 姓名' / 'price - 金额'
3030
* 类型:string
3131
* 默认值:'text'
3232
*/
3333
mode: makeStringProp('text'),
34+
/**
35+
* 文字装饰,下划线,中划线等
36+
* 可选值:'underline/line-through/overline'
37+
* 类型:string
38+
* 默认值:'none'
39+
*/
40+
decoration: makeStringProp('none'),
41+
3442
/**
3543
* mode=phone时,点击文本是否拨打电话
3644
* 类型:boolean
@@ -55,6 +63,18 @@ export const textProps = {
5563
* 默认值:''
5664
*/
5765
color: makeStringProp(''),
66+
/**
67+
* 前置插槽
68+
* 类型:string
69+
* 默认值:''
70+
*/
71+
prefix: makeStringProp(''),
72+
/**
73+
* 后置插槽
74+
* 类型:string
75+
* 默认值:''
76+
*/
77+
suffix: makeStringProp(''),
5878
/**
5979
* 文本显示的行数,如果设置,超出此行数,将会显示省略号。最大值为5。
6080
*/

src/uni_modules/wot-design-uni/components/wd-text/wd-text.vue

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
2-
<view>
3-
<text @click="handleClick" :class="rootClass" :style="rootStyle">
4-
{{ formattedText }}
5-
</text>
6-
</view>
2+
<text @click="handleClick" :class="rootClass" :style="rootStyle">
3+
<slot name="prefix">{{ props.prefix }}</slot>
4+
<text>{{ formattedText }}</text>
5+
<slot name="suffix">{{ props.suffix }}</slot>
6+
</text>
77
</template>
88

99
<script lang="ts">
@@ -23,25 +23,26 @@ import { isDef, objToStyle } from '../common/util'
2323
import { textProps } from './types'
2424
import { dayjs } from '../common/dayjs'
2525
26+
// 获取组件的 props 和 emit 函数
2627
const props = defineProps(textProps)
2728
const emit = defineEmits(['click'])
2829
30+
// 存储文本类名的响应式变量
2931
const textClass = ref<string>('')
3032
31-
// 合并 watch 逻辑
33+
// 监听 props 变化,合并 watch 逻辑
3234
watch(
3335
() => ({
3436
type: props.type,
3537
text: props.text,
3638
mode: props.mode,
37-
call: props.call,
3839
color: props.color,
3940
bold: props.bold,
4041
lines: props.lines,
4142
format: props.format
4243
}),
4344
({ type }) => {
44-
// type 验证
45+
// 验证 type 属性
4546
if (type && !['primary', 'error', 'warning', 'success', 'default'].includes(type)) {
4647
console.error(`type must be one of ${type.toString()}`)
4748
}
@@ -50,10 +51,12 @@ watch(
5051
{ deep: true, immediate: true }
5152
)
5253
54+
// 计算根元素的类名
5355
const rootClass = computed(() => {
5456
return `wd-text ${props.customClass} ${textClass.value}`
5557
})
5658
59+
// 计算根元素的样式
5760
const rootStyle = computed(() => {
5861
const rootStyle: Record<string, any> = {}
5962
if (props.color) {
@@ -65,12 +68,16 @@ const rootStyle = computed(() => {
6568
if (props.lineHeight) {
6669
rootStyle['line-height'] = `${props.lineHeight}`
6770
}
71+
if (props.decoration) {
72+
rootStyle['text-decoration'] = `${props.decoration}`
73+
}
6874
return `${objToStyle(rootStyle)};${props.customStyle}`
6975
})
7076
77+
// 计算文本类名的函数
7178
function computeTextClass() {
7279
const { type, color, bold, lines } = props
73-
let textClassList: string[] = []
80+
const textClassList: string[] = []
7481
if (!color) {
7582
textClassList.push(`is-${type}`)
7683
}
@@ -81,7 +88,7 @@ function computeTextClass() {
8188
textClass.value = textClassList.join(' ')
8289
}
8390
84-
// 格式化文本函数
91+
// 格式化文本的函数
8592
function formatText(text: string, format: boolean, mode: string): string {
8693
if (format) {
8794
if (mode === 'phone') {
@@ -95,14 +102,33 @@ function formatText(text: string, format: boolean, mode: string): string {
95102
return text
96103
}
97104
105+
// 格式化数字的函数
106+
function formatNumber(num: number | string): string {
107+
num = Number(num).toFixed(2)
108+
const x = num.split('.')
109+
let x1 = x[0]
110+
const x2 = x.length > 1 ? '.' + x[1] : ''
111+
const rgx = /(\d+)(\d{3})/
112+
while (rgx.test(x1)) {
113+
x1 = x1.replace(rgx, '$1,$2')
114+
}
115+
return x1 + x2
116+
}
117+
118+
// 计算格式化后的文本
98119
const formattedText = computed(() => {
99-
if (props.mode === 'date') {
100-
return dayjs(Number(props.text)).format('YYYY-MM-DD')
120+
const { text, mode, format } = props
121+
if (mode === 'date') {
122+
return dayjs(Number(text)).format('YYYY-MM-DD')
123+
}
124+
if (mode === 'price') {
125+
return formatNumber(text)
101126
}
102-
return formatText(props.text, props.format, props.mode)
127+
return formatText(text, format, mode)
103128
})
104129
105-
function handleClick(event: any) {
130+
// 处理点击事件
131+
function handleClick(event: Event) {
106132
emit('click', event)
107133
}
108134
</script>

0 commit comments

Comments
 (0)