docs(Toast): add word-break demo (#11156)

This commit is contained in:
neverland 2022-10-20 22:33:37 +08:00 committed by GitHub
parent 299b558439
commit 882bfb3924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 100 additions and 9 deletions

View File

@ -96,6 +96,24 @@ showToast({
}); });
``` ```
### Word Break
Using `wordBreak` option to set whether line breaks appear wherever the text would otherwise overflow its content box. The default value is `break-all`, and can be set to `break-word` or `normal`.
```js
import { showToast } from 'vant';
showToast({
message: 'This message will contain a incomprehensibilities long word.',
wordBreak: 'break-all',
});
showToast({
message: 'This message will contain a incomprehensibilities long word.',
wordBreak: 'break-word',
});
```
### Update Message ### Update Message
```js ```js
@ -199,7 +217,7 @@ Vant exports following Toast utility functions:
| type | Can be set to `loading` `success` `fail` `html` | _ToastType_ | `text` | | type | Can be set to `loading` `success` `fail` `html` | _ToastType_ | `text` |
| position | Can be set to `top` `middle` `bottom` | _ToastPosition_ | `middle` | | position | Can be set to `top` `middle` `bottom` | _ToastPosition_ | `middle` |
| message | Message | _string_ | `''` | | message | Message | _string_ | `''` |
| wordBreak | Can be set to `break-normal` `break-all` `break-word` | _ToastWordBreak_ | `''` | | wordBreak | Can be set to `normal` `break-all` `break-word` | _ToastWordBreak_ | `'break-all'` |
| icon | Custom icon | _string_ | - | | icon | Custom icon | _string_ | - |
| iconSize | Custom icon size | _number \| string_ | `36px` | | iconSize | Custom icon size | _number \| string_ | `36px` |
| iconPrefix | Icon className prefix | _string_ | `van-icon` | | iconPrefix | Icon className prefix | _string_ | `van-icon` |

View File

