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(TextEllipsis): add action slot #12560

Merged
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cc983e7
feat: slot
nemo-shen Jan 9, 2024
5bee727
fix: update test snapshot
nemo-shen Jan 9, 2024
f1ce2c8
fix: translate
nemo-shen Jan 9, 2024
815c112
docs(textEllipsis): update readme
nemo-shen Jan 10, 2024
ec55ea2
chore(textEllipsis): update readme
nemo-shen Jan 10, 2024
e2c40f5
docs(textEllipsis): update usage in README
nemo-shen Jan 10, 2024
969c6f4
docs(TextEllipsis): update usage
nemo-shen Jan 10, 2024
c2f0359
Merge branch 'main' into feature/text-ellipsis_action-prop_0109
nemo-shen Jan 10, 2024
c8ba6d3
Merge branch 'main' into feature/text-ellipsis_action-prop_0109
nemo-shen Jan 10, 2024
25722d9
test(TextEllipsis): add unit test
nemo-shen Jan 10, 2024
e4ea7fc
chore: demo title
nemo-shen Jan 10, 2024
62ce9a0
feat(TextEllipsis): add `action` slot
nemo-shen Jan 11, 2024
86d71ee
docs(TextEllipsis): update README
nemo-shen Jan 11, 2024
4a4a578
chore: remove
nemo-shen Jan 11, 2024
ee74fef
docs(textEllipsis): update README
nemo-shen Jan 11, 2024
93b5eaa
docs(TextEllipsis): update README
nemo-shen Jan 11, 2024
b9c3be2
docs(TextEllipsis): update README
nemo-shen Jan 11, 2024
c487482
docs(TextEllipsis): update README
nemo-shen Jan 11, 2024
bf8a744
test(TextEllipsis): update unit test
nemo-shen Jan 11, 2024
defd5cc
docs(TextEllipsis): update README
nemo-shen Jan 11, 2024
5f76eaf
docs(TextEllipsis): update README
nemo-shen Jan 11, 2024
6ad08f1
fix(TextEllipsis): udpate demo
nemo-shen Jan 11, 2024
8468d22
Merge branch 'main' into feature/text-ellipsis_action-prop_0109
nemo-shen Jan 11, 2024
f386512
Apply suggestions from code review
chenjiahan Jan 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/vant/src/text-ellipsis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,28 @@ export default {
};
```

### Custom Action

Use `action` slots to custom action
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved

```html
<van-text-ellipsis :content="text">
<template #action="{ expanded }">
{{ expanded ? 'Collapse' : 'Expand' }}
</template>
</van-text-ellipsis>
```

```js
export default {
setup() {
const text =
'Take your time and be patient. Life itself will eventually answer all those questions it once raised for you.';
return { text };
},
};
```

## API

### Props
Expand Down Expand Up @@ -154,6 +176,12 @@ Use [ref](https://vuejs.org/guide/essentials/template-refs.html) to get TextElli
| ------ | ---------------------- | -------------------- | ------------ |
| toggle | Toggle expanded status | _expanded?: boolean_ | - |

### Slots

| Name | Description | SlotProps |
| --------------- | ------------- | ----------------------- |
| action `v4.8.3` | Custom action | _{ expanded: boolean }_ |

### Types

The component exports the following type definitions:
Expand Down
25 changes: 25 additions & 0 deletions packages/vant/src/text-ellipsis/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ export default {
};
```

### 自定义操作内容

通过插槽 `action` 自定义操作内容
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved

```html
<van-text-ellipsis :content="text">
<template #action="{ expanded }">{{ expanded ? '收起' : '展开' }}</template>
</van-text-ellipsis>
```

```js
export default {
setup() {
const text = '慢慢来,不要急,生活给你出了难题,可也终有一天会给出答案。';
return { text };
},
};
```

## API

