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

133 lines
3.3 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-06 07:40:42 +00:00
selectPrefixCls: 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-06 07:40:42 +00:00
defaultBuildOptionText (opt) {
return `${opt.value} ${this.locale.items_per_page}`
2017-12-20 02:56:21 +00:00
},
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-06 07:40:42 +00:00
const { rootPrefixCls, locale, changeSize, quickGo, goButton, selectComponentClass: Select, defaultBuildOptionText } = this
2018-03-05 11:06:44 +00:00
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]
2018-03-06 07:40:42 +00:00
const buildOptionText = this.buildOptionText || defaultBuildOptionText
2017-12-20 02:56:21 +00:00
const options = this.pageSizeOptions.map((opt, i) => (
2018-03-06 07:40:42 +00:00
<Option key={i} value={opt}>{buildOptionText({ value: 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}
2018-03-07 02:48:33 +00:00
onKeyup={this.go}
2018-03-05 11:06:44 +00:00
>{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>