Skip to content

Commit af8ee7e

Browse files
committed
wip: update
1 parent 66cfde7 commit af8ee7e

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

misc.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ declare namespace Layui {
1111
? never
1212
: KeyType]: ObjectType[KeyType];
1313
};
14+
type PlainObject<T = any> = {
15+
[key: string]: T;
16+
}
1417

18+
type Selector = string;
1519
type ExportsCallback = (this: Layui, fn: (app: string, exports: object) => void) => void;
1620

1721
/**

modules/lay.d.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ declare namespace Layui {
4040
* 对元素遍历
4141
* @param fn 回调函数,返回 `true` 则停止遍历,和 jQuery 相反
4242
*/
43-
each(fn?: (this: TElement, index: number, elem: TElement) => any): this;
43+
each(fn: (this: TElement, index: number, elem: TElement) => any): this;
4444
/**
4545
* 查找子元素
4646
* @param selector 选择器
@@ -80,6 +80,16 @@ declare namespace Layui {
8080
fn: (this: TElement, e: HTMLElementEventMap[K]) => any,
8181
options?: boolean | EventListenerOptions
8282
): this;
83+
off<K extends keyof DocumentEventMap>(
84+
eventName: K,
85+
fn: (this: TElement, e: DocumentEventMap[K]) => any,
86+
options?: boolean | EventListenerOptions
87+
): this;
88+
off<K extends keyof WindowEventMap>(
89+
eventName: K,
90+
fn: (this: TElement, e: WindowEventMap[K]) => any,
91+
options?: boolean | EventListenerOptions
92+
): this;
8393
/**
8494
* 事件绑定,注意:只支持内置事件,不支持自定义事件
8595
* @param eventName 事件名 比如click,自定事件会绑定失败
@@ -91,6 +101,16 @@ declare namespace Layui {
91101
fn: (this: TElement, e: HTMLElementEventMap[K]) => any,
92102
options?: boolean | AddEventListenerOptions
93103
): this;
104+
on<K extends keyof DocumentEventMap>(
105+
eventName: K,
106+
fn: (this: TElement, e: DocumentEventMap[K]) => any,
107+
options?: boolean | AddEventListenerOptions
108+
): this;
109+
on<K extends keyof WindowEventMap>(
110+
eventName: K,
111+
fn: (this: TElement, e: WindowEventMap[K]) => any,
112+
options?: boolean | AddEventListenerOptions
113+
): this;
94114
/**
95115
* 移除元素
96116
* @param elem 实际是 removeChild(elem)
@@ -228,7 +248,15 @@ declare namespace Layui {
228248
* 查找 DOM 作为返回实例的操作对象
229249
* @param selector 选择器
230250
*/
231-
(selector?: string | HTMLElement | JQuery): Lay;
251+
(window: Window): Lay<Window>;
252+
<T extends keyof HTMLElementTagNameMap>(selector: T): Lay<HTMLElementTagNameMap[T]>;
253+
<T extends keyof SVGElementTagNameMap>(selector: T): Lay<SVGElementTagNameMap[T]>;
254+
<T extends globalThis.Element = HTMLElement>(selector: Layui.Selector): Lay<T>;
255+
(element: HTMLSelectElement): Lay<HTMLSelectElement>;
256+
<T extends globalThis.Element>(element_or_elementArray: T | ArrayLike<T>): Lay<T>;
257+
<T>(selection: Lay<T>): Lay<T>;
258+
<T extends Layui.PlainObject>(object: T): Lay<T>;
259+
<T = HTMLElement>(): Lay<T>;
232260
/**
233261
* 版本
234262
*/
@@ -276,7 +304,7 @@ declare namespace Layui {
276304
stope(event: Event | JQuery.Event): void;
277305
/**
278306
* 对象(Array、Object、DOM 对象等)遍历,可用于取代 for 语句
279-
* @param collection Array对象
307+
* @param collection 集合,可以是数组或对象等可遍历的元素
280308
* @param callback 回调函数,返回 true 停止遍历,和 jQUery.each 相反
281309
*/
282310
each<T>(collection: ArrayLike<T>, callback: (this: T, indexInArray: number, value: T) => any): Lay;
@@ -302,7 +330,8 @@ declare namespace Layui {
302330
* lay.elem('div', {id: 'test'}) // <div id="test"></div>
303331
* ```
304332
*/
305-
elem<K extends keyof HTMLElementTagNameMap>(elemName: K, attr?: Record<string, any>): HTMLElementTagNameMap[K];
333+
elem<K extends keyof HTMLElementTagNameMap>(tagName: K, attr?: Record<string, any>): HTMLElementTagNameMap[K];
334+
elem(tagName: string, attr?: Record<string, any>): HTMLElement;
306335
/**
307336
* 当前页面body是否存在滚动条
308337
*/
@@ -432,7 +461,7 @@ declare namespace Layui {
432461
* @since 2.9.2
433462
*/
434463
touchSwipe(
435-
elem: string | HTMLElement | JQuery,
464+
elem: string | Element | JQuery,
436465
options: {
437466
onTouchStart(e: TouchEvent, state: LayTouchSwipeState): void;
438467
onTouchMove(e: TouchEvent, state: LayTouchSwipeState): void;

modules/layer.d.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ declare namespace Layui {
4646
* @param layero 当前层的 jQuery 对象
4747
*/
4848
type LayerCallbackPrompt = (value: string, index: number, layero: JQuery) => void;
49-
type LayerType = 0 | 1 | 2 | 3 | 4;
49+
type layerTypeMap = {
50+
0: 'dialog',
51+
1: 'page',
52+
2: 'iframe',
53+
3: 'loading',
54+
4: 'tips'
55+
}
56+
type LayerType = keyof layerTypeMap;
5057
type LayerIndex = number;
5158
/**
5259
* 弹层选项
@@ -529,7 +536,7 @@ declare namespace Layui {
529536
* - 2 多行文本输入框
530537
* @default 0
531538
*/
532-
formType?: number;
539+
formType?: 0 | 1 | 2;
533540
/**
534541
* 输入框初始值
535542
* @default ''
@@ -817,7 +824,7 @@ declare namespace Layui {
817824
* @param type 关闭的弹层类型,不传则关闭全部
818825
* @param callback 关闭所有层后执行回调
819826
*/
820-
closeAll(type?: 'dialog' | 'page' | 'iframe' | 'loading' | 'tips', callback?: () => any): void;
827+
closeAll(type?: layerTypeMap[keyof layerTypeMap], callback?: () => any): void;
821828
/**
822829
* 关闭所有层
823830
* @param callback 关闭所有层后执行回调
@@ -829,7 +836,7 @@ declare namespace Layui {
829836
* @param callback 关闭后执行的回调(2.9.0)
830837
* @since 2.8.0
831838
*/
832-
closeLast(type?: MaybeArray<'dialog' | 'page' | 'iframe' | 'loading' | 'tips'>, callback?: AnyFn): void;
839+
closeLast(type?: layerTypeMap[keyof layerTypeMap] | Array<layerTypeMap[keyof layerTypeMap]>, callback?: AnyFn): void;
833840
/**
834841
* 重新定义层的样式
835842
* @param index 打开弹层时返回的唯一索引

test/lay.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ function layTest() {
1515
layui.lay.options('.a');
1616
layui.lay.options('.a', 'id');
1717

18+
lay()
19+
lay(window)
20+
lay(document)
21+
lay(document.documentElement)
22+
lay([document.body, document.body])
23+
lay(lay.elem('select'))
24+
lay((lay()))
25+
lay($('div'))
26+
lay('div')
27+
lay('.touchEventsSupported')
1828
const ll = layui.lay(document.body);
1929
ll.addClass('abc a', false);
2030
ll.addClass('abc b', true);
@@ -37,6 +47,8 @@ function layTest() {
3747
layui.lay('#abc').on('click', function(e) {
3848
console.log(this, e);
3949
});
50+
lay(window).on('resize', e => {})
51+
lay(document).on('load', e => {})
4052
layui.laypage.on(document.getElementById('abc'), 'click', e => {
4153
console.log(e);
4254
});

0 commit comments

Comments
 (0)