@@ -8,6 +8,8 @@ import React, {
8
8
} from 'react' ;
9
9
import { Box , Flex } from 'rebass' ;
10
10
import ResizeObserver from 'resize-observer-polyfill' ;
11
+ import * as R from 'ramda' ;
12
+
11
13
import {
12
14
summaryContainerStyles ,
13
15
containerStyles ,
@@ -51,7 +53,7 @@ const FormTabs: FC<FormTabsProps> = ({
51
53
hasScrollOnScreen = true ,
52
54
...props
53
55
} : FormTabsProps ) => {
54
- const initialActive = useMemo ( ( ) => {
56
+ const active = useMemo ( ( ) => {
55
57
const tabIdx = tabs . findIndex ( ( t ) => t . id === initialTab ) ;
56
58
return Math . max ( tabIdx , 0 ) ;
57
59
} , [ initialTab , tabs ] ) ;
@@ -61,54 +63,47 @@ const FormTabs: FC<FormTabsProps> = ({
61
63
const hasOverflow = useCallback ( ( ) => {
62
64
const el = tabsContainerRef ?. current ;
63
65
return el ? el . clientWidth !== el . scrollWidth : false ;
64
- } , [ tabsContainerRef ?. current ] ) ;
66
+ } , [ ] ) ;
65
67
66
- const [ active , setActive ] = useState < number > ( initialActive ) ;
67
68
const [ tabArray , setTabArray ] = useState < TabItem [ ] > ( tabs ) ;
68
69
const [ isOverflown , setIsOverflown ] = useState < boolean > ( false ) ;
69
70
70
71
const currentTab = useMemo ( ( ) => tabArray [ active ] , [ tabArray , active ] ) ;
71
72
72
73
const handleGoForward = useCallback ( async ( ) => {
73
74
const validation = await currentTab . validationFn ( ) ;
74
- const copy = [ ...tabArray . map ( ( x ) => x ) ] ;
75
75
76
76
// VALID: move to next tab and show check mark.
77
77
if ( validation === ValidateOpts . valid ) {
78
- copy [ active ] = { ...copy [ active ] , state : TabState . valid } ;
79
- onTabChange ( copy [ active + 1 ] . id ) ;
80
- setActive ( ( act ) => act + 1 ) ;
78
+ setTabArray ( R . adjust ( active , R . assoc ( 'state' , TabState . valid ) ) ) ;
79
+ onTabChange ( tabArray [ active + 1 ] . id ) ;
81
80
}
82
81
83
82
// UNTOUCHED: if optional, move to next tab. Otherwise show error.
84
83
if ( validation === ValidateOpts . untouched ) {
85
84
if ( currentTab . optional ) {
86
- copy [ active ] = { ...copy [ active ] , state : TabState . optional } ;
87
- onTabChange ( copy [ active + 1 ] . id ) ;
88
- setActive ( ( act ) => act + 1 ) ;
85
+ setTabArray ( R . adjust ( active , R . assoc ( 'state' , TabState . optional ) ) ) ;
86
+ onTabChange ( tabArray [ active + 1 ] . id ) ;
89
87
} else {
90
- copy [ active ] = { ... copy [ active ] , state : TabState . error } ;
88
+ setTabArray ( R . adjust ( active , R . assoc ( ' state' , TabState . error ) ) ) ;
91
89
}
92
90
}
93
91
94
92
// INVALID: don't move to next tab and show error.
95
93
if ( validation === ValidateOpts . error ) {
96
- copy [ active ] = { ... copy [ active ] , state : TabState . error } ;
94
+ setTabArray ( R . adjust ( active , R . assoc ( ' state' , TabState . error ) ) ) ;
97
95
}
98
- setTabArray ( copy ) ;
99
- } , [ setActive , active , setTabArray , tabArray , onTabChange , currentTab ] ) ;
96
+ } , [ active , setTabArray , tabArray , onTabChange , currentTab ] ) ;
100
97
101
98
const handleGoBack = useCallback ( ( ) => {
102
99
onTabChange ( tabArray [ active - 1 ] . id ) ;
103
- setActive ( ( act ) => act - 1 ) ;
104
- } , [ active , onTabChange , setActive ] ) ;
100
+ } , [ active , onTabChange , tabArray ] ) ;
105
101
106
102
const handleTabClick = useCallback (
107
103
( idx : number ) => {
108
- setActive ( idx ) ;
109
104
onTabChange ( tabArray [ idx ] . id ) ;
110
105
} ,
111
- [ setActive , onTabChange ] ,
106
+ [ onTabChange , tabArray ] ,
112
107
) ;
113
108
114
109
const handleSubmit = useCallback ( async ( ) => {
@@ -141,7 +136,7 @@ const FormTabs: FC<FormTabsProps> = ({
141
136
) ;
142
137
setTabArray ( updated ) ;
143
138
return allValid ;
144
- } , [ ] ) ;
139
+ } , [ tabArray ] ) ;
145
140
146
141
useEffect ( ( ) => {
147
142
if ( ! tabsContainerRef . current ) return ;
@@ -150,9 +145,10 @@ const FormTabs: FC<FormTabsProps> = ({
150
145
setIsOverflown ( hasOverflow ( ) ) ;
151
146
} ) ;
152
147
resizeObserver . observe ( tabsContainerRef . current ) ;
148
+
153
149
// eslint-disable-next-line consistent-return
154
150
return ( ) => resizeObserver . disconnect ( ) ;
155
- } , [ tabsContainerRef ?. current ] ) ;
151
+ } , [ hasOverflow ] ) ;
156
152
157
153
return (
158
154
< FormSummaryContainer
0 commit comments