### Props
Expand Down Expand Up @@ -151,6 +170,12 @@ export default {
| --- | --- | --- | --- |
| toggle | 切换文本的展开状态,传 `true` 为展开,`false` 为收起,不传参为切换 | _expanded?: boolean_ | - |

### Slots

| 名称 | 说明 | 参数 |
| --------------- | ---------- | ----------------------- |
| action `v4.8.3` | 自定义操作 | _{ expanded: boolean }_ |

### 类型定义

组件导出以下类型定义:
Expand Down
17 changes: 11 additions & 6 deletions packages/vant/src/text-ellipsis/TextEllipsis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default defineComponent({

emits: ['clickAction'],

setup(props, { emit }) {
setup(props, { emit, slots }) {
const text = ref('');
const expanded = ref(false);
const hasAction = ref(false);
Expand Down Expand Up @@ -196,11 +196,16 @@ export default defineComponent({
emit('clickAction', event);
};

const renderAction = () => (
<span class={bem('action')} onClick={onClickAction}>
{actionText.value}
</span>
);
const renderAction = () => {
const action = slots.action
? slots.action({ expanded: expanded.value })
: actionText.value;
return (
<span class={bem('action')} onClick={onClickAction}>
{action}
</span>
);
};

onMounted(calcEllipsised);

Expand Down
10 changes: 10 additions & 0 deletions packages/vant/src/text-ellipsis/demo/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const t = useTranslate({
collapsePosition: '自定义省略位置',
collapseStart: '头部省略',
collapseMiddle: '中部省略',
customAction: '自定义操作内容',
},
'en-US': {
text1:
Expand All @@ -31,6 +32,7 @@ const t = useTranslate({
collapsePosition: 'Custom Collapse Position',
collapseStart: 'Head Area Collapse Position',
collapseMiddle: 'Middle Area Collapse Position',
customAction: 'Custom Action',
},
});
</script>
Expand Down Expand Up @@ -77,6 +79,14 @@ const t = useTranslate({
position="middle"
/>
</demo-block>

<demo-block :title="t('customAction')">
<van-text-ellipsis :content="t('text1')">
<template #action="{ expanded }">
{{ expanded ? t('collapseText') : t('expandText') }}
</template>
</van-text-ellipsis>
</demo-block>
</demo-block>
</template>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ exports[`should render demo and match snapshot 1`] = `
<div class="van-text-ellipsis">
</div>
</div>
<div>
<!--[-->
<div class="van-text-ellipsis">
</div>
</div>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@ exports[`should render demo and match snapshot 1`] = `
</span>
</div>
</div>
<div>
<div class="van-text-ellipsis">
...
<span class="van-text-ellipsis__action">
expand
</span>
</div>
</div>
</div>
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`should render action slot correctly 1`] = `
<div class="van-text-ellipsis">
Vant is a lightweight, customizable mobile component library th...
<span class="van-text-ellipsis__action">
Collapse
</span>
</div>
`;

exports[`should render action slot correctly 2`] = `
<div class="van-text-ellipsis">
Vant is a lightweight, customizable mobile component library that was open sourced in 2017. Currently Vant officially provides Vue 2 version, Vue 3 version and WeChat applet version, and the community team maintains React version and Alipay applet version.
<span class="van-text-ellipsis__action">
Expand
</span>
</div>
`;

exports[`should render action slot correctly 3`] = `
<div class="van-text-ellipsis">
Vant is a lightweight, customizable mobile component library th...
<span class="van-text-ellipsis__action">
Collapse
</span>
</div>
`;

exports[`should render content correctly 1`] = `
<div class="van-text-ellipsis">
Vant is a lightweight, customizable mobile component library th...
Expand Down
25 changes: 24 additions & 1 deletion packages/vant/src/text-ellipsis/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mount } from '../../../test';
import { nextTick } from 'vue';
import TextEllipsis from '..';
import TextEllipsis, { type TextEllipsisInstance } from '..';

const originGetComputedStyle = window.getComputedStyle;

Expand Down Expand Up @@ -32,6 +32,29 @@ afterAll(() => {
window.getComputedStyle = originGetComputedStyle;
});

test('should render action slot correctly', async () => {
const wrapper = mount(TextEllipsis, {
props: {
content,
},
slots: {
action: ({ expanded }) => (expanded ? 'Expand' : 'Collapse'),
},
});

await nextTick();

expect(wrapper.html()).toMatchSnapshot();

(wrapper.vm as TextEllipsisInstance).toggle();
await nextTick();
expect(wrapper.html()).toMatchSnapshot();

(wrapper.vm as TextEllipsisInstance).toggle();
await nextTick();
expect(wrapper.html()).toMatchSnapshot();
});

test('should render content correctly', async () => {
const wrapper = mount(TextEllipsis, {
props: {
Expand Down