@ -110,6 +110,26 @@ showToast({
}); });
``` ```
### 文字换行方式
通过 `wordBreak` 选择可以控制 Toast 中的文字过长时的截断方式,默认值为 `break-all`,可选值为 `break-word``normal`
```js
import { showToast } from 'vant';
// 换行时截断单词
showToast({
message: 'This message will contain a incomprehensibilities long word.',
wordBreak: 'break-all',
});
// 换行时不截断单词
showToast({
message: 'This message will contain a incomprehensibilities long word.',
wordBreak: 'break-word',
});
```
### 动态更新提示 ### 动态更新提示
执行 Toast 方法时会返回对应的 Toast 实例,通过修改实例上的 `message` 属性可以实现动态更新提示的效果。 执行 Toast 方法时会返回对应的 Toast 实例,通过修改实例上的 `message` 属性可以实现动态更新提示的效果。
@ -216,7 +236,7 @@ Vant 中导出了以下 Toast 相关的辅助函数:
| type | 提示类型,可选值为 `loading` `success`<br>`fail` `html` | _ToastType_ | `text` | | type | 提示类型,可选值为 `loading` `success`<br>`fail` `html` | _ToastType_ | `text` |
| position | 位置,可选值为 `top` `bottom` | _ToastPosition_ | `middle` | | position | 位置,可选值为 `top` `bottom` | _ToastPosition_ | `middle` |
| message | 文本内容,支持通过`\n`换行 | _string_ | `''` | | message | 文本内容,支持通过`\n`换行 | _string_ | `''` |
| wordBreak | 文本内容的换行方式,可选值为 `break-normal` `break-all` `break-word` | _ToastWordBreak_ | `''` | | wordBreak | 文本内容的换行方式,可选值为 `normal` `break-all` `break-word` | _ToastWordBreak_ | `'break-all'` |
| icon | 自定义图标,支持传入图标名称或图片链接,等同于 Icon 组件的 [name 属性](#/zh-CN/icon#props) | _string_ | - | | icon | 自定义图标,支持传入图标名称或图片链接,等同于 Icon 组件的 [name 属性](#/zh-CN/icon#props) | _string_ | - |
| iconSize | 图标大小,如 `20px` `2em`,默认单位为 `px` | _number \| string_ | `36px` | | iconSize | 图标大小,如 `20px` `2em`,默认单位为 `px` | _number \| string_ | `36px` |
| iconPrefix | 图标类名前缀,等同于 Icon 组件的 [class-prefix 属性](#/zh-CN/icon#props) | _string_ | `van-icon` | | iconPrefix | 图标类名前缀,等同于 Icon 组件的 [class-prefix 属性](#/zh-CN/icon#props) | _string_ | `van-icon` |

View File

@ -152,7 +152,11 @@ export default defineComponent({
return () => ( return () => (
<Popup <Popup
class={[ class={[
bem([props.position, props.wordBreak, { [props.type]: !props.icon }]), bem([
props.position,
props.wordBreak === 'normal' ? 'break-normal' : props.wordBreak,
{ [props.type]: !props.icon },
]),
props.className, props.className,
]} ]}
lockScroll={false} lockScroll={false}

View File

@ -11,6 +11,7 @@ import VanToast, {
showLoadingToast, showLoadingToast,
} from '..'; } from '..';
import type { LoadingType } from '../../loading'; import type { LoadingType } from '../../loading';
import type { ToastWordBreak } from '../types';
const t = useTranslate({ const t = useTranslate({
'zh-CN': { 'zh-CN': {
@ -23,6 +24,9 @@ const t = useTranslate({
title2: '加载提示', title2: '加载提示',
title3: '成功/失败提示', title3: '成功/失败提示',
success: '成功提示', success: '成功提示',
breakAll: '换行时截断单词',
breakWord: '换行时不截断单词',
wordBreak: '文字换行方式',
customIcon: '自定义图标', customIcon: '自定义图标',
customImage: '自定义图片', customImage: '自定义图片',
loadingType: '自定义加载图标', loadingType: '自定义加载图标',
@ -42,6 +46,9 @@ const t = useTranslate({
title2: 'Loading', title2: 'Loading',
title3: 'Success/Fail', title3: 'Success/Fail',
success: 'Success', success: 'Success',
wordBreak: 'Word Break',
breakAll: 'Break All',
breakWord: 'Break Word',
customIcon: 'Custom Icon', customIcon: 'Custom Icon',
customImage: 'Custom Image', customImage: 'Custom Image',
loadingType: 'Loading Type', loadingType: 'Loading Type',
@ -108,6 +115,13 @@ const showCustomToast = () => {
}, 1000); }, 1000);
}; };
const showWordBreakToast = (wordBreak: ToastWordBreak) => {
showToast({
message: 'This message will contain a incomprehensibilities long word.',
wordBreak,
});
};
const show = ref(false); const show = ref(false);
const image = cdnURL('cat.jpeg'); const image = cdnURL('cat.jpeg');
</script> </script>
@ -143,6 +157,19 @@ const image = cdnURL('cat.jpeg');
<van-cell is-link :title="t('positionBottom')" @click="showBottomToast" /> <van-cell is-link :title="t('positionBottom')" @click="showBottomToast" />
</demo-block> </demo-block>
<demo-block card :title="t('wordBreak')">
<van-cell
is-link
:title="t('breakAll')"
@click="showWordBreakToast('break-all')"
/>
<van-cell
is-link
:title="t('breakWord')"
@click="showWordBreakToast('break-word')"
/>
</demo-block>
<demo-block card :title="t('updateMessage')"> <demo-block card :title="t('updateMessage')">
<van-cell is-link :title="t('updateMessage')" @click="showCustomToast" /> <van-cell is-link :title="t('updateMessage')" @click="showCustomToast" />
</demo-block> </demo-block>

View File

@ -46,11 +46,6 @@
word-wrap: normal; word-wrap: normal;
} }
&-all {
word-wrap: normal;
word-break: break-all;
}
&-word { &-word {
word-break: normal; word-break: normal;
word-wrap: break-word; word-wrap: break-word;

View File

@ -21,6 +21,7 @@ export type {
ToastOptions, ToastOptions,
ToastPosition, ToastPosition,
ToastThemeVars, ToastThemeVars,
ToastWordBreak,
} from './types'; } from './types';
declare module 'vue' { declare module 'vue' {

View File

@ -115,6 +115,32 @@ exports[`should render demo and match snapshot 1`] = `
</i> </i>
</div> </div>
</div> </div>
<div>
<div class="van-cell van-cell--clickable"
role="button"
tabindex="0"
>
<div class="van-cell__title">
<span>
Break All
</span>
</div>
<i class="van-badge__wrapper van-icon van-icon-arrow van-cell__right-icon">
</i>
</div>
<div class="van-cell van-cell--clickable"
role="button"
tabindex="0"
>
<div class="van-cell__title">
<span>
Break Word
</span>
</div>
<i class="van-badge__wrapper van-icon van-icon-arrow van-cell__right-icon">
</i>
</div>
</div>
<div> <div>
<div class="van-cell van-cell--clickable" <div class="van-cell van-cell--clickable"
role="button" role="button"

View File

@ -4,7 +4,7 @@ import type { Numeric } from '../utils';
export type ToastType = 'text' | 'loading' | 'success' | 'fail' | 'html'; export type ToastType = 'text' | 'loading' | 'success' | 'fail' | 'html';
export type ToastPosition = 'top' | 'middle' | 'bottom'; export type ToastPosition = 'top' | 'middle' | 'bottom';
export type ToastWordBreak = 'break-all' | 'break-word' | 'break-normal'; export type ToastWordBreak = 'break-all' | 'break-word' | 'normal';
export type ToastOptions = { export type ToastOptions = {
icon?: string; icon?: string;