Skip to content

Commit acf2c36

Browse files
authored
🤖 Merge PR DefinitelyTyped#72893 [@types/layui] add @types/layui typings by @Sight-wcg
1 parent ce811b1 commit acf2c36

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+9515
-0
lines changed

types/layui/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts

types/layui/Layui.d.ts

Lines changed: 393 additions & 0 deletions
Large diffs are not rendered by default.

types/layui/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Project Structure
2+
3+
- `index.d.ts`
4+
- Entry point for type definitions
5+
- `layui.d.ts`
6+
- Core Layui type definitions
7+
- `modules/*.d.ts`
8+
- Type definitions for individual modules
9+
- `layui-tests.ts`
10+
- Test suite for layui.js
11+
- `test/*.test.ts`
12+
- Module-specific test files
13+
14+
# Writing Type Definitions for Layui Modules
15+
16+
Declare an interface describing plugin configuration options:
17+
18+
```typescript
19+
interface MyPlugin {
20+
render(options: MyPluginOptions): MyPluginReturn;
21+
reload(options?: MyPluginOptions): void;
22+
}
23+
24+
interface MyPluginOptions {
25+
elem: string | HTMLElement | JQuery;
26+
}
27+
28+
interface MyPluginReturn {
29+
reload(options?: MyPluginOptions): void;
30+
}
31+
```
32+
33+
Then declare your module's type property on Layui.BuiltinModules:
34+
35+
```typescript
36+
declare namespace Layui{
37+
interface BuiltinModules{
38+
MyPlugin: MyPlugin;
39+
}
40+
}
41+
```
42+
43+
# Contributing
44+
45+
PRs are welcome to improve type definitions!
46+
47+
This project is based on [@types/layui-src@2.6](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/layui-src)

types/layui/index.d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference types="jquery" />
2+
/// <reference path="modules/carousel.d.ts" />
3+
/// <reference path="modules/code.d.ts" />
4+
/// <reference path="modules/colorpicker.d.ts" />
5+
/// <reference path="modules/dropdown.d.ts" />
6+
/// <reference path="modules/element.d.ts" />
7+
/// <reference path="modules/flow.d.ts" />
8+
/// <reference path="modules/form.d.ts" />
9+
/// <reference path="modules/lay.d.ts" />
10+
/// <reference path="modules/laydate.d.ts" />
11+
/// <reference path="modules/layedit.d.ts" />
12+
/// <reference path="modules/layer.d.ts" />
13+
/// <reference path="modules/laypage.d.ts" />
14+
/// <reference path="modules/laytpl.d.ts" />
15+
/// <reference path="modules/rate.d.ts" />
16+
/// <reference path="modules/slider.d.ts" />
17+
/// <reference path="modules/table.d.ts" />
18+
/// <reference path="modules/transfer.d.ts" />
19+
/// <reference path="modules/tree.d.ts" />
20+
/// <reference path="modules/treeTable.d.ts" />
21+
/// <reference path="modules/upload.d.ts" />
22+
/// <reference path="modules/util.d.ts" />
23+
/// <reference path="Layui.d.ts" />
24+
/// <reference path="misc.d.ts" />
25+
26+
declare const layui: Layui;
27+
declare const lay: Layui.LayStatic;
28+
declare const layer: Layui.Layer;
29+
/**
30+
* 动态加载等特殊场景设置 layui 目录
31+
* @since 2.6.6
32+
*/
33+
declare const LAYUI_GLOBAL: { dir: string };
34+
35+
interface Window {
36+
layui: Layui;
37+
lay: Layui.LayStatic;
38+
layer: Layui.Layer;
39+
/**
40+
* 动态加载等特殊场景设置 layui 目录
41+
* @since 2.6.6
42+
*/
43+
LAYUI_GLOBAL: { dir: string };
44+
}

