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

146 lines
3.6 KiB
Vue
Raw Normal View History

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