Skip to content

Commit

Permalink
refactor type definitions (ant-design#2323)
Browse files Browse the repository at this point in the history
* refactor(util): noImplicitAny

* refactor(accordion): type definitions improvements

* refactor(action-sheet): add missing type definitions

* chore: configuration

* refactor(activity-indicator): type definitions

* chore: configuration

* refactor(badge): type definitions

* refactor(button): type definitions

* refactor(calendar): type definitions

* refactor(card): type definitions

* refactor(carousel): add type definitions and refactor native part

* refactor(checkbox): type definitions

* refactor(list): type definitions

* refactor(date-picker): type definitions

* refactor(date-picker-view): type definitions

* refactor(drawer): type definitions

* refactor(flex): type definitions

* refactor(grid): type definitions

* refactor(icon): type definitions

* refactor(image-picker): type definitions

* refactor(input-item): type definitions and code refactor

* refactor(locale-provider): type definitions

* refactor(menu): type definitions

* refactor(modal): type definitions

* refactor(nav-bar): type definitions

* refactor(notice-bar): type definitions

* refactor(pagination): type definitions

* refactor(picker): type definitions

* refactor(picker-view): type definitions

* refactor(popover): type definitions

* refactor(progress): type definitions

* refactor(radio): type definitions

* refactor(range): type definitions

* refactor(result): type definitions

* refactor(search-bar): type definitions

* refactor(segmented-control): type definitions

* refactor(slider): type definitions

* refactor(stepper): type definitions

* refactor(steps): type definitions

* refactor(swipe-action): type definitions

* refactor(switch): type definitions

* refactor(tab-bar): type definitions

* refactor(tabs): type definitions

* refactor(tag): type definitions

* refactor(view|text): type definitions

* refactor(textarea-item): type definitions

* refactor(toast): type definitions

* refactor(white-space): type definitions

* refactor(wing-blank): type definitions

* refactor(list-view): type definitions

* refactor(demo): any

* chore(style): use ant-tools tslint configurations

* test: update snapshots

* chore: remove unnecessary configurations and deps

* chore: tslint

* chore: add setup file

* fix: ant-design#2302

* test: update snap

* chore: update deps and  fix tests

* chore: try to let jest faster

* refactor(Icon): extends from SVG Props
  • Loading branch information
BANG88 authored and lixiaoyang1992 committed Apr 26, 2018
1 parent 35a1338 commit 71a4ccc
Show file tree
Hide file tree
Showing 255 changed files with 6,157 additions and 3,295 deletions.
7 changes: 3 additions & 4 deletions components/_util/class.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

export function hasClass(node, className) {
export function hasClass(node: HTMLElement, className: string) {
if (node.classList) {
return node.classList.contains(className);
}
const originClass = node.className;
return ` ${originClass} `.indexOf(` ${className} `) > -1;
}

export function addClass(node, className) {
export function addClass(node: HTMLElement, className: string) {
if (node.classList) {
node.classList.add(className);
} else {
Expand All @@ -17,7 +16,7 @@ export function addClass(node, className) {
}
}

export function removeClass(node, className) {
export function removeClass(node: HTMLElement, className: string) {
if (node.classList) {
node.classList.remove(className);
} else {
Expand Down
17 changes: 11 additions & 6 deletions components/_util/closest.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export default function closest(el, selector) {
const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
while (el) {
if (matchesSelector.call(el, selector)) {
return el;
export default function closest(el: Element, selector: string) {
const matchesSelector =
el.matches ||
el.webkitMatchesSelector ||
(el as any).mozMatchesSelector ||
el.msMatchesSelector;
let p: Element | null = el;
while (p) {
if (matchesSelector.call(p, selector)) {
return p;
}
el = el.parentElement;
p = p.parentElement;
}
return null;
}
10 changes: 7 additions & 3 deletions components/_util/getDataAttr.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export default (props) => {
return Object.keys(props).reduce((prev, key) => {
if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') {
export default (props: { [key: string]: any }) => {
return Object.keys(props).reduce<{ [key: string]: string }>((prev, key) => {
if (
key.substr(0, 5) === 'aria-' ||
key.substr(0, 5) === 'data-' ||
key === 'role'
) {
prev[key] = props[key];
}
return prev;
Expand Down
9 changes: 7 additions & 2 deletions components/_util/getLocale.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export function getComponentLocale(props, context, componentName, getDefaultLocale) {
export function getComponentLocale(
props: any,
context: any,
componentName: string,
getDefaultLocale: () => any,
) {
let locale: any = {};
if (context && context.antLocale && context.antLocale[componentName]) {
locale = context.antLocale[componentName];
Expand Down Expand Up @@ -27,7 +32,7 @@ export function getComponentLocale(props, context, componentName, getDefaultLoca
return result;
}

export function getLocaleCode(context) {
export function getLocaleCode(context: any) {
const localeCode = context.antLocale && context.antLocale.locale;
// Had use LocaleProvide but didn't set locale
if (context.antLocale && context.antLocale.exist && !localeCode) {
Expand Down
6 changes: 6 additions & 0 deletions components/_util/types.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
export type Diff<T extends string, U extends string> = ({ [P in T]: P } &
{ [P in U]: never } & { [x: string]: never })[T];
// get all from A except B*
// type C = Omit<A,'B1'|'B2'>
export type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;
178 changes: 178 additions & 0 deletions components/_util/upgradeTip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// tslint:disable
if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'development' &&
!process.env.DISABLE_ANTD_MOBILE_UPGRADE
) {
try {
const localVersion = require('./version.json');
if (!localVersion._disable && fetch) {
//#region fetch-jsonp
const defaultOptions = {
timeout: 5000,
jsonpCallback: 'callback',
jsonpCallbackFunction: null,
};

function generateCallbackFunction() {
return `jsonp_${Date.now()}_${Math.ceil(Math.random() * 100000)}`;
}

function clearFunction(functionName: string) {
// IE8 throws an exception when you try to delete a property on window
// http://stackoverflow.com/a/1824228/751089
try {
delete (window as any)[functionName];
} catch (e) {
(window as any)[functionName] = undefined;
}
}

function removeScript(scriptId: string) {
const script = document.getElementById(scriptId);
if (script) {
document.getElementsByTagName('head')[0].removeChild(script);
}
}

function fetchJsonp(_url: string, options: any = {}) {
// to avoid param reassign
let url = _url;
const timeout = options.timeout || defaultOptions.timeout;
const jsonpCallback =
options.jsonpCallback || defaultOptions.jsonpCallback;

let timeoutId: number;

return new Promise((resolve, reject) => {
const callbackFunction =
options.jsonpCallbackFunction || generateCallbackFunction();
const scriptId = `${jsonpCallback}_${callbackFunction}`;
(window as any)[callbackFunction] = (response: any) => {
resolve({
ok: true,
// keep consistent with fetch API
json: () => Promise.resolve(response),
});

if (timeoutId) {
clearTimeout(timeoutId);
}

removeScript(scriptId);

clearFunction(callbackFunction);
};

// Check if the user set their own params, and if not add a ? to start a list of params
url += url.indexOf('?') === -1 ? '?' : '&';

const jsonpScript = document.createElement('script');
jsonpScript.setAttribute(
'src',
`${url}${jsonpCallback}=${callbackFunction}`
);
if (options.charset) {
jsonpScript.setAttribute('charset', options.charset);
}
jsonpScript.id = scriptId;
document.getElementsByTagName('head')[0].appendChild(jsonpScript);

timeoutId = setTimeout(() => {
reject(new Error(`JSONP request to ${_url} timed out`));

clearFunction(callbackFunction);
removeScript(scriptId);
(window as any)[callbackFunction] = () => {
clearFunction(callbackFunction);
};
}, timeout);

// Caught if got 404/500
jsonpScript.onerror = () => {
reject(new Error(`JSONP request to ${_url} failed`));

clearFunction(callbackFunction);
removeScript(scriptId);
if (timeoutId) {
clearTimeout(timeoutId);
}
};
});
}
//#endregion

const gateway =
'https://basement-gzone.alipay.com/mgw_proxy/unauthorized_endpoint';
const params = {
path: 'antd-mobile-upgrade-tip/upgrade-tip-h5data',
'x-basement-operation': 'com.alipay.h5data.fengdie.get',
};
const requestData = encodeURIComponent(JSON.stringify([params]));

fetchJsonp(`${gateway}?requestData=${requestData}`)
.then((res: any) => res.json())
.then(data => {
if (!data.result || !data.result.success) {
return;
} else {
data = data.result.result;
}
let notice = '';
const tipComponents = data.filter((item: any) => {
const key = item.name;
if (key === '[notice]') {
notice = item.tip;
}
if (!localVersion[key]) {
return false;
}
const oldVer = localVersion[key].split('.') as string[];
const newVer = item.version.split('.') as string[];
let result = false;
const length = Math.max(oldVer.length, newVer.length);
for (let i = 0; i < length; i++) {
if (oldVer[i] !== newVer[i]) {
const newVerNum = +newVer[i];
const oldVerNum = +oldVer[i];
const hasNaN = isNaN(newVerNum) || isNaN(oldVerNum);
result = !(!hasNaN && oldVerNum >= newVerNum);
if (result) {
break;
}
}
}
return result;
});

if (notice) {
console.warn(
'--------------------------------------------------------\n',
'[antd-mobile-upgrade-tip] Notice:\n',
`${notice}\n`,
'--------------------------------------------------------'
);
}

if (tipComponents.length > 0) {
console.warn(
'--------------------------------------------------------\n',
'[antd-mobile-upgrade-tip] some components is expired:\n',
'\n',
...tipComponents.map((newData: any) => {
const key = newData.name;
return `${key}: ${localVersion[key]} => ${newData.version} ${
newData.tip
} ${newData.url}\n`;
}),
'\n',
'[you can reinstall node_modules to upgrade all of them.]\n',
'[about this] http://mobile.ant.design/docs/react/upgrade-tip-cn\n',
'--------------------------------------------------------'
);
}
})
.catch(() => {});
}
} catch (error) {}
}
9 changes: 3 additions & 6 deletions components/accordion/PropsType.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
interface AccordionProps {
style?: any;
activeKey?: string | Array<string>;
defaultActiveKey?: string | Array<string>;
export interface AccordionPropsTypes {
activeKey?: string | string[];
defaultActiveKey?: string | string[];
onChange?: (x: any) => void;
}

export default AccordionProps;
11 changes: 7 additions & 4 deletions components/accordion/demo/basic.native.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* tslint:disable:no-console */
import { Accordion, List } from 'antd-mobile';
import React from 'react';
import { View } from 'react-native';
import { Accordion, List } from 'antd-mobile';

export default class AccordionExmple extends React.Component<any, any> {
onChange = (key) => {
onChange = (key: string) => {
console.log(key);
}
render() {
Expand All @@ -18,9 +18,12 @@ export default class AccordionExmple extends React.Component<any, any> {
<List.Item>Content 3</List.Item>
</List>
</Accordion.Panel>
<Accordion.Panel header="Title 2">this is panel content2 or other</Accordion.Panel>
<Accordion.Panel header="Title 2">
this is panel content2 or other
</Accordion.Panel>
<Accordion.Panel header="Title 3">
Text text text text text text text text text text text text text text text
Text text text text text text text text text text text text text
text text
</Accordion.Panel>
</Accordion>
</View>
Expand Down
Loading

0 comments on commit 71a4ccc

Please sign in to comment.