@vnejs/plugins.view.coralina.mainmenu 0.1.1 → 0.1.2

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.
package/modules/view.js CHANGED
@@ -10,5 +10,5 @@ export class MainMenuView extends ModuleView {
10
10
  updateEvent = this.EVENTS.MAINMENU_VIEW.UPDATE;
11
11
 
12
12
  renderFunc = render;
13
- updateHandler = this.onUpdateReactComponent;
13
+ updateHandler = this.onUpdateStoreComponent;
14
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.view.coralina.mainmenu",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
package/view/index.jsx CHANGED
@@ -1,67 +1,55 @@
1
- import React from "react";
1
+ import React, { useCallback, useEffect, useState } from "react";
2
2
  import ReactDOM from "react-dom";
3
3
 
4
4
  import { PositionBox, Screen, TextControls } from "@vnejs/uis.base";
5
5
 
6
- class MainMenuControls extends React.Component {
7
- state = {};
8
-
9
- componentDidUpdate(props) {
10
- const { items = [] } = this.props;
11
-
12
- if (items !== props.items || this.props.realItems !== props.realItems || this.props.locs !== props.locs) this.setState({ texts: items.map(this.mapItems) });
13
- if (this.props.currentItem !== props.currentItem) this.setState({ selectedIndex: items.findIndex(this.findSelectedIndex) });
14
- }
15
- componentDidMount() {
16
- this.setState({ texts: this.props.items.map(this.mapItems), selectedIndex: this.props.items.findIndex(this.findSelectedIndex) });
17
- }
18
-
19
- mapItems = ({ locKey, isDisabled, isHide }, i) => ({ text: this.props.locs[locKey], isDisabled, isNotRender: isHide, value: i });
20
- findSelectedIndex = (value) => this.props.realItems[this.props.currentItem]?.locKey === value?.locKey;
21
-
22
- onClick = (index) => this.props.emit(this.props.EVENTS.MAINMENU_VIEW.CLICK, { index });
23
-
24
- render() {
25
- const { texts = [], selectedIndex = null } = this.state;
26
-
27
- return (
28
- <TextControls
29
- selectedIndex={selectedIndex}
30
- texts={texts}
31
- onClick={this.onClick}
32
- {...this.props.PARAMS.MAINMENU_VIEW.VIEW_PROPS.controls.props}
33
- />
34
- );
35
- }
36
- }
37
-
38
- class MainMenu extends React.Component {
39
- state = {};
40
-
41
- render() {
42
- const { isShow = false, items = [], bgSrc = "", currentItem = null, realItems = [], locs = {} } = this.state;
43
-
44
- return (
45
- <Screen
46
- isShow={isShow}
47
- bgSrc={bgSrc}
48
- zIndex={this.props.PARAMS.MAINMENU_VIEW.ZINDEX}
49
- transition={this.props.PARAMS.MAINMENU_VIEW.TRANSITION}
50
- withBackgroundDark={this.props.PARAMS.MAINMENU_VIEW.VIEW_PROPS.background.withDark}
51
- withBackgroundBlur={this.props.PARAMS.MAINMENU_VIEW.VIEW_PROPS.background.withBlur}
52
- >
53
- <PositionBox {...this.props.PARAMS.MAINMENU_VIEW.VIEW_PROPS.controls.position}>
54
- <MainMenuControls
55
- {...this.props}
56
- items={items}
57
- realItems={realItems}
58
- currentItem={currentItem}
59
- locs={locs}
60
- />
61
- </PositionBox>
62
- </Screen>
63
- );
64
- }
65
- }
66
-
67
- export const render = (props, root, resolve) => ReactDOM.render(<MainMenu {...props} />, root, resolve);
6
+ const MainMenuControls = (props) => {
7
+ const [texts, setTexts] = useState([]);
8
+ const [selectedIndex, setSelectedIndex] = useState(-1);
9
+
10
+ const mapItems = useCallback(({ locKey, isDisabled, isHide }, i) => ({ text: props.locs[locKey], isDisabled, isNotRender: isHide, value: i }), [props.locs]);
11
+ const findSelectedIndex = useCallback((value) => props.realItems[props.currentItem]?.locKey === value?.locKey, [props.realItems, props.currentItem]);
12
+ const onClick = useCallback((index) => props.emit(props.EVENTS.MAINMENU_VIEW.CLICK, { index }), []);
13
+
14
+ useEffect(() => void setTexts(props.items.map(mapItems)), [props.items, mapItems]);
15
+ useEffect(() => void setSelectedIndex(props.items.findIndex(findSelectedIndex)), [props.items, findSelectedIndex]);
16
+
17
+ return (
18
+ <TextControls
19
+ texts={texts}
20
+ onClick={onClick}
21
+ selectedIndex={selectedIndex}
22
+ {...props.PARAMS.MAINMENU_VIEW.VIEW_PROPS.controls.props}
23
+ />
24
+ );
25
+ };
26
+
27
+ const MainMenu = ({ store, onMount, ...props } = {}) => {
28
+ const [{ isShow = false, items = [], bgSrc = "", currentItem = null, realItems = [], locs = {} }, setState] = useState({});
29
+
30
+ useEffect(() => store.subscribe(setState), []);
31
+ useEffect(() => void onMount(), []);
32
+
33
+ return (
34
+ <Screen
35
+ bgSrc={bgSrc}
36
+ isShow={isShow}
37
+ zIndex={props.PARAMS.MAINMENU_VIEW.ZINDEX}
38
+ transition={props.PARAMS.MAINMENU_VIEW.TRANSITION}
39
+ withBackgroundDark={props.PARAMS.MAINMENU_VIEW.VIEW_PROPS.background.withDark}
40
+ withBackgroundBlur={props.PARAMS.MAINMENU_VIEW.VIEW_PROPS.background.withBlur}
41
+ >
42
+ <PositionBox {...props.PARAMS.MAINMENU_VIEW.VIEW_PROPS.controls.position}>
43
+ <MainMenuControls
44
+ {...props}
45
+ items={items}
46
+ realItems={realItems}
47
+ currentItem={currentItem}
48
+ locs={locs}
49
+ />
50
+ </PositionBox>
51
+ </Screen>
52
+ );
53
+ };
54
+
55
+ export const render = (props, root) => ReactDOM.render(<MainMenu {...props} />, root);