-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
75 lines (61 loc) · 2.33 KB
/
index.tsx
File metadata and controls
75 lines (61 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { useRef } from "react";
import ComposableRow from "../composableContainerRow";
import CoreComposableContainer from "../../core/ComposableContainer";
import _ from "lodash";
import SkyflowError from "../../utils/skyflow-error";
import SKYFLOW_ERROR_CODE from "../../utils/skyflow-error-code";
export interface IComposableContainer {
/** Type of the container. */
container: CoreComposableContainer,
/** Function to call when the onSubmit event triggers. */
onSubmit?: () => void
}
/**
* Container Component for all composable elements.
*/
const ComposableContainer: React.FC<IComposableContainer> = (props) => {
const containerRef = useRef([]);
const { container, children } = props;
const { layout, styles, errorTextStyles } = container.options;
const childComponentArray = React.Children.toArray(children);
const setParentRef = (index,elementRef,elementID)=>{
containerRef.current[index] = {elementID:elementID,ref:elementRef};
}
const shiftFocus = (currentElementID)=>{
let nextElementIndex
containerRef.current.forEach((element,index)=>{
if(element.elementID === currentElementID.current && (index + 1) !== containerRef.current.length){
nextElementIndex = index + 1
}
});
if(nextElementIndex){
const nextElementRef = containerRef.current[nextElementIndex].ref ;
if(nextElementRef) nextElementRef?.current?.focus();
}
}
const getElements = () => {
if(_.sum(layout) !== childComponentArray.length){
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISMATCH_ELEMENT_COUNT_LAYOUT_SUM, [], true);
}
let currentIndex = 0;
return layout.map((rowCount, index) => {
const rowView = <ComposableRow
key={index}
children={childComponentArray.slice(currentIndex, currentIndex + rowCount)}
styles={styles}
errorTextStyles={errorTextStyles}
setParentRef={setParentRef}
shiftFocus={shiftFocus}
rowIndex={currentIndex}
/>
currentIndex = currentIndex + rowCount;
return rowView
})
};
return (
<>
{getElements()}
</>
)
};
export default ComposableContainer