Skip to content

Commit 960c63a

Browse files
Hunter-GuTao-VanJS
authored andcommitted
feat(van-ui): add Await component
1 parent c08c558 commit 960c63a

File tree

5 files changed

+121
-3
lines changed

5 files changed

+121
-3
lines changed

components/dist/van-ui.d.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChildDom, State } from "vanjs-core";
1+
import { ChildDom, State, ValidChildDomValue } from "vanjs-core";
22
export type CSSPropertyBag = Record<string, string | number>;
33
export type CSSStyles = Record<string, CSSPropertyBag>;
44
export interface ModalProps {
@@ -135,3 +135,19 @@ export interface FloatingWindowProps {
135135
}
136136
export declare const topMostZIndex: () => number;
137137
export declare const FloatingWindow: ({ title, closed, x, y, width, height, closeCross, customStacking, zIndex, disableMove, disableResize, headerColor, windowClass, windowStyleOverrides, headerClass, headerStyleOverrides, childrenContainerClass, childrenContainerStyleOverrides, crossClass, crossStyleOverrides, crossHoverClass, crossHoverStyleOverrides, }: FloatingWindowProps, ...children: readonly ChildDom[]) => () => HTMLDivElement | null;
138+
interface AwaitProps<Value> {
139+
value: Promise<Value>;
140+
Loading?: () => ValidChildDomValue;
141+
Error?: (reason: Error) => ValidChildDomValue;
142+
}
143+
export type AwaitState<Value> = {
144+
status: 'pending';
145+
} | {
146+
value: Value;
147+
status: 'fulfilled';
148+
} | {
149+
value: Error;
150+
status: 'rejected';
151+
};
152+
export declare const Await: <T>({ value, Loading, Error }: AwaitProps<T>, children: (data: T) => ValidChildDomValue) => () => ValidChildDomValue;
153+
export {};

components/dist/van-ui.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,28 @@ export const FloatingWindow = ({ title, closed = van.state(false), x = 100, y =
427427
style: toStyleStr(childrenContainerStyleOverrides)
428428
}, children));
429429
};
430+
export const Await = ({ value, Loading, Error }, children) => {
431+
const data = van.state({ status: 'pending' });
432+
const resolve = (promise) => {
433+
data.val = { status: 'pending' };
434+
promise
435+
.then((result) => {
436+
data.val = {
437+
value: result,
438+
status: 'fulfilled',
439+
};
440+
})
441+
.catch((err) => {
442+
data.val = {
443+
value: err,
444+
status: 'rejected',
445+
};
446+
});
447+
};
448+
van.derive(() => resolve(value));
449+
return () => data.val.status === 'pending'
450+
? Loading?.()
451+
: data.val.status === 'rejected'
452+
? Error?.(data.val.value)
453+
: children(data.val.value);
454+
};

components/dist/van-ui.nomodule.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,29 @@
427427
style: toStyleStr(childrenContainerStyleOverrides)
428428
}, children));
429429
};
430+
window.Await = ({ value, Loading, Error }, children) => {
431+
const data = van.state({ status: 'pending' });
432+
const resolve = (promise) => {
433+
data.val = { status: 'pending' };
434+
promise
435+
.then((result) => {
436+
data.val = {
437+
value: result,
438+
status: 'fulfilled',
439+
};
440+
})
441+
.catch((err) => {
442+
data.val = {
443+
value: err,
444+
status: 'rejected',
445+
};
446+
});
447+
};
448+
van.derive(() => resolve(value));
449+
return () => data.val.status === 'pending'
450+
? Loading?.()
451+
: data.val.status === 'rejected'
452+
? Error?.(data.val.value)
453+
: children(data.val.value);
454+
};
430455
}

0 commit comments

Comments
 (0)