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

improve(ui): improve generated types #930

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
},
"devDependencies": {
"@ant-design/pro-form": "^2.31.3",
"@ant-design/pro-layout": "^7.22.0"
"@ant-design/pro-layout": "^7.22.0",
"@types/hoist-non-react-statics": "^3.3.6"
},
"peerDependencies": {
"react": ">=16.9.0",
Expand Down
11 changes: 9 additions & 2 deletions packages/ui/src/DateRanger/Ranger.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ElementRef } from 'react';
import React, { useEffect, useRef, useState, useImperativeHandle } from 'react';
import {
Button,
Expand Down Expand Up @@ -91,9 +92,15 @@ export interface DateRangerProps
locale?: any;
}

export type DateRangerHandle = {
updateCurrentTime: () => void;
};

export type DateRangerHandleRef = ElementRef<typeof Ranger>;

const prefix = getPrefix('date-ranger');

const Ranger = React.forwardRef((props: DateRangerProps, ref) => {
const Ranger = React.forwardRef<DateRangerHandle, DateRangerProps>((props, ref) => {
const {
selects = [
NEAR_1_MINUTES,
Expand Down Expand Up @@ -574,4 +581,4 @@ const Ranger = React.forwardRef((props: DateRangerProps, ref) => {
export default LocaleWrapper({
componentName: 'DateRanger',
defaultLocale: zhCN,
})(Ranger) as typeof Ranger;
})(Ranger);
4 changes: 2 additions & 2 deletions packages/ui/src/DateRanger/demo/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default () => {
const options = [
{ label: 'hasRewind', value: 'hasRewind' },
{ label: 'hasForward', value: 'hasForward' },
{ label: 'hasNow', value: 'hasNow' },
{ label: 'hasSync', value: 'hasSync' },
];

const [state, setState] = useState(options.map(item => item.value));
Expand Down Expand Up @@ -36,7 +36,7 @@ export default () => {
allowClear={true}
hasForward={getValue('hasForward')}
hasRewind={getValue('hasRewind')}
hasNow={getValue('hasNow')}
hasSync={getValue('hasSync')}
/>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/DateRanger/demo/updateCurrentTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type TimeRangeRef = {
};

export default () => {
const ref = useRef<TimeRangeRef>();
const ref = useRef<TimeRangeRef>(null);
const [timeRange, setTimeRange] = useState([]);
const format = 'YYYY-MM-DD HH:mm:ss';
return (
Expand Down
73 changes: 50 additions & 23 deletions packages/ui/src/DateRanger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,55 @@ import InternalDateRanger from './Ranger';

export * from './Ranger';

const DateRanger = Object.assign(InternalDateRanger, {
NEAR_1_MINUTES,
NEAR_5_MINUTES,
NEAR_10_MINUTES,
NEAR_20_MINUTES,
NEAR_30_MINUTES,
NEAR_1_HOURS,
NEAR_2_HOURS,
NEAR_3_HOURS,
NEAR_6_HOURS,
NEAR_12_HOURS,
TODAY,
LAST_1_DAY,
LAST_3_DAYS,
YESTERDAY,
THIS_WEEK,
LAST_WEEK,
THIS_MONTH,
LAST_MONTH,
THIS_YEAR,
LAST_YEAR,
NEXT_YEAR,
});
type InternalDateRangerType = typeof InternalDateRanger;

export interface DateRangerInstance extends InternalDateRangerType {
NEAR_1_MINUTES: typeof NEAR_1_MINUTES;
NEAR_5_MINUTES: typeof NEAR_5_MINUTES;
NEAR_10_MINUTES: typeof NEAR_10_MINUTES;
NEAR_20_MINUTES: typeof NEAR_20_MINUTES;
NEAR_30_MINUTES: typeof NEAR_30_MINUTES;
NEAR_1_HOURS: typeof NEAR_1_HOURS;
NEAR_2_HOURS: typeof NEAR_2_HOURS;
NEAR_3_HOURS: typeof NEAR_3_HOURS;
NEAR_6_HOURS: typeof NEAR_6_HOURS;
NEAR_12_HOURS: typeof NEAR_12_HOURS;
TODAY: typeof TODAY;
LAST_1_DAY: typeof LAST_1_DAY;
LAST_3_DAYS: typeof LAST_3_DAYS;
LAST_WEEK: typeof LAST_WEEK;
YESTERDAY: typeof YESTERDAY;
THIS_WEEK: typeof THIS_WEEK;
THIS_MONTH: typeof THIS_MONTH;
LAST_MONTH: typeof LAST_MONTH;
THIS_YEAR: typeof THIS_YEAR;
LAST_YEAR: typeof LAST_YEAR;
NEXT_YEAR: typeof NEXT_YEAR;
}

const DateRanger = InternalDateRanger as DateRangerInstance;

// 内置 ranges
DateRanger.NEAR_1_MINUTES = NEAR_1_MINUTES;
DateRanger.NEAR_5_MINUTES = NEAR_5_MINUTES;
DateRanger.NEAR_10_MINUTES = NEAR_10_MINUTES;
DateRanger.NEAR_20_MINUTES = NEAR_20_MINUTES;
DateRanger.NEAR_30_MINUTES = NEAR_30_MINUTES;
DateRanger.NEAR_1_HOURS = NEAR_1_HOURS;
DateRanger.NEAR_2_HOURS = NEAR_2_HOURS;
DateRanger.NEAR_3_HOURS = NEAR_3_HOURS;
DateRanger.NEAR_6_HOURS = NEAR_6_HOURS;
DateRanger.NEAR_12_HOURS = NEAR_12_HOURS;
DateRanger.TODAY = TODAY;
DateRanger.LAST_1_DAY = LAST_1_DAY;
DateRanger.LAST_3_DAYS = LAST_3_DAYS;
DateRanger.YESTERDAY = YESTERDAY;
DateRanger.THIS_WEEK = THIS_WEEK;
DateRanger.LAST_WEEK = LAST_WEEK;
DateRanger.THIS_MONTH = THIS_MONTH;
DateRanger.LAST_MONTH = LAST_MONTH;
DateRanger.THIS_YEAR = THIS_YEAR;
DateRanger.LAST_YEAR = LAST_YEAR;
DateRanger.NEXT_YEAR = NEXT_YEAR;

export default DateRanger;
2 changes: 1 addition & 1 deletion packages/ui/src/FullscreenBox/demo/ListToolbarDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SimpleTable } from './SimpleTable';

export default () => {
const [fullscreen, setFullscreen] = useState(false);
const boxRef = useRef<typeof FullscreenBox>();
const boxRef = useRef<React.ElementRef<typeof FullscreenBox>>();

function handleFullscreenChange(fs) {
setFullscreen(fs);
Expand Down
32 changes: 31 additions & 1 deletion packages/ui/src/Ranger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,37 @@ import InternalRanger from './Ranger';
export * from './QuickPicker';
export * from './Ranger';

const Ranger = InternalRanger;
type InternalRangerType = typeof InternalRanger;

export interface RangerInstance extends InternalRangerType {
QuickPicker: typeof QuickPicker;
NEAR_1_MINUTES: typeof NEAR_1_MINUTES;
NEAR_5_MINUTES: typeof NEAR_5_MINUTES;
NEAR_10_MINUTES: typeof NEAR_10_MINUTES;
NEAR_20_MINUTES: typeof NEAR_20_MINUTES;
NEAR_30_MINUTES: typeof NEAR_30_MINUTES;
NEAR_1_HOURS: typeof NEAR_1_HOURS;
NEAR_2_HOURS: typeof NEAR_2_HOURS;
NEAR_3_HOURS: typeof NEAR_3_HOURS;
NEAR_6_HOURS: typeof NEAR_6_HOURS;
TODAY: typeof TODAY;
YESTERDAY: typeof YESTERDAY;
TOMORROW: typeof TOMORROW;
THIS_WEEK: typeof THIS_WEEK;
LAST_WEEK: typeof LAST_WEEK;
NEXT_WEEK: typeof NEXT_WEEK;
THIS_MONTH: typeof THIS_MONTH;
LAST_MONTH: typeof LAST_MONTH;
NEXT_MONTH: typeof NEXT_MONTH;
THIS_QUARTER: typeof THIS_QUARTER;
LAST_QUARTER: typeof LAST_QUARTER;
NEXT_QUARTER: typeof NEXT_QUARTER;
THIS_YEAR: typeof THIS_YEAR;
LAST_YEAR: typeof LAST_YEAR;
NEXT_YEAR: typeof NEXT_YEAR;
}

const Ranger = InternalRanger as RangerInstance;

// 内置 ranges
Ranger.NEAR_1_MINUTES = NEAR_1_MINUTES;
Expand Down
15 changes: 8 additions & 7 deletions packages/ui/src/TagSelect/Group.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classNames from 'classnames';
import { getPrefix } from '../_util';
import React, { ReactNode, useMemo, useState, useEffect } from 'react';
import type { ReactNode } from 'react';
import React, { useMemo, useState, useEffect } from 'react';
import TagSelectContext from './TagSelectContext';
import Item from './Item';
import useStyle from './style';
Expand All @@ -21,11 +22,11 @@ export interface TagSelectGroupProps {
disabled?: boolean;
className?: string;
multiple?: boolean;
defaultValue?: TagSelectValueType | Array<TagSelectValueType>;
value?: TagSelectValueType | Array<TagSelectValueType>;
defaultValue?: TagSelectValueType | TagSelectValueType[];
value?: TagSelectValueType | TagSelectValueType[];
size?: string;
options?: Array<TagSelectOptionType | string | number>;
onChange?: (value: Array<TagSelectValueType> | TagSelectValueType) => void;
options?: (TagSelectOptionType | string | number)[];
onChange?: (value: TagSelectValueType[] | TagSelectValueType) => void;
}

function toArray(value) {
Expand All @@ -50,10 +51,10 @@ const Group: React.FC<TagSelectGroupProps> = ({
}) => {
const prefix = getPrefix('tag-select');
const { wrapSSR, hashId } = useStyle(prefix);
const [value, setValue] = useState<Array<TagSelectValueType>>(
const [value, setValue] = useState<TagSelectValueType[]>(
toArray(defaultValue || restProps.value)
);
const [registeredValues, setRegisteredValues] = React.useState<Array<TagSelectValueType>>([]);
const [registeredValues, setRegisteredValues] = React.useState<TagSelectValueType[]>([]);

const registerValue = (val: TagSelectValueType) => {
setRegisteredValues(prev => [...prev, val]);
Expand Down
9 changes: 7 additions & 2 deletions packages/ui/src/locale/LocaleWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ export interface LocaleWrapperInput {
defaultLocale: any;
}

type CTR<T> =
T extends React.ForwardRefExoticComponent<object & React.RefAttributes<infer R>> ? R : never;

export default ({ componentName, defaultLocale }: LocaleWrapperInput) =>
<BaseProps extends LocaleWrapperProps>(BaseComponent: React.ComponentType<BaseProps>) => {
<BaseProps extends LocaleWrapperProps>(
BaseComponent: React.ForwardRefExoticComponent<BaseProps> | React.ComponentType<BaseProps>
) => {
type WrappedComponentProps = BaseProps & { forwardedRef: Ref<typeof BaseComponent> };

class Hoc extends React.Component<WrappedComponentProps> {
Expand Down Expand Up @@ -49,7 +54,7 @@ export default ({ componentName, defaultLocale }: LocaleWrapperInput) =>

// 高阶组件需要转发ref
// 参考: https://zh-hans.reactjs.org/docs/forwarding-refs.html#forwarding-refs-in-higher-order-components
const ForwardComponent = React.forwardRef<typeof BaseComponent, BaseProps>(
const ForwardComponent = React.forwardRef<CTR<typeof BaseComponent>, BaseProps>(
// @ts-ignore
(props: BaseProps, ref) => <Hoc {...(props as BaseProps)} forwardedRef={ref} />
);
Expand Down
Loading