From b9ebec149ace2d1bbfca625df71ba026863e480d Mon Sep 17 00:00:00 2001 From: kodai3 Date: Sun, 12 Jul 2020 09:37:15 +0900 Subject: [PATCH 1/6] swipeable views type definitions --- package.json | 4 +- .../src/SwipeableViews.d.ts | 56 +++++++++++++++++++ packages/react-swipeable-views/src/index.d.ts | 2 + scripts/copy-files.js | 31 ++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 packages/react-swipeable-views/src/SwipeableViews.d.ts create mode 100644 packages/react-swipeable-views/src/index.d.ts create mode 100644 scripts/copy-files.js diff --git a/package.json b/package.json index 11d966f9..673c89b9 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,9 @@ "size": "size-limit", "size:why": "size-limit --why packages/react-swipeable-views/lib/index.js", "watch": "lerna exec --concurrency 99 -- babel src --out-dir lib --watch", - "build": "rm -rf packages/*/lib && cross-env NODE_ENV=production lerna exec -- babel --config-file ../../babel.config.js src --out-dir lib --ignore test.js", + "build": "yarn prebuild && cross-env NODE_ENV=production lerna exec -- babel --config-file ../../babel.config.js src --out-dir lib --ignore test.js && yarn build:copy-files", + "prebuild": "rm -rf packages/*/lib", + "build:copy-files": "lerna exec node ../../scripts/copy-files.js", "release": "yarn build && yarn lerna exec yarn prepublish && lerna publish", "postrelease": "yarn docs:deploy" }, diff --git a/packages/react-swipeable-views/src/SwipeableViews.d.ts b/packages/react-swipeable-views/src/SwipeableViews.d.ts new file mode 100644 index 00000000..2fac625f --- /dev/null +++ b/packages/react-swipeable-views/src/SwipeableViews.d.ts @@ -0,0 +1,56 @@ +import * as React from 'react'; + +export type OnChangeIndexCallback = (index: number, indexLatest: number) => void; + +export type OnTransitionEndCallback = () => void; + +export type OnSwitchingCallback = (index: number, type: OnSwitchingCallbackTypeDescriptor) => void; + +export type OnSwitchingCallbackTypeDescriptor = 'move' | 'end'; + +export type AxisType = 'x' | 'x-reverse' | 'y' | 'y-reverse'; + +export interface SpringConfig { + duration: string; + easeFunction: string; + delay: string; +} + +export interface SwipeableViewsProps extends React.HTMLProps { + animateHeight?: boolean; + animateTransitions?: boolean; + axis?: AxisType; + containerStyle?: React.CSSProperties; + disabled?: boolean; + /* + * This is the config used to disable lazy loading, if true it will render all the views in first rendering. + */ + disableLazyLoading?: boolean; + enableMouseEvents?: boolean; + hysteresis?: number; + ignoreNativeScroll?: boolean; + index?: number; + onChangeIndex?: OnChangeIndexCallback; + onSwitching?: OnSwitchingCallback; + onTransitionEnd?: OnTransitionEndCallback; + resistance?: boolean; + style?: React.CSSProperties; + slideStyle?: React.CSSProperties; + springConfig?: SpringConfig; + slideClassName?: string; + threshold?: number; +} + +export interface SwipeableViewsState { + indexCurrent?: number; + indexLatest?: number; + isDragging?: boolean; + isFirstRender?: boolean; + heightLatest?: number; + displaySameSlide?: boolean; +} + +export default class SwipeableViews extends React.Component< + SwipeableViewsProps, + SwipeableViewsState, +> {} diff --git a/packages/react-swipeable-views/src/index.d.ts b/packages/react-swipeable-views/src/index.d.ts new file mode 100644 index 00000000..4da37384 --- /dev/null +++ b/packages/react-swipeable-views/src/index.d.ts @@ -0,0 +1,2 @@ +export * from './SwipeableViews'; +export { default } from './SwipeableViews'; diff --git a/scripts/copy-files.js b/scripts/copy-files.js new file mode 100644 index 00000000..eb2390a4 --- /dev/null +++ b/scripts/copy-files.js @@ -0,0 +1,31 @@ +/* eslint-disable no-console */ +const path = require('path'); +const fse = require('fs-extra'); +const glob = require('glob'); + +const packagePath = process.cwd(); +const buildPath = path.join(packagePath, './lib'); +const srcPath = path.join(packagePath, './src'); + +async function typescriptCopy({ from, to }) { + if (!(await fse.exists(to))) { + console.warn(`path ${to} does not exists`); + return []; + } + + const files = glob.sync('**/*.d.ts', { cwd: from }); + const cmds = files.map(file => fse.copy(path.resolve(from, file), path.resolve(to, file))); + return Promise.all(cmds); +} + +async function run() { + try { + // TypeScript + await typescriptCopy({ from: srcPath, to: buildPath }); + } catch (err) { + console.error(err); + process.exit(1); + } +} + +run(); From ef093ee6e5dd03a2dc9dce7720e9abd269fc2d1c Mon Sep 17 00:00:00 2001 From: kodai3 Date: Sun, 12 Jul 2020 09:46:05 +0900 Subject: [PATCH 2/6] patch type fixes #545 --- packages/react-swipeable-views/src/SwipeableViews.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-swipeable-views/src/SwipeableViews.d.ts b/packages/react-swipeable-views/src/SwipeableViews.d.ts index 2fac625f..6c457faa 100644 --- a/packages/react-swipeable-views/src/SwipeableViews.d.ts +++ b/packages/react-swipeable-views/src/SwipeableViews.d.ts @@ -19,6 +19,7 @@ export interface SpringConfig { export interface SwipeableViewsProps extends React.HTMLProps { animateHeight?: boolean; animateTransitions?: boolean; + action?: (hooks: SwipeableViewsHooks) => void; axis?: AxisType; containerStyle?: React.CSSProperties; disabled?: boolean; From 987f9c8bb957bd1ab4d75134a66eb8379d69ef28 Mon Sep 17 00:00:00 2001 From: kodai3 Date: Sun, 12 Jul 2020 09:54:45 +0900 Subject: [PATCH 3/6] feature core utils type definitions --- .../src/checkIndexBounds.d.ts | 3 ++ .../src/computeIndex.d.ts | 17 +++++++++++ .../src/constant.d.ts | 4 +++ .../src/getDisplaySameSlide.d.ts | 3 ++ .../react-swipeable-views-core/src/index.d.ts | 5 ++++ .../react-swipeable-views-core/src/mod.d.ts | 1 + .../react-swipeable-views-utils/package.json | 3 +- .../src/autoPlay.d.ts | 17 +++++++++++ .../src/bindKeyboard.d.ts | 15 ++++++++++ .../src/index.d.ts | 3 ++ .../src/virtualize.d.ts | 29 +++++++++++++++++++ 11 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 packages/react-swipeable-views-core/src/checkIndexBounds.d.ts create mode 100644 packages/react-swipeable-views-core/src/computeIndex.d.ts create mode 100644 packages/react-swipeable-views-core/src/constant.d.ts create mode 100644 packages/react-swipeable-views-core/src/getDisplaySameSlide.d.ts create mode 100644 packages/react-swipeable-views-core/src/index.d.ts create mode 100644 packages/react-swipeable-views-core/src/mod.d.ts create mode 100644 packages/react-swipeable-views-utils/src/autoPlay.d.ts create mode 100644 packages/react-swipeable-views-utils/src/bindKeyboard.d.ts create mode 100644 packages/react-swipeable-views-utils/src/index.d.ts create mode 100644 packages/react-swipeable-views-utils/src/virtualize.d.ts diff --git a/packages/react-swipeable-views-core/src/checkIndexBounds.d.ts b/packages/react-swipeable-views-core/src/checkIndexBounds.d.ts new file mode 100644 index 00000000..a5b63c2c --- /dev/null +++ b/packages/react-swipeable-views-core/src/checkIndexBounds.d.ts @@ -0,0 +1,3 @@ +import { SwipeableViewsProps } from 'react-swipeable-views'; + +export function checkIndexBounds(props: SwipeableViewsProps): void; diff --git a/packages/react-swipeable-views-core/src/computeIndex.d.ts b/packages/react-swipeable-views-core/src/computeIndex.d.ts new file mode 100644 index 00000000..139599a3 --- /dev/null +++ b/packages/react-swipeable-views-core/src/computeIndex.d.ts @@ -0,0 +1,17 @@ +import { SwipeableViewsProps } from 'react-swipeable-views'; + +export interface ComputeIndexParams { + children: SwipeableViewsProps['children']; + resistance: SwipeableViewsProps['resistance']; + startIndex: number; + startX: number; + pageX: number; + viewLength: number; +} + +export function computeIndex( + params: ComputeIndexParams, +): { + index: number; + startX: number; +}; diff --git a/packages/react-swipeable-views-core/src/constant.d.ts b/packages/react-swipeable-views-core/src/constant.d.ts new file mode 100644 index 00000000..a460ae3e --- /dev/null +++ b/packages/react-swipeable-views-core/src/constant.d.ts @@ -0,0 +1,4 @@ +export const constant: { + RESISTANCE_COEF: number; + UNCERTAINTY_THRESHOLD: number; +}; diff --git a/packages/react-swipeable-views-core/src/getDisplaySameSlide.d.ts b/packages/react-swipeable-views-core/src/getDisplaySameSlide.d.ts new file mode 100644 index 00000000..0a4aa14b --- /dev/null +++ b/packages/react-swipeable-views-core/src/getDisplaySameSlide.d.ts @@ -0,0 +1,3 @@ +import { SwipeableViewsProps } from 'react-swipeable-views'; + +export function getDisplaySameSlide(props: SwipeableViewsProps, nextProps: SwipeableViewsProps): boolean; diff --git a/packages/react-swipeable-views-core/src/index.d.ts b/packages/react-swipeable-views-core/src/index.d.ts new file mode 100644 index 00000000..f532d19c --- /dev/null +++ b/packages/react-swipeable-views-core/src/index.d.ts @@ -0,0 +1,5 @@ +export { default as checkIndexBounds } from './checkIndexBounds'; +export { default as computeIndex } from './computeIndex'; +export { default as constant } from './constant'; +export { default as getDisplaySameSlide } from './getDisplaySameSlide'; +export { default as mod } from './mod'; diff --git a/packages/react-swipeable-views-core/src/mod.d.ts b/packages/react-swipeable-views-core/src/mod.d.ts new file mode 100644 index 00000000..48251bf8 --- /dev/null +++ b/packages/react-swipeable-views-core/src/mod.d.ts @@ -0,0 +1 @@ +export function mod(n: number, m: number): number; diff --git a/packages/react-swipeable-views-utils/package.json b/packages/react-swipeable-views-utils/package.json index b682bcf7..d6e55644 100644 --- a/packages/react-swipeable-views-utils/package.json +++ b/packages/react-swipeable-views-utils/package.json @@ -20,7 +20,8 @@ "prop-types": "^15.6.0", "react-event-listener": "^0.6.0", "react-swipeable-views-core": "^0.13.7", - "shallow-equal": "^1.2.1" + "shallow-equal": "^1.2.1", + "@material-ui/types": "^4.0.0" }, "devDependencies": { "pkgfiles": "^2.3.2" diff --git a/packages/react-swipeable-views-utils/src/autoPlay.d.ts b/packages/react-swipeable-views-utils/src/autoPlay.d.ts new file mode 100644 index 00000000..87486aa4 --- /dev/null +++ b/packages/react-swipeable-views-utils/src/autoPlay.d.ts @@ -0,0 +1,17 @@ +import { OnChangeIndexCallback, OnSwitchingCallback } from 'react-swipeable-views'; + +export interface WithAutoPlay { + index: number; + onChangeIndex: OnChangeIndexCallback; + onSwitching?: OnSwitchingCallback; +} + +export interface WithAutoPlayProps { + autoplay?: boolean; + direction?: 'incremental' | 'decremental'; + index: number; + interval?: number; + onChangeIndex: OnChangeIndexCallback; + slideCount?: number; +} +export const autoPlay: PropInjector; diff --git a/packages/react-swipeable-views-utils/src/bindKeyboard.d.ts b/packages/react-swipeable-views-utils/src/bindKeyboard.d.ts new file mode 100644 index 00000000..0b68cc7c --- /dev/null +++ b/packages/react-swipeable-views-utils/src/bindKeyboard.d.ts @@ -0,0 +1,15 @@ +import { OnChangeIndexCallback } from 'react-swipeable-views'; + +export interface WithBindKeyboard { + index: number; + onChangeIndex: OnChangeIndexCallback; +} + +export interface WithBindKeyboardProps { + axis?: "x" | "x-reverse" | "y" | "y-reverse"; + index: number; + onChangeIndex: OnChangeIndexCallback; + slidecount?: number; +} + +export const bindKeyboard: PropInjector; diff --git a/packages/react-swipeable-views-utils/src/index.d.ts b/packages/react-swipeable-views-utils/src/index.d.ts new file mode 100644 index 00000000..f17968f7 --- /dev/null +++ b/packages/react-swipeable-views-utils/src/index.d.ts @@ -0,0 +1,3 @@ +export { default as autoPlay } from './autoPlay'; +export { default as bindKeyboard } from './bindKeyboard'; +export { default as virtualize } from './virtualize'; diff --git a/packages/react-swipeable-views-utils/src/virtualize.d.ts b/packages/react-swipeable-views-utils/src/virtualize.d.ts new file mode 100644 index 00000000..8cbde91b --- /dev/null +++ b/packages/react-swipeable-views-utils/src/virtualize.d.ts @@ -0,0 +1,29 @@ +import { PropInjector } from '@material-ui/types'; +import * as React from 'react'; +import { OnChangeIndexCallback, OnTransitionEndCallback } from 'react-swipeable-views'; + +export interface SlideRenderProps { + index: number; + key: number; +} + +export type SlideRendererCallback = (render: SlideRenderProps) => React.ReactNode; + +export interface WithVirtualize { + index: number; + onChangeIndex: OnChangeIndexCallback; + slideRenderer: (render: SlideRendererCallback) => React.ReactNode; +} + +export interface WithVirtualizeProps { + index: number; + onChangeIndex: OnChangeIndexCallback; + onTransitionEnd?: OnTransitionEndCallback; + overscanSlideAfter?: number; + overscanSlideBefore?: number; + slideCount?: number; + children?: React.ReactNode; + slideRenderer: SlideRendererCallback; +} + +export const virtualize: PropInjector; From 05eb2dd5b397ce01de79c346b31dfc8445c48db1 Mon Sep 17 00:00:00 2001 From: kodai3 Date: Wed, 29 Jul 2020 06:36:13 +0900 Subject: [PATCH 4/6] update lock file --- yarn.lock | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/yarn.lock b/yarn.lock index 36202642..bd5e2aaf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1640,6 +1640,13 @@ "@babel/runtime" "7.0.0" recompose "^0.29.0" +"@material-ui/types@^4.0.0": + version "4.1.1" + resolved "https://registry.yarnpkg.com/@material-ui/types/-/types-4.1.1.tgz#b65e002d926089970a3271213a3ad7a21b17f02b" + integrity sha512-AN+GZNXytX9yxGi0JOfxHrRTbhFybjUJ05rnsBVjcB+16e466Z0Xe5IxawuOayVZgTBNDxmPKo5j4V6OnMtaSQ== + dependencies: + "@types/react" "*" + "@mrmlnc/readdir-enhanced@^2.2.1": version "2.2.1" resolved "https://registry.yarnpkg.com/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz#524af240d1a360527b730475ecfa1344aa540dde" From 318adf454bde5e43b56f789c4269d4357c234399 Mon Sep 17 00:00:00 2001 From: kodai3 Date: Wed, 29 Jul 2020 06:51:41 +0900 Subject: [PATCH 5/6] Revert "patch type fixes #545" This reverts commit ef093ee6e5dd03a2dc9dce7720e9abd269fc2d1c. --- packages/react-swipeable-views/src/SwipeableViews.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/react-swipeable-views/src/SwipeableViews.d.ts b/packages/react-swipeable-views/src/SwipeableViews.d.ts index 6c457faa..2fac625f 100644 --- a/packages/react-swipeable-views/src/SwipeableViews.d.ts +++ b/packages/react-swipeable-views/src/SwipeableViews.d.ts @@ -19,7 +19,6 @@ export interface SpringConfig { export interface SwipeableViewsProps extends React.HTMLProps { animateHeight?: boolean; animateTransitions?: boolean; - action?: (hooks: SwipeableViewsHooks) => void; axis?: AxisType; containerStyle?: React.CSSProperties; disabled?: boolean; From 9bae6fa84ae702887842b3be5d5f3d6de71d566e Mon Sep 17 00:00:00 2001 From: kodai3 Date: Thu, 30 Jul 2020 10:22:11 +0900 Subject: [PATCH 6/6] fix import --- packages/react-swipeable-views-utils/src/autoPlay.d.ts | 1 + packages/react-swipeable-views-utils/src/bindKeyboard.d.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/react-swipeable-views-utils/src/autoPlay.d.ts b/packages/react-swipeable-views-utils/src/autoPlay.d.ts index 87486aa4..1bf66942 100644 --- a/packages/react-swipeable-views-utils/src/autoPlay.d.ts +++ b/packages/react-swipeable-views-utils/src/autoPlay.d.ts @@ -1,4 +1,5 @@ import { OnChangeIndexCallback, OnSwitchingCallback } from 'react-swipeable-views'; +import { PropInjector } from '@material-ui/types'; export interface WithAutoPlay { index: number; diff --git a/packages/react-swipeable-views-utils/src/bindKeyboard.d.ts b/packages/react-swipeable-views-utils/src/bindKeyboard.d.ts index 0b68cc7c..b4641167 100644 --- a/packages/react-swipeable-views-utils/src/bindKeyboard.d.ts +++ b/packages/react-swipeable-views-utils/src/bindKeyboard.d.ts @@ -1,3 +1,4 @@ +import { PropInjector } from '@material-ui/types'; import { OnChangeIndexCallback } from 'react-swipeable-views'; export interface WithBindKeyboard {