ant-design-vue/components/vc-pagination/Options.vue

131 lines
3.2 KiB
Vue
Raw Normal View History

2017-12-20 02:56:21 +00:00
<script>
2018-03-05 11:06:44 +00:00
import PropTypes from '../_util/vue-types'
2017-12-20 02:56:21 +00:00
import KEYCODE from './KeyCode'
2018-03-05 11:06:44 +00:00
import BaseMixin from '../_util/BaseMixin'
2017-12-20 02:56:21 +00:00
export default {
2018-03-05 11:06:44 +00:00
mixins: [BaseMixin],
2017-12-20 02:56:21 +00:00
props: {
rootPrefixCls: PropTypes.String,
2018-03-05 11:06:44 +00:00
changeSize: PropTypes.func,
quickGo: PropTypes.func,
selectComponentClass: PropTypes.any,
2017-12-20 02:56:21 +00:00
current: PropTypes.number,
pageSizeOptions: PropTypes.array.def(['10', '20', '30', '40']),
pageSize: PropTypes.number,
2018-03-05 11:06:44 +00:00
buildOptionText: PropTypes.func,
2017-12-20 02:56:21 +00:00
locale: PropTypes.object,
2018-03-05 11:06:44 +00:00
goButton: PropTypes.any,
2017-12-20 02:56:21 +00:00
},
data () {
return {
goInputText: '',
stateCurrent: this.current,
}
},
methods: {
2018-03-05 11:06:44 +00:00
defaultBuildOptionText (value) {
2017-12-20 02:56:21 +00:00
return `${value} ${this.locale.items_per_page}`
},
2018-03-05 11:06:44 +00:00
handleChange (e) {
this.setState({
goInputText: e.target.value,
})
2017-12-20 02:56:21 +00:00
},
go (e) {
let val = this.goInputText
if (val === '') {
return
}
val = Number(val)
if (isNaN(val)) {
val = this.stateCurrent
}
if (e.keyCode === KEYCODE.ENTER || e.type === 'click') {
2018-03-05 11:06:44 +00:00
this.setState({
goInputText: '',
stateCurrent: this.quickGo(val),
})
2017-12-20 02:56:21 +00:00
}
},
},
render () {
2018-03-05 11:06:44 +00:00
const { rootPrefixCls, locale, changeSize, quickGo, goButton, buildOptionText, selectComponentClass: Select, defaultBuildOptionText } = this
const prefixCls = `${rootPrefixCls}-options`
2017-12-20 02:56:21 +00:00
let changeSelect = null
let goInput = null
let gotoButton = null
if (!(changeSize || quickGo)) {
return null
}
if (changeSize && Select) {
const Option = Select.Option
const pageSize = this.pageSize || this.pageSizeOptions[0]
const options = this.pageSizeOptions.map((opt, i) => (
2018-03-05 11:06:44 +00:00
<Option key={i} value={opt}>{buildOptionText ? buildOptionText(opt) : defaultBuildOptionText(opt)}</Option>
2017-12-20 02:56:21 +00:00
))
changeSelect = (
<Select
prefixCls={this.selectPrefixCls}
showSearch={false}
class={`${prefixCls}-size-changer`}
optionLabelProp='children'
dropdownMatchSelectWidth={false}
value={pageSize.toString()}
2018-03-05 11:06:44 +00:00
onChange={value => this.changeSize(Number(value))}
2017-12-20 02:56:21 +00:00
getPopupContainer={triggerNode => triggerNode.parentNode}
>
{options}
</Select>
)
}
if (quickGo) {
if (goButton) {
if (typeof goButton === 'boolean') {
gotoButton = (
<button
type='button'
onClick={this.go}
onKeyup={this.go}
>
{locale.jump_to_confirm}
</button>
)
} else {
2018-03-05 11:06:44 +00:00
gotoButton = (
<span
onClick={this.go}
onKeyUp={this.go}
>{goButton}</span>
)
2017-12-20 02:56:21 +00:00
}
}
goInput = (
<div class={`${prefixCls}-quick-jumper`}>
{locale.jump_to}
<input
type='text'
value={this.goInputText}
2018-03-05 11:06:44 +00:00
onChange={this.handleChange}
2017-12-20 02:56:21 +00:00
onKeyup={this.go}
/>
{locale.page}
{gotoButton}
</div>
)
}
return (
<li class={`${prefixCls}`}>
{changeSelect}
{goInput}
</li>
)
},
}
</script>