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(infinite): add reverse prop #6333

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- `n-data-table` adds `allowExport` prop for column.
- `n-date-picker` adds `year-range` prop.
- `n-tree-select` adds `header` slot, closes [#5915](https://github.com/tusen-ai/naive-ui/issues/5915).
- `n-infinite` adds `reverse` prop,closes [#6308](https://github.com/tusen-ai/naive-ui/issues/6308)

## 2.39.0

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- `n-data-table` 在列的配置中新增 `allowExport` 属性
- `n-date-picker` 新增 `year-range` 属性
- `n-tree-select` 新增 `header` 插槽,关闭 [#5915](https://github.com/tusen-ai/naive-ui/issues/5915)
- `n-infinite` 新增 `reverse` 属性,关闭 [#6308](https://github.com/tusen-ai/naive-ui/issues/6308)

## 2.39.0

Expand Down
44 changes: 28 additions & 16 deletions src/infinite-scroll/demos/enUS/basic.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@
# Basic
</markdown>

<script lang="ts">
import { defineComponent, ref } from 'vue'
<script setup lang="ts">
import { ref } from 'vue'

export default defineComponent({
setup() {
const count = ref(6)
const handleLoad = () => {
count.value += 1
}
return {
count,
handleLoad
}
const dataRef = ref<number[]>([])
const loading = ref(false)
async function handleLoad() {
loading.value = true
try {
const data = await loadData()
dataRef.value.push(...data)
}
})
finally {
loading.value = false
}
}
function loadData(): Promise<number[]> {
const index = dataRef.value.length
const result = Array.from({ length: 10 })
.fill(null)
.map((_, i) => index + i + 1)
return new Promise(resolve => setTimeout(() => resolve(result), 1000))
}
</script>

<template>
<n-infinite-scroll style="height: 240px" :distance="10" @load="handleLoad">
<div v-for="i in count" :key="i" class="item">
<n-infinite-scroll style="height: 240px" :distance="100" @load="handleLoad">
<div v-for="i in dataRef" :key="i" class="item">
{{ i }}
</div>
<div v-if="loading" class="text">
loading...
</div>
</n-infinite-scroll>
</template>

Expand All @@ -36,8 +46,10 @@ export default defineComponent({
margin-bottom: 10px;
background-color: rgba(0, 128, 0, 0.16);
}

.item:last-child {
margin-bottom: 0;
}
.text {
text-align: center;
}
</style>
8 changes: 8 additions & 0 deletions src/infinite-scroll/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Available since `2.38.2`.

```demo
basic.vue
reverse.vue
chat.vue
```

Expand All @@ -18,5 +19,12 @@ chat.vue
| Name | Type | Default | Description | Version |
| --- | --- | --- | --- | --- |
| distance | `number` | `0` | Distance threshold that triggers loading. | 2.38.2 |
| reverse | `boolean` | `false` | Whether to trigger load more from the top. Defaults to loading from bottom. | NEXT_VERSION |
| scrollbar-props | `Object` | `undefined` | Attribute reference [Scrollbar props](scrollbar#Scrollbar-Props). | 2.38.2 |
| on-load | `() => Promise<void> \| void` | `undefined` | The callback function when scrolling to the bottom. | 2.38.2 |

### Infinite Slots

| Name | Parameters | Description |
| ------- | ---------- | ------------------ |
| default | `()` | Infinite's content |
61 changes: 61 additions & 0 deletions src/infinite-scroll/demos/enUS/reverse.demo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<markdown>
# Reverse
</markdown>

<script setup lang="ts">
import { ref } from 'vue'

const dataRef = ref<number[]>([])
const loading = ref(false)
async function handleLoad() {
loading.value = true
try {
const data = await loadData()
dataRef.value.unshift(...data)
}
finally {
loading.value = false
}
}
function loadData(): Promise<number[]> {
const index = dataRef.value.length
const result = Array.from({ length: 10 })
.fill(null)
.map((_, i) => index + i + 1)
.reverse()
return new Promise(resolve => setTimeout(() => resolve(result), 1000))
}
</script>

<template>
<n-infinite-scroll
style="height: 240px"
reverse
:distance="100"
@load="handleLoad"
>
<div v-if="loading" class="text">
loading...
</div>
<div v-for="i in dataRef" :key="i" class="item">
{{ i }}
</div>
</n-infinite-scroll>
</template>

<style>
.item {
display: flex;
align-items: center;
height: 46px;
justify-content: center;
margin-bottom: 10px;
background-color: rgba(0, 128, 0, 0.16);
}
.item:last-child {
margin-bottom: 0;
}
.text {
text-align: center;
}
</style>
44 changes: 28 additions & 16 deletions src/infinite-scroll/demos/zhCN/basic.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@
# 基础
</markdown>

<script lang="ts">
import { defineComponent, ref } from 'vue'
<script setup lang="ts">
import { ref } from 'vue'

export default defineComponent({
setup() {
const count = ref(6)
const handleLoad = () => {
count.value += 1
}
return {
count,
handleLoad
}
const dataRef = ref<number[]>([])
const loading = ref(false)
async function handleLoad() {
loading.value = true
try {
const data = await loadData()
dataRef.value.push(...data)
}
})
finally {
loading.value = false
}
}
function loadData(): Promise<number[]> {
const index = dataRef.value.length
const result = Array.from({ length: 10 })
.fill(null)
.map((_, i) => index + i + 1)
return new Promise(resolve => setTimeout(() => resolve(result), 1000))
}
</script>

<template>
<n-infinite-scroll style="height: 240px" :distance="10" @load="handleLoad">
<div v-for="i in count" :key="i" class="item">
<n-infinite-scroll style="height: 240px" :distance="100" @load="handleLoad">
<div v-for="i in dataRef" :key="i" class="item">
{{ i }}
</div>
<div v-if="loading" class="text">
loading...
</div>
</n-infinite-scroll>
</template>

Expand All @@ -36,8 +46,10 @@ export default defineComponent({
margin-bottom: 10px;
background-color: rgba(0, 128, 0, 0.16);
}

.item:last-child {
margin-bottom: 0;
}
.text {
text-align: center;
}
</style>
8 changes: 8 additions & 0 deletions src/infinite-scroll/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

```demo
basic.vue
reverse.vue
chat.vue
```

Expand All @@ -18,5 +19,12 @@ chat.vue
| 名称 | 类型 | 默认值 | 说明 | 版本 |
| --- | --- | --- | --- | --- |
| distance | `number` | `0` | 触发加载的距离阈值 | 2.38.2 |
| reverse | `boolean` | `false` | 是否从顶部触发加载。默认从底部触发加载 | NEXT_VERSION |
| scrollbar-props | `Object` | `undefined` | 属性参考 [Scrollbar props](scrollbar#Scrollbar-Props) | 2.38.2 |
| on-load | `() => Promise<void> \| void` | `undefined` | 滚动到底部时的回调函数 | 2.38.2 |

### Infinite Slots

| 名称 | 参数 | 说明 |
| ------- | ---- | --------------- |
| default | `()` | Infinite 的内容 |
61 changes: 61 additions & 0 deletions src/infinite-scroll/demos/zhCN/reverse.demo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<markdown>
# 反向
</markdown>

<script setup lang="ts">
import { ref } from 'vue'

const dataRef = ref<number[]>([])
const loading = ref(false)
async function handleLoad() {
loading.value = true
try {
const data = await loadData()
dataRef.value.unshift(...data)
}
finally {
loading.value = false
}
}
function loadData(): Promise<number[]> {
const index = dataRef.value.length
const result = Array.from({ length: 10 })
.fill(null)
.map((_, i) => index + i + 1)
.reverse()
return new Promise(resolve => setTimeout(() => resolve(result), 1000))
}
</script>

<template>
<n-infinite-scroll
style="height: 240px"
reverse
:distance="100"
@load="handleLoad"
>
<div v-if="loading" class="text">
loading...
</div>
<div v-for="i in dataRef" :key="i" class="item">
{{ i }}
</div>
</n-infinite-scroll>
</template>

<style>
.item {
display: flex;
align-items: center;
height: 46px;
justify-content: center;
margin-bottom: 10px;
background-color: rgba(0, 128, 0, 0.16);
}
.item:last-child {
margin-bottom: 0;
}
.text {
text-align: center;
}
</style>
Loading
Loading