更新分页组件兼容新版wkit

master
yutent 2023-11-14 18:03:07 +08:00
parent 04b501eb45
commit 10c3b134e2
1 changed files with 21 additions and 48 deletions

View File

@ -4,14 +4,12 @@
* @date 2023/04/18 09:38:01 * @date 2023/04/18 09:38:01
*/ */
import { css, html, Component, bind, styleMap } from 'wkit' import { css, html, Component } from 'wkit'
import '../form/button.js' import '../form/button.js'
const LAYOUT_DICT = { const LAYOUT_DICT = {
home: e => home: e =>
html`<wc-button html`<wc-button
solid
type="plain"
data-act="1" data-act="1"
disabled=${e} disabled=${e}
class="item" class="item"
@ -19,8 +17,6 @@ const LAYOUT_DICT = {
></wc-button>`, ></wc-button>`,
end: e => end: e =>
html`<wc-button html`<wc-button
solid
type="plain"
data-act="end" data-act="end"
disabled=${e} disabled=${e}
class="item" class="item"
@ -28,8 +24,6 @@ const LAYOUT_DICT = {
></wc-button>`, ></wc-button>`,
prev: e => prev: e =>
html`<wc-button html`<wc-button
solid
type="plain"
data-act="prev" data-act="prev"
disabled=${e} disabled=${e}
class="item" class="item"
@ -37,8 +31,6 @@ const LAYOUT_DICT = {
></wc-button>`, ></wc-button>`,
next: e => next: e =>
html`<wc-button html`<wc-button
solid
type="plain"
data-act="next" data-act="next"
disabled=${e} disabled=${e}
class="item" class="item"
@ -49,16 +41,16 @@ const LAYOUT_DICT = {
jump(n) { jump(n) {
return html`<section class="item jump"> return html`<section class="item jump">
<span>Go to</span> <span>Go to</span>
<input ref="input" @keydown=${this.gotoPage} maxlength="6" value=${n} /> <input ref="input" @keydown=${this.__gotoPage} maxlength="6" value=${n} />
</section>` </section>`
} }
} }
// 计算页码 // 计算页码
function calculate(curr, total) { function calculate(curr, total) {
var arr = [] let arr = []
var fixed = 0 let fixed = 0
var half = curr < 3 ? 6 - curr : 2 // 中间页码 let half = curr < 3 ? 6 - curr : 2 // 中间页码
// 总页码小于2 // 总页码小于2
if (total < 2) { if (total < 2) {
@ -68,7 +60,7 @@ function calculate(curr, total) {
// 当前页面比半数多时, 前面的用省略号代替 // 当前页面比半数多时, 前面的用省略号代替
if (curr - half > 1 && total > 5) { if (curr - half > 1 && total > 5) {
var to = curr - 2 * half let to = curr - 2 * half
to = to < 1 ? 1 : to to = to < 1 ? 1 : to
arr.push({ to, txt: '...' }) arr.push({ to, txt: '...' })
} }
@ -78,7 +70,7 @@ function calculate(curr, total) {
} }
// 把页码拆成2部分来 // 把页码拆成2部分来
for (var i = curr - half - fixed; i < curr + half + 1 && i <= total; i++) { for (let i = curr - half - fixed; i < curr + half + 1 && i <= total; i++) {
if (i > 0) { if (i > 0) {
arr.push({ to: i, txt: i }) arr.push({ to: i, txt: i })
} }
@ -86,7 +78,7 @@ function calculate(curr, total) {
// 总页码太多时, 以省略号代替 // 总页码太多时, 以省略号代替
if (curr + half < total) { if (curr + half < total) {
var to = curr + 2 * half let to = curr + 2 * half
to = to > total ? total : to to = to > total ? total : to
arr.push({ to, txt: '...' }) arr.push({ to, txt: '...' })
} }
@ -96,20 +88,14 @@ function calculate(curr, total) {
class Pager extends Component { class Pager extends Component {
static props = { static props = {
layout: { layout: 'str!home, prev, pages, next, end',
type: String,
default: 'home, prev, pages, next, end',
attribute: false
},
total: { total: {
type: Number, type: Number,
default: 0, default: 0,
attribute: false, attribute: false,
observer(v) { observer(v) {
if (this.#mounted) {
this.totalpage = Math.ceil(v / this.pagesize) this.totalpage = Math.ceil(v / this.pagesize)
} }
}
}, },
totalpage: { totalpage: {
type: Number, type: Number,
@ -160,15 +146,17 @@ class Pager extends Component {
justify-content: center; justify-content: center;
align-items: center; align-items: center;
margin: 10px auto; margin: 10px auto;
--wc-button-icon-size: 10px;
--wc-button-border-color: none;
--wc-button-background: var(--color-plain-1);
--wc-button-color-hover: var(--color-teal-1);
} }
.item { .item {
min-width: 32px; min-width: 32px;
width: auto; width: auto;
--padding: 0;
margin: 0 5px; margin: 0 5px;
--icon-size: 12px; border-radius: 3px;
border-radius: 4px;
&.curr { &.curr {
display: inline-flex; display: inline-flex;
@ -183,10 +171,6 @@ class Pager extends Component {
display: flex; display: flex;
align-items: center; align-items: center;
} }
&[size='l'] {
--icon-size: 12px;
}
} }
input { input {
@ -210,9 +194,7 @@ class Pager extends Component {
css`` css``
] ]
#mounted = false #handleBtnClick(ev) {
handleBtnClick(ev) {
let elem = ev.target let elem = ev.target
if (elem.tagName === 'WC-BUTTON') { if (elem.tagName === 'WC-BUTTON') {
let { page, totalpage } = this let { page, totalpage } = this
@ -242,44 +224,35 @@ class Pager extends Component {
} }
this.page = num this.page = num
this.$refs.input && (this.$refs.input.value = num)
console.log(this)
this.$emit('page-change', { data: num }) this.$emit('page-change', { data: num })
} }
} }
gotoPage(ev) { __gotoPage(ev) {
if (ev.keyCode === 13) { if (ev.keyCode === 13) {
let n = +ev.target.value let n = +ev.target.value
if (n === n) { if (n === n) {
this.page = n this.page = n
this.$emit('page-change', { data: n }) this.$emit('page-change', { data: n })
} }
ev.target.value = n ev.target.value = this.page
} }
} }
mounted() {
this.#mounted = true
}
render() { render() {
let layout = this.layout.split(',').map(s => s.trim()) let layout = this.layout.split(',').map(s => s.trim())
let { page, total, totalpage } = this let { page, total, totalpage } = this
return html` return html`
<div class="layout" @click=${this.handleBtnClick}> <div class="layout" @click=${this.#handleBtnClick}>
${layout.map(n => { ${layout.map(n => {
switch (n) { switch (n) {
case 'pages': case 'pages':
return calculate(page, totalpage).map(it => return calculate(page, totalpage).map(it =>
page === it.to page === it.to
? LAYOUT_DICT.curr(page) ? LAYOUT_DICT.curr(page)
: html`<wc-button : html`<wc-button class="item" data-act="${it.to}"
type="plain"
solid
class="item"
data-act="${it.to}"
>${it.txt}</wc-button >${it.txt}</wc-button
>` >`
) )