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

132 lines
3.2 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../_util/vue-types';
import KEYCODE from './KeyCode';
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
},
2019-01-12 03:33:27 +00:00
data() {
2017-12-20 02:56:21 +00:00
return {
goInputText: '',
2019-01-12 03:33:27 +00:00
};
2017-12-20 02:56:21 +00:00
},
methods: {
2019-01-12 03:33:27 +00:00
defaultBuildOptionText(opt) {
return `${opt.value} ${this.locale.items_per_page}`;
2017-12-20 02:56:21 +00:00
},
2019-01-12 03:33:27 +00:00
handleChange(e) {
2018-03-05 11:06:44 +00:00
this.setState({
goInputText: e.target.value,
2019-01-12 03:33:27 +00:00
});
2017-12-20 02:56:21 +00:00
},
2019-01-12 03:33:27 +00:00
go(e) {
let val = this.goInputText;
2017-12-20 02:56:21 +00:00
if (val === '') {
2019-01-12 03:33:27 +00:00
return;
2017-12-20 02:56:21 +00:00
}
2019-01-12 03:33:27 +00:00
val = isNaN(val) ? this.current : Number(val);
2017-12-20 02:56:21 +00:00
if (e.keyCode === KEYCODE.ENTER || e.type === 'click') {
2018-03-05 11:06:44 +00:00
this.setState({
goInputText: '',
2019-01-12 03:33:27 +00:00
});
this.quickGo(val);
2017-12-20 02:56:21 +00:00
}
},
},
2019-01-12 03:33:27 +00:00
render() {
const {
rootPrefixCls,
locale,
changeSize,
quickGo,
goButton,
selectComponentClass: Select,
defaultBuildOptionText,
} = this;
const prefixCls = `${rootPrefixCls}-options`;
let changeSelect = null;
let goInput = null;
let gotoButton = null;
2017-12-20 02:56:21 +00:00
if (!(changeSize || quickGo)) {
2019-01-12 03:33:27 +00:00
return null;
2017-12-20 02:56:21 +00:00
}
if (changeSize && Select) {
2019-01-12 03:33:27 +00:00
const Option = Select.Option;
const pageSize = this.pageSize || this.pageSizeOptions[0];
const buildOptionText = this.buildOptionText || defaultBuildOptionText;
2017-12-20 02:56:21 +00:00
const options = this.pageSizeOptions.map((opt, i) => (
2019-01-12 03:33:27 +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`}
2019-01-12 03:33:27 +00:00
optionLabelProp="children"
2017-12-20 02:56:21 +00:00
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>
2019-01-12 03:33:27 +00:00
);
2017-12-20 02:56:21 +00:00
}
if (quickGo) {
if (goButton) {
if (typeof goButton === 'boolean') {
gotoButton = (
2019-01-12 03:33:27 +00:00
<button type="button" onClick={this.go} onKeyup={this.go}>
2017-12-20 02:56:21 +00:00
{locale.jump_to_confirm}
</button>
2019-01-12 03:33:27 +00:00
);
2017-12-20 02:56:21 +00:00
} else {
2018-03-05 11:06:44 +00:00
gotoButton = (
2019-01-12 03:33:27 +00:00
<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
2019-01-12 03:33:27 +00:00
type="text"
2017-12-20 02:56:21 +00:00
value={this.goInputText}
2018-03-12 14:13:59 +00:00
onInput={this.handleChange}
2017-12-20 02:56:21 +00:00
onKeyup={this.go}
/>
{locale.page}
{gotoButton}
</div>
2019-01-12 03:33:27 +00:00
);
2017-12-20 02:56:21 +00:00
}
return (
<li class={`${prefixCls}`}>
{changeSelect}
{goInput}
</li>
2019-01-12 03:33:27 +00:00
);
2017-12-20 02:56:21 +00:00
},
2019-01-12 03:33:27 +00:00
};