types/layui/layui-tests.ts

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
// layui方法和属性
2+
function B_method() {
3+
window.lay().find;
4+
window.layer.v;
5+
window.layui.v;
6+
layui["layui.all"];
7+
layui.v;
8+
$.ajax;
9+
// layui.modules['all'];
10+
// layui.modules['notExists'];
11+
12+
// layui.define
13+
layui.define(["layer", "form"], (exports) => {
14+
const layer = layui.layer;
15+
const form = layui.form;
16+
layer.msg("Hello World");
17+
exports("index", {}); // 注意,这里是模块输出的核心,模块名必须和 use 时的模块名一致
18+
});
19+
layui.define((exports) => {
20+
// 从 layui 2.6 开始,如果你引入的是构建后的 layui.js,里面即包含了 layui 所有的内置模块,无需再指定内置模块。如
21+
// 需确保您的 layui.js 是引入的构建后的版本(即官网下载或 git 平台的发行版)
22+
// 直接可得到各种内置模块
23+
const layer = layui.layer;
24+
const form = layui.form;
25+
const table = layui.table;
26+
27+
// …
28+
layer.msg("Hello World");
29+
30+
exports("index", {}); // 注意,这里是模块输出的核心,模块名必须和 use 时的模块名一致
31+
});
32+
layui.define(["layer", "laypage", "mod1"], (exports) => {
33+
// 此处 mod1 为你的任意扩展模块
34+
// do something
35+
36+
exports("demo", {
37+
msg: "Hello Demo",
38+
});
39+
});
40+
41+
// layui 模块的使用
42+
layui.use(["mod1", "mod2"], (args) => {
43+
// 缺陷1:没法给namespace添加数组支持
44+
// layui["a"];
45+
// ……
46+
});
47+
// 引用指定模块
48+
layui.use(["layer", "laydate"], () => {
49+
// console.log(this.$);
50+
const layer = layui.layer;
51+
const laydate = layui.laydate;
52+
53+
// do something
54+
});
55+
56+
// 引用所有模块(layui 2.6 开始支持)
57+
layui.use(() => {
58+
// console.log(this.carousel);
59+
const layer = layui.layer;
60+
const laydate = layui.laydate;
61+
const table = layui.table;
62+
// …
63+
64+
// do something
65+
});
66+
// 通过回调的参数得到模块对象
67+
layui.use(["layer", "laydate", "table"], (layer: Layui.Layer, laydate, table) => {
68+
// console.log(this.carousel);
69+
// 使用 layer
70+
layer.msg("test");
71+
72+
// 使用 laydate
73+
laydate.render({});
74+
75+
// 使用 table
76+
table.render({});
77+
});
78+
layui
79+
.config({
80+
base: "/res/js/modules/", // 你的扩展模块所在目录
81+
})
82+
.use(() => {}); // 这里的 main 模块包含了 mod1、mod2、mod3 等等扩展模块
83+
layui.config({
84+
dir: "/res/layui/", // layui.js 所在目录(如果是 script 单独引入 layui.js,无需设定该参数)一般可无视
85+
version: false, // 一般用于更新模块缓存,默认不开启。设为 true 即让浏览器不缓存。也可以设为一个固定的值,如:201610
86+
debug: false, // 用于开启调试模式,默认 false,如果设为 true,则JS模块的节点会保留在页面
87+
base: "", // 设定扩展的 layui 模块的所在目录,一般用于外部模块扩展
88+
});
89+
layui.use(["layer", "form"], () => {
90+
const layer = layui.layer;
91+
const form = layui.form;
92+
93+
layer.msg("Hello World");
94+
});
95+
layui.use(() => {});
96+
layui.link("a.js");
97+
layui.link("a.js", alert);
98+
layui.link("a.js", alert, "cc");
99+
100+
layui.config({ debug: true });
101+
// 【增】:向 test 表插入一个 nickname 字段,如果该表不存在,则自动建立。
102+
layui.data("test", {
103+
key: "nickname",
104+
value: "贤心",
105+
});
106+
107+
// 【删】:删除 test 表的 nickname 字段
108+
layui.data("test", {
109+
key: "nickname",
110+
remove: true,
111+
});
112+
layui.data("test", null); // 删除test表
113+
// 【改】:同【增】,会覆盖已经存储的数据
114+
115+
// 【查】:向 test 表读取全部的数据
116+
let localTest = layui.data("test");
117+
layui.data("test");
118+
console.log(localTest.nickname); // 获得“贤心”
119+
120+
const device = layui.device();
121+
device.os === "Windows";
122+
device.android;
123+
layui.device("android");
124+
var myDevice = layui.device("myflag");
125+
myDevice.myflag;
126+
myDevice["myflag"];
127+
// layui.device().myflag;
128+
layui.device("os");
129+
130+
// 其他底层方法
131+
layui["cache"];
132+
layui.cache.base;
133+
// layui.cache.builtin['all'];
134+
// layui.cache.callback.colorpicker;
135+
// layui.cache.callback['notExists'];
136+
layui.cache.dir;
137+
layui.cache.event;
138+
layui.cache.event["carousel.change"];
139+
layui.cache.event["element.tab:"];
140+
layui.cache.event["form.select"];
141+
layui.cache.event["carousel.change"]["site-top-carousel"][0].call;
142+
layui.cache.host;
143+
layui.cache.modules["global"];
144+
layui.cache.modules.global;
145+
layui.cache.status.colorpicker;
146+
layui.cache.status["notExists"];
147+
layui.cache.timeout;
148+
layui.cache.version;
149+
150+
// config的设置是全局的
151+
layui
152+
.config({
153+
base: "/res/js/", // 假设这是你存放拓展模块的根目录
154+
})
155+
.extend({
156+
// 设定模块别名
157+
mymod: "mymod", // 如果 mymod.js 是在根目录,也可以不用设定别名
158+
mod1: "admin/mod1", // 相对于上述 base 目录的子目录
159+
});
160+
161+
// 你也可以忽略 base 设定的根目录,直接在 extend 指定路径(主要:该功能为 layui 2.2.0 新增)
162+
layui.extend({
163+
mod2: "{/}http://cdn.xxx.com/lib/mod2", // {/}的意思即代表采用自有路径,即不跟随 base 路径
164+
});
165+
layui.extend({ test: "/res/js/test" });
166+
// 使用拓展模块
167+
layui.use(["mymod", "mod1"], () => {
168+
// let mymod = layui.mymod;
169+
// mymod.hello('World!'); // 弹出 Hello World!
170+
});
171+
172+
layui.each({ a: 1 }, (k, v) => {
173+
console.log(k + v);
174+
});
175+
176+
layui.each(["a", "b"], (k, v) => {
177+
console.log(k + v);
178+
});
179+
layui._typeof("");
180+
layui._typeof([]);
181+
layui._typeof(() => 1);
182+
layui._isArray([]);
183+
184+
layui.getStyle(document.forms[0], "font-size");
185+
layui.getStyle(document.getElementById("test"), "font-size");
186+
187+
layui.img("");
188+
layui.img("", () => 1);
189+
layui.img(
190+
"",
191+
(img) => {
192+
img.sizes;
193+
},
194+
(e) => {},
195+
);
196+
197+
layui.router(location.hash);
198+
layui.router().href == null;
199+
layui.router().path[0];
200+
layui.router().search["m"];
201+
layui.router().search.constructor;
202+
203+
// 【增】:向 test 表插入一个 nickname 字段,如果该表不存在,则自动建立。
204+
layui.sessionData("test", {
205+
key: "nickname",
206+
value: "贤心",
207+
});
208+
209+
// 【删】:删除 test 表的 nickname 字段
210+
layui.sessionData("test", {
211+
key: "nickname",
212+
remove: true,
213+
});
214+
layui.sessionData("test", null); // 删除test表
215+
// 【改】:同【增】,会覆盖已经存储的数据
216+
217+
// 【查】:向 test 表读取全部的数据
218+
localTest = layui.sessionData("test");
219+
layui.sessionData("test");
220+
console.log(localTest.nickname); // 获得“贤心”
221+
222+
layui.sort([{ a: 3 }, { a: 1 }, { a: 5 }], "a");
223+
layui.sort([1, 2, 3, 4], "a");
224+
225+
window.document.onkeydown = (e) => {
226+
console.log(e);
227+
layui.stope(e);
228+
};
229+
230+
layui.url().hash.href;
231+
layui.url("").hash.href;
232+
layui.url().pathname[0];
233+
234+
layui.hint().error("出错啦");
235+
layui.hint().error(null, "log");
236+
237+
layui.on("select(*)", "form").v;
238+
layui.on("select(*)", "form", console.log)();
239+
240+
layui.onevent("form", "select(*)").v === "";
241+
const x = layui.onevent("form", "select(*)", console.log);
242+
let y = layui.event("form", "select(abc)", 2);
243+
y = layui.event("form", "select(abc)", [1, 2, 3]);
244+
245+
layui.off("select(filter)", "form");
246+
const factoryCallback = layui.factory("form");
247+
if (factoryCallback) {
248+
factoryCallback();
249+
}
250+
251+
layui.disuse("flow");
252+
layui.disuse(["form", "lay"]);
253+
layui.disuse("t");
254+
layui.disuse(["t", "form"]);
255+
256+
var dFn = layui.debounce((a: number, b: string) => {
257+
return a + b;
258+
}, 0);
259+
dFn(1, "2");
260+
var tFn = layui.throttle((a: number, b: string) => {
261+
return a + b;
262+
}, 0);
263+
tFn(1, "2");
264+
}

0 commit comments

Comments
 (0)