Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor by cursor #615

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 26 additions & 39 deletions src/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import type { SelectProps } from 'rc-select';
import type { OptionProps } from 'rc-select/es/Option';
import KEYCODE from 'rc-util/lib/KeyCode';
import classNames from 'classnames';
import React from 'react';
import React, { useState } from 'react';
import type { PaginationLocale, PaginationProps } from './interface';

interface InternalSelectProps extends SelectProps {
/**
* form antd v5.5.0, popupMatchSelectWidth default is true
*/
popupMatchSelectWidth?: boolean;
}

Expand All @@ -31,26 +28,24 @@ interface OptionsProps {

const defaultPageSizeOptions = ['10', '20', '50', '100'];

const Options: React.FC<OptionsProps> = (props) => {
const {
pageSizeOptions = defaultPageSizeOptions,
locale,
changeSize,
pageSize,
goButton,
quickGo,
rootPrefixCls,
selectComponentClass: Select,
selectPrefixCls,
disabled,
buildOptionText,
showSizeChanger,
} = props;

const [goInputText, setGoInputText] = React.useState('');
const Options: React.FC<OptionsProps> = ({
pageSizeOptions = defaultPageSizeOptions,
locale,
changeSize,
pageSize,
goButton,
quickGo,
rootPrefixCls,
selectComponentClass: Select,
selectPrefixCls,
disabled,
buildOptionText,
showSizeChanger,
}) => {
const [goInputText, setGoInputText] = useState('');

const getValidValue = () => {
return !goInputText || Number.isNaN(goInputText)
return !goInputText || isNaN(Number(goInputText))
? undefined
: Number(goInputText);
};
Expand All @@ -71,18 +66,17 @@ const Options: React.FC<OptionsProps> = (props) => {
setGoInputText(e.target.value);
};

const handleBlur = (e: React.FocusEvent<HTMLInputElement, Element>) => {
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
if (goButton || goInputText === '') {
return;
}
setGoInputText('');
if (
e.relatedTarget &&
(e.relatedTarget.className.indexOf(`${rootPrefixCls}-item-link`) >= 0 ||
e.relatedTarget.className.indexOf(`${rootPrefixCls}-item`) >= 0)
) {
(e.relatedTarget.className.includes(`${rootPrefixCls}-item-link`) ||
e.relatedTarget.className.includes(`${rootPrefixCls}-item`))
)
return;
}
quickGo?.(getValidValue());
};

Expand All @@ -104,20 +98,14 @@ const Options: React.FC<OptionsProps> = (props) => {
) {
return pageSizeOptions;
}
return pageSizeOptions.concat([pageSize.toString()]).sort((a, b) => {
const numberA = Number.isNaN(Number(a)) ? 0 : Number(a);
const numberB = Number.isNaN(Number(b)) ? 0 : Number(b);
return numberA - numberB;
});
return [...pageSizeOptions, pageSize.toString()].sort(
(a, b) => Number(a) - Number(b),
);
};
// ============== cls ==============
const prefixCls = `${rootPrefixCls}-options`;

// ============== render ==============
const prefixCls = `${rootPrefixCls}-options`;

if (!showSizeChanger && !quickGo) {
return null;
}
if (!showSizeChanger && !quickGo) return null;

let changeSelect: React.ReactNode = null;
let goInput: React.ReactNode = null;
Expand All @@ -131,7 +119,6 @@ const Options: React.FC<OptionsProps> = (props) => {
typeof showSizeChanger === 'object'
? showSizeChanger
: ({} as SelectProps);
// use showSizeChanger.options if existed, otherwise use pageSizeOptions
const options = showSizeChangerOptions
? undefined
: getPageSizeOptions().map((opt, i) => (
Expand Down
32 changes: 14 additions & 18 deletions src/Pager.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint react/prop-types: 0 */
import classNames from 'classnames';
import React from 'react';
import type { PaginationProps } from './interface';
Expand All @@ -9,25 +8,24 @@ export interface PagerProps extends Pick<PaginationProps, 'itemRender'> {
active?: boolean;
className?: string;
showTitle: boolean;
onClick?: (page: number) => void;
onKeyPress?: (
onClick: (page: number) => void;
onKeyPress: (
e: React.KeyboardEvent<HTMLLIElement>,
onClick: PagerProps['onClick'],
page: PagerProps['page'],
page: number,
) => void;
}

const Pager: React.FC<PagerProps> = (props) => {
const {
rootPrefixCls,
page,
active,
className,
showTitle,
onClick,
onKeyPress,
itemRender,
} = props;
const Pager: React.FC<PagerProps> = ({
rootPrefixCls,
page,
active,
className,
showTitle,
onClick,
onKeyPress,
itemRender,
}) => {
const prefixCls = `${rootPrefixCls}-item`;

const cls = classNames(
Expand All @@ -40,9 +38,7 @@ const Pager: React.FC<PagerProps> = (props) => {
className,
);

const handleClick = () => {
onClick(page);
};
const handleClick = () => onClick(page);

const handleKeyPress = (e: React.KeyboardEvent<HTMLLIElement>) => {
onKeyPress(e, onClick, page);
Expand Down
Loading
Loading