sample-ui-component-library 0.0.9-dev → 0.0.10-dev

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sample-ui-component-library",
3
- "version": "0.0.9-dev",
3
+ "version": "0.0.10-dev",
4
4
  "description": "A library which contains sample UI elements that can be used for populating layouts.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -37,8 +37,8 @@ export const Editor = forwardRef(({ }, ref) => {
37
37
  dispatch({ type: "MOVE_TAB", payload: { tabId, newIndex } });
38
38
  }, []);
39
39
 
40
- const addTab = useCallback((tab) => {
41
- dispatch({ type: "ADD_TAB", payload: tab });
40
+ const addTab = useCallback((tab, index) => {
41
+ dispatch({ type: "ADD_TAB", payload: { tab, index } });
42
42
  }, []);
43
43
 
44
44
  const setTabGroupId = useCallback((id) => {
@@ -8,20 +8,29 @@ export const editorReducer = (state, action) => {
8
8
  switch (action.type) {
9
9
 
10
10
  case "ADD_TAB": {
11
- // TODO: Add some validation for the payload here.
12
- const tab = state.tabs.find(obj => obj.uid === action.payload.uid);
13
- if (tab) {
14
- console.warn(`Tab with id ${action.payload.uid} already exists`);
11
+ const { tab, index } = action.payload;
12
+
13
+ const tabInfo = state.tabs.find(obj => obj.uid === tab.uid);
14
+ if (tabInfo) {
15
+ console.warn(`Tab with id ${tabInfo.uid} already exists`);
15
16
  return {
16
17
  ...state,
17
- activeTab: tab
18
+ activeTab: tabInfo
18
19
  };
19
20
  }
20
21
 
22
+ // Insert tab at specific location if it was provided.
23
+ let tabs = [...state.tabs];
24
+ if (index !== undefined && index < state.tabs.length) {
25
+ tabs.splice(index, 0, tab);
26
+ } else {
27
+ tabs.push(tab);
28
+ }
29
+
21
30
  return {
22
31
  ...state,
23
- tabs: [...state.tabs, action.payload],
24
- activeTab: action.payload
32
+ tabs: tabs,
33
+ activeTab: tab
25
34
  };
26
35
  }
27
36
 
@@ -5,6 +5,7 @@ import {
5
5
  useCallback,
6
6
  useContext,
7
7
  useImperativeHandle,
8
+ useEffect,
8
9
  } from "react";
9
10
  import "./FileBrowser.scss";
10
11
 
@@ -20,13 +21,18 @@ import { fileBrowserReducer, initialState } from "./FileBrowserReducer";
20
21
  *
21
22
  * @return {JSX}
22
23
  */
23
- export const FileBrowser = forwardRef(({ }, ref) => {
24
+ export const FileBrowser = forwardRef(({onSelectFile}, ref) => {
24
25
  const [state, dispatch] = useReducer(fileBrowserReducer, initialState);
25
26
 
26
27
  const addFileTree = useCallback((tree) => {
27
28
  dispatch({ type: "ADD_FILE_TREE", payload: tree });
28
29
  }, []);
29
30
 
31
+ useEffect(() => {
32
+ if (state.selectedNode && state.selectedNode.type === "file") {
33
+ onSelectFile(state.selectedNode);
34
+ }
35
+ }, [state.selectedNode]);
30
36
 
31
37
  const selectNode = useCallback((node) => {
32
38
  dispatch({ type: "SELECT_NODE", payload: node });
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  setDefaultCollapsed,
3
3
  collapseTree,
4
- selectNode,
5
4
  flattenTree,
6
5
  } from "./helper";
7
6
 
8
7
  export const initialState = {
9
8
  tree: {},
10
9
  flattenedTree: {},
10
+ selectedNode: null,
11
11
  collapsedTree: [],
12
12
  };
13
13
 
@@ -28,8 +28,10 @@ export const fileBrowserReducer = (state, action) => {
28
28
 
29
29
  case "SELECT_NODE": {
30
30
  const tree = [...state.flattenedTree];
31
+ let selectedNode;
31
32
  tree.forEach((n) => {
32
33
  if (n.uid === action.payload) {
34
+ selectedNode = n;
33
35
  n.selected = true;
34
36
  n.collapsed = !n.collapsed;
35
37
  } else {
@@ -40,7 +42,8 @@ export const fileBrowserReducer = (state, action) => {
40
42
  return {
41
43
  ...state,
42
44
  flattenedTree: tree,
43
- collapsedTree: collapsedTree
45
+ collapsedTree: collapsedTree,
46
+ selectedNode: selectedNode
44
47
  }
45
48
  }
46
49
  }
@@ -8,7 +8,6 @@ export const setDefaultCollapsed = (tree) => {
8
8
  return tree;
9
9
  }
10
10
 
11
-
12
11
  /**
13
12
  * Given a file tree, it flattens the tree into a list of nodes with the level of each node in the tree.
14
13
  *
@@ -58,14 +57,3 @@ export const collapseTree = (tree) => {
58
57
  }
59
58
  return rows;
60
59
  }
61
-
62
- /**
63
- * Selects the given node and deselects all other nodes in the tree.
64
- */
65
- export const selectNode = (tree, node) => {
66
- tree.forEach((n) => {
67
- n.selected = false;
68
- });
69
- node.selected = true;
70
- node.collapsed = !node.collapsed;
71
- }
@@ -51,6 +51,16 @@ const Template = (args) => {
51
51
  editorRef.current.addTab(node);
52
52
  }
53
53
  });
54
+
55
+
56
+ const node = {
57
+ "name": "SAMPLE",
58
+ "type": "file",
59
+ "uid": "dissr-f6459410-1634-4dbc-8d76-35896822158d",
60
+ "content": "1234"
61
+ }
62
+
63
+ editorRef.current.addTab(node,2);
54
64
  }, []);
55
65
 
56
66
  const onDragStart = (event) => {
@@ -39,18 +39,16 @@ const Template = (args) => {
39
39
 
40
40
  const [dragPreviewLabel, setDragPreviewLabel] = useState(<></>);
41
41
 
42
- const onNodeSelect = (selectedFile) => {
42
+ const onSelectFile = (selectedFile) => {
43
43
  action('Selected Node:')(selectedFile);
44
44
  }
45
45
 
46
46
  useEffect(() => {
47
- updateArgs({onNodeSelect : onNodeSelect});
47
+ updateArgs({onSelectFile : onSelectFile});
48
48
  }, []);
49
49
 
50
-
51
50
  useLayoutEffect(() => {
52
51
  fileBrowserRef.current.addFileTree(args.tree);
53
-
54
52
  setTimeout(() => {
55
53
  fileBrowserRef.current.selectNode("dir-f6459410-1634-4dbc-8d76-35896822158d");
56
54
  }, 3000);