@vnejs/plugins.views.screens.confirm 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ SHOW: "vne:confirm:show",
3
+ HIDE: "vne:confirm:hide",
4
+ EMIT: "vne:confirm:emit",
5
+ UPDATE: "vne:confirm:update",
6
+ ACCEPT: "vne:confirm:accept",
7
+ CHOOSE: "vne:confirm:choose",
8
+ DECLINE: "vne:confirm:decline",
9
+ };
@@ -0,0 +1,11 @@
1
+ export const TRANSITION = 500;
2
+ export const ZINDEX = 9000;
3
+
4
+ export const VIEW_PROPS = {
5
+ screen: { withBlur: true, withDark: true, zIndex: ZINDEX, transition: TRANSITION },
6
+ position: { isCentered: true },
7
+ flex: { direction: "column", gap: 60 },
8
+ flexTitles: { direction: "column", gap: 12 },
9
+ controls: { wrapWidth: 3120, flexGap: 0, flexJustify: "center", textSize: 72, textWidth: 780, textAlign: "center" },
10
+ text: { size: 96, width: 3120, align: "center" },
11
+ };
package/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import { SUBSCRIBE_EVENTS } from "./const/events";
4
+
5
+ import * as params from "./const/params";
6
+
7
+ import { Confirm } from "./modules/confirm";
8
+ import { ConfirmController } from "./modules/controller";
9
+ import { ConfirmView } from "./modules/view";
10
+
11
+ regPlugin("CONFIRM", { events: SUBSCRIBE_EVENTS, params }, [Confirm, ConfirmController, ConfirmView]);
@@ -0,0 +1,27 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class Confirm extends Module {
4
+ name = "confirm";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.CONFIRM.EMIT, this.onEmit);
8
+ this.on(this.EVENTS.CONFIRM.ACCEPT, this.onAccept);
9
+ this.on(this.EVENTS.CONFIRM.DECLINE, this.onDecline);
10
+ };
11
+
12
+ onEmit = ({ onAccept, onDecline, ...args } = {}) => {
13
+ this.onAccept = onAccept;
14
+ this.onDecline = onDecline;
15
+ return this.emit(this.EVENTS.CONFIRM.SHOW, { ...args, onClose: this.onClose });
16
+ };
17
+
18
+ onClose = () => {
19
+ delete this.onAccept;
20
+ delete this.onDecline;
21
+ };
22
+
23
+ onAccept = () => Promise.all([this.accept(), this.emit(this.EVENTS.CONFIRM.HIDE)].filter(Boolean));
24
+ onDecline = () => Promise.all([this.decline(), this.emit(this.EVENTS.CONFIRM.HIDE)].filter(Boolean));
25
+ accept = () => this.onAccept && this.onAccept();
26
+ decline = () => this.onDecline && this.onDecline();
27
+ }
@@ -0,0 +1,26 @@
1
+ import { ModuleController } from "@vnejs/module.components";
2
+
3
+ export class ConfirmController extends ModuleController {
4
+ name = "confirm.controller";
5
+
6
+ maxCurrentItem = 1;
7
+
8
+ updateEvent = this.EVENTS.CONFIRM.UPDATE;
9
+ controls = {
10
+ [this.CONST.CONTROLS.BUTTONS.INTERACT]: () => this.emit(this.maxCurrentItem === 0 ? this.EVENTS.CONFIRM.ACCEPT : this.EVENTS.CONFIRM.DECLINE),
11
+ [this.CONST.CONTROLS.BUTTONS.ACCEPT]: () => this.emit(this.EVENTS.CONFIRM.ACCEPT),
12
+ [this.CONST.CONTROLS.BUTTONS.BACK]: () => this.emit(this.EVENTS.CONFIRM.DECLINE),
13
+ [this.CONST.CONTROLS.BUTTONS.ARROW_LEFT]: this.decCurrentItem,
14
+ [this.CONST.CONTROLS.BUTTONS.ARROW_RIGHT]: this.incCurrentItem,
15
+ };
16
+ controlsIndex = this.PARAMS.CONFIRM.ZINDEX;
17
+
18
+ subscribe = () => {
19
+ this.on(this.EVENTS.CONFIRM.SHOW, this.onShow);
20
+ this.on(this.EVENTS.CONFIRM.HIDE, this.onHide);
21
+
22
+ this.on(this.EVENTS.STATE.CLEAR, this.onHide);
23
+ };
24
+
25
+ beforeShow = (args) => this.setState(args);
26
+ }
@@ -0,0 +1,13 @@
1
+ import { ModuleView } from "@vnejs/module.components";
2
+
3
+ import { render } from "../view";
4
+
5
+ export class ConfirmView extends ModuleView {
6
+ name = "confirm.view";
7
+
8
+ animationTime = this.PARAMS.CONFIRM.TRANSITION;
9
+ updateEvent = this.EVENTS.CONFIRM.UPDATE;
10
+
11
+ renderFunc = render;
12
+ updateHandler = this.onUpdateStoreComponent;
13
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.views.screens.confirm",
3
+ "version": "0.1.4",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major:plugin": "npm run publish:major",
8
+ "publish:minor:plugin": "npm run publish:minor",
9
+ "publish:patch:plugin": "npm run publish:patch",
10
+ "publish:major": "npm version major && npm publish --access public",
11
+ "publish:minor": "npm version minor && npm publish --access public",
12
+ "publish:patch": "npm version patch && npm publish --access public"
13
+ },
14
+ "author": "",
15
+ "license": "ISC",
16
+ "description": ""
17
+ }
package/view/index.jsx ADDED
@@ -0,0 +1,45 @@
1
+ import { useCallback, useEffect, useMemo, useState } from "react";
2
+ import { createRoot } from "react-dom/client";
3
+
4
+ import { Flex, PositionBox, Screen, Text, TextControls } from "@vnejs/uis.react";
5
+
6
+ const renderTitleLine = ({ text, props } = {}) => (
7
+ <Text
8
+ {...props}
9
+ text={text}
10
+ key={text}
11
+ />
12
+ );
13
+
14
+ const mapChoises = (text, i) => ({ text, value: i === 0 });
15
+
16
+ const ConfirmView = ({ store, onMount, ...props } = {}) => {
17
+ const [{ isShow = false, isForce = false, text = "", textAccept = "", textDecline = "", currentItem: selectedIndex = null }, setState] = useState({});
18
+
19
+ useEffect(() => store.subscribe(setState), []);
20
+ useEffect(() => void onMount(), []);
21
+
22
+ const texts = useMemo(() => [textAccept, textDecline].filter(Boolean).map(mapChoises), [textAccept, textDecline]);
23
+
24
+ const onClick = useCallback((isAccept) => props.emit(isAccept ? props.EVENTS.CONFIRM.ACCEPT : props.EVENTS.CONFIRM.DECLINE), []);
25
+
26
+ const propsView = props.PARAMS.CONFIRM.VIEW_PROPS;
27
+
28
+ const propsScreen = useMemo(() => ({ ...propsView.screen, isShow, isForce }), [isShow, isForce]);
29
+ const propsControls = useMemo(() => ({ ...propsView.controls, selectedIndex, texts, onClick }), [selectedIndex, texts]);
30
+
31
+ const textsWithProps = useMemo(() => (text || "").split("\n").map((text = "") => ({ text, props: propsView.text })), [text]);
32
+
33
+ return (
34
+ <Screen {...propsScreen}>
35
+ <PositionBox {...propsView.position}>
36
+ <Flex {...propsView.flex}>
37
+ <Flex {...propsView.flexTitles}>{textsWithProps.map(renderTitleLine)}</Flex>
38
+ <TextControls {...propsControls} />
39
+ </Flex>
40
+ </PositionBox>
41
+ </Screen>
42
+ );
43
+ };
44
+
45
+ export const render = (props, root) => createRoot(root).render(<ConfirmView {...props} />);