From 9af68d8d9594bd7f2e5d944fd4ede05f0b2f7f8a Mon Sep 17 00:00:00 2001 From: Valikhan Akhmedov Date: Mon, 2 Mar 2020 23:27:16 +0600 Subject: [PATCH] [Simplebar] Add typing for v5 (#42654) * move old typings in v2 * add new typings * fix linter errors * move tests under v2 * add more classes * revert original comments in v2 --- types/simplebar/index.d.ts | 31 +++++++++++---- types/simplebar/simplebar-tests.ts | 48 +++++++++-------------- types/simplebar/v2/index.d.ts | 34 +++++++++++++++++ types/simplebar/v2/simplebar-tests.ts | 51 +++++++++++++++++++++++++ types/simplebar/v2/test/module-tests.ts | 4 ++ types/simplebar/v2/tsconfig.json | 30 +++++++++++++++ types/simplebar/v2/tslint.json | 3 ++ 7 files changed, 164 insertions(+), 37 deletions(-) create mode 100644 types/simplebar/v2/index.d.ts create mode 100644 types/simplebar/v2/simplebar-tests.ts create mode 100644 types/simplebar/v2/test/module-tests.ts create mode 100644 types/simplebar/v2/tsconfig.json create mode 100644 types/simplebar/v2/tslint.json diff --git a/types/simplebar/index.d.ts b/types/simplebar/index.d.ts index fd3776527d..39d3cb4b9d 100644 --- a/types/simplebar/index.d.ts +++ b/types/simplebar/index.d.ts @@ -1,14 +1,15 @@ -// Type definitions for simplebar.js 2.4 +// Type definitions for simplebar.js 5.1 // Project: https://github.com/Grsmto/simplebar, https://grsmto.github.io/simplebar -// Definitions by: Gregor Woiwode , Leonard Thieu +// Definitions by: Valikhan Akhmedov , Gregor Woiwode , Leonard Thieu // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.1 +// TypeScript Version: 2.8 export as namespace SimpleBar; export = SimpleBar; declare class SimpleBar { static removeObserver(): void; + static instances: Pick, 'get' | 'has'>; constructor(element: HTMLElement, options?: SimpleBar.Options); @@ -19,16 +20,32 @@ declare class SimpleBar { declare namespace SimpleBar { interface Options { - wrapContent?: boolean; autoHide?: boolean; - scrollbarMinSize?: number; classNames?: ClassNamesOptions; + forceVisible?: boolean | 'x' | 'y'; + direction?: 'rtl' | 'ltr'; + timeout?: number; + clickOnTrack?: boolean; + scrollbarMinSize?: number; + scrollbarMaxSize?: number; } interface ClassNamesOptions { - content?: string; - scrollContent?: string; + contentEl?: string; + contentWrapper?: string; + offset?: string; + mask?: string; + wrapper?: string; + placeholder?: string; scrollbar?: string; track?: string; + heightAutoObserverWrapperEl?: string; + heightAutoObserverEl?: string; + visible?: string; + horizontal?: string; + vertical?: string; + hover?: string; + dragging?: string; + [className: string]: string; } } diff --git a/types/simplebar/simplebar-tests.ts b/types/simplebar/simplebar-tests.ts index ad403d22a5..e7d8f5bb61 100644 --- a/types/simplebar/simplebar-tests.ts +++ b/types/simplebar/simplebar-tests.ts @@ -1,17 +1,17 @@ -function test_start() { +function test_constructorWithoutOpts() { new SimpleBar(document.getElementById('myElement')); } -function test_options_wrapContent() { - new SimpleBar(document.getElementById('myElement'), { wrapContent: false }); -} - -function test_options_autoHide() { - new SimpleBar(document.getElementById('myElement'), { autoHide: false }); -} - -function test_options_scrollbarMinSize() { - new SimpleBar(document.getElementById('myElement'), { scrollbarMinSize: 10 }); +function test_constructor() { + new SimpleBar(document.getElementById('myElement'), { + autoHide: true, + clickOnTrack: true, + direction: 'ltr', + forceVisible: 'x', + scrollbarMaxSize: 20, + scrollbarMinSize: 10, + timeout: 300, + }); } function test_options_classNames() { @@ -21,31 +21,19 @@ function test_options_classNames() { content: 'simplebar-content', scrollContent: 'simplebar-scroll-content', scrollbar: 'simplebar-scrollbar', - track: 'simplebar-track' - } + track: 'simplebar-track', + }, }); } -function test_recalculate() { +function test_instanceFunctions() { const el = new SimpleBar(document.getElementById('myElement')); el.recalculate(); + const contentEl: Element = el.getContentElement(); + const scrollEl: Element = el.getScrollElement(); } -function test_getScrollElement() { - const el = new SimpleBar(document.getElementById('myElement')); - el.getScrollElement(); -} - -function test_scrollEvent() { - const el = new SimpleBar(document.getElementById('myElement')); - el.getScrollElement().addEventListener('scroll', () => { }); -} - -function test_getContentElement() { - const el = new SimpleBar(document.getElementById('myElement')); - el.getContentElement(); -} - -function test_removeObserver() { +function test_staticFunctions() { SimpleBar.removeObserver(); + SimpleBar.instances.get(new HTMLDivElement()); } diff --git a/types/simplebar/v2/index.d.ts b/types/simplebar/v2/index.d.ts new file mode 100644 index 0000000000..fd3776527d --- /dev/null +++ b/types/simplebar/v2/index.d.ts @@ -0,0 +1,34 @@ +// Type definitions for simplebar.js 2.4 +// Project: https://github.com/Grsmto/simplebar, https://grsmto.github.io/simplebar +// Definitions by: Gregor Woiwode , Leonard Thieu +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +export as namespace SimpleBar; +export = SimpleBar; + +declare class SimpleBar { + static removeObserver(): void; + + constructor(element: HTMLElement, options?: SimpleBar.Options); + + recalculate(): void; + getScrollElement(): Element; + getContentElement(): Element; +} + +declare namespace SimpleBar { + interface Options { + wrapContent?: boolean; + autoHide?: boolean; + scrollbarMinSize?: number; + classNames?: ClassNamesOptions; + } + + interface ClassNamesOptions { + content?: string; + scrollContent?: string; + scrollbar?: string; + track?: string; + } +} diff --git a/types/simplebar/v2/simplebar-tests.ts b/types/simplebar/v2/simplebar-tests.ts new file mode 100644 index 0000000000..ad403d22a5 --- /dev/null +++ b/types/simplebar/v2/simplebar-tests.ts @@ -0,0 +1,51 @@ +function test_start() { + new SimpleBar(document.getElementById('myElement')); +} + +function test_options_wrapContent() { + new SimpleBar(document.getElementById('myElement'), { wrapContent: false }); +} + +function test_options_autoHide() { + new SimpleBar(document.getElementById('myElement'), { autoHide: false }); +} + +function test_options_scrollbarMinSize() { + new SimpleBar(document.getElementById('myElement'), { scrollbarMinSize: 10 }); +} + +function test_options_classNames() { + new SimpleBar(document.getElementById('myElement'), { + classNames: { + // defaults + content: 'simplebar-content', + scrollContent: 'simplebar-scroll-content', + scrollbar: 'simplebar-scrollbar', + track: 'simplebar-track' + } + }); +} + +function test_recalculate() { + const el = new SimpleBar(document.getElementById('myElement')); + el.recalculate(); +} + +function test_getScrollElement() { + const el = new SimpleBar(document.getElementById('myElement')); + el.getScrollElement(); +} + +function test_scrollEvent() { + const el = new SimpleBar(document.getElementById('myElement')); + el.getScrollElement().addEventListener('scroll', () => { }); +} + +function test_getContentElement() { + const el = new SimpleBar(document.getElementById('myElement')); + el.getContentElement(); +} + +function test_removeObserver() { + SimpleBar.removeObserver(); +} diff --git a/types/simplebar/v2/test/module-tests.ts b/types/simplebar/v2/test/module-tests.ts new file mode 100644 index 0000000000..b7d10d73bf --- /dev/null +++ b/types/simplebar/v2/test/module-tests.ts @@ -0,0 +1,4 @@ +import SimpleBar = require('simplebar'); + +// $ExpectType typeof SimpleBar +SimpleBar; diff --git a/types/simplebar/v2/tsconfig.json b/types/simplebar/v2/tsconfig.json new file mode 100644 index 0000000000..8cce86f4e4 --- /dev/null +++ b/types/simplebar/v2/tsconfig.json @@ -0,0 +1,30 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "paths": { + "simplebar": [ + "simplebar/v2" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "simplebar-tests.ts", + "test/module-tests.ts" + ] +} diff --git a/types/simplebar/v2/tslint.json b/types/simplebar/v2/tslint.json new file mode 100644 index 0000000000..d88586e5bd --- /dev/null +++ b/types/simplebar/v2/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}