22
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
23
THE SOFTWARE.
24
24
*/
25
- import React , { useEffect , useState } from 'react' ;
25
+ import React , { useMemo , useState } from 'react' ;
26
26
import type { Categorization , Category , LayoutProps } from '@jsonforms/core' ;
27
+ import { isVisible } from '@jsonforms/core' ;
27
28
import {
28
29
TranslateProps ,
29
30
withJsonFormsLayoutProps ,
30
31
withTranslateProps ,
31
32
} from '@jsonforms/react' ;
32
33
import { CategorizationList } from './CategorizationList' ;
33
34
import { SingleCategory } from './SingleCategory' ;
34
- import { isCategorization } from './tester' ;
35
35
import { withVanillaControlProps } from '../../util' ;
36
- import type { VanillaRendererProps } from '../../index' ;
36
+ import type { AjvProps , VanillaRendererProps } from '../../index' ;
37
+ import { withAjvProps } from '../../index' ;
37
38
38
39
export interface CategorizationState {
39
40
selectedCategory : Category ;
40
41
}
41
42
43
+ interface CategorizationProps {
44
+ selected ?: number ;
45
+ onChange ?( selected : number , prevSelected : number ) : void ;
46
+ }
47
+
42
48
export const CategorizationRenderer = ( {
49
+ data,
43
50
uischema,
44
51
schema,
45
52
path,
53
+ selected,
46
54
t,
47
55
visible,
48
56
getStyleAsClassName,
49
- } : LayoutProps & VanillaRendererProps & TranslateProps ) => {
50
- const findCategory = ( categorization : Categorization ) : Category => {
51
- const category = categorization . elements [ 0 ] ;
52
-
53
- if ( isCategorization ( category ) ) {
54
- return findCategory ( category ) ;
55
- }
56
-
57
- return category ;
58
- } ;
59
-
57
+ onChange,
58
+ ajv,
59
+ } : LayoutProps &
60
+ VanillaRendererProps &
61
+ TranslateProps &
62
+ CategorizationProps &
63
+ AjvProps ) => {
60
64
const categorization = uischema as Categorization ;
65
+ const categories = useMemo (
66
+ ( ) =>
67
+ categorization . elements . filter ( ( category : Category ) =>
68
+ isVisible ( category , data , undefined , ajv )
69
+ ) ,
70
+ [ categorization , data , ajv ]
71
+ ) as [ Category ] ;
61
72
const classNames = getStyleAsClassName ( 'categorization' ) ;
62
73
const masterClassNames = getStyleAsClassName ( 'categorization.master' ) ;
63
74
const detailClassNames = getStyleAsClassName ( 'categorization.detail' ) ;
64
75
const subcategoriesClassName = getStyleAsClassName ( 'category.subcategories' ) ;
65
76
const groupClassName = getStyleAsClassName ( 'category.group' ) ;
66
77
67
- useEffect ( ( ) => {
68
- setSelectedCategory ( findCategory ( categorization ) ) ;
69
- } , [ uischema , schema ] ) ;
78
+ const [ previousCategorization , setPreviousCategorization ] =
79
+ useState < Categorization > ( uischema as Categorization ) ;
80
+ const [ activeCategory , setActiveCategory ] = useState < number > ( selected ?? 0 ) ;
70
81
71
- const [ selectedCategory , setSelectedCategory ] = useState (
72
- findCategory ( categorization )
73
- ) ;
82
+ const safeCategory =
83
+ activeCategory >= categorization . elements . length ? 0 : activeCategory ;
74
84
75
- const onCategorySelected = ( category : Category ) => ( ) => {
76
- return setSelectedCategory ( category ) ;
85
+ if ( categorization !== previousCategorization ) {
86
+ setActiveCategory ( 0 ) ;
87
+ setPreviousCategorization ( categorization ) ;
88
+ }
89
+
90
+ const onCategorySelected = ( categoryIndex : number ) => ( ) => {
91
+ if ( onChange ) {
92
+ return onChange ( categoryIndex , safeCategory ) ;
93
+ }
94
+ return setActiveCategory ( categoryIndex ) ;
77
95
} ;
78
96
79
97
return (
@@ -84,7 +102,7 @@ export const CategorizationRenderer = ({
84
102
< div className = { masterClassNames } >
85
103
< CategorizationList
86
104
categorization = { categorization }
87
- selectedCategory = { selectedCategory }
105
+ selectedCategory = { categories [ safeCategory ] }
88
106
depth = { 0 }
89
107
onSelect = { onCategorySelected }
90
108
subcategoriesClassName = { subcategoriesClassName }
@@ -94,15 +112,18 @@ export const CategorizationRenderer = ({
94
112
</ div >
95
113
< div className = { detailClassNames } >
96
114
< SingleCategory
97
- category = { selectedCategory }
115
+ category = { categories [ safeCategory ] }
98
116
schema = { schema }
99
117
path = { path }
118
+ key = { safeCategory }
100
119
/>
101
120
</ div >
102
121
</ div >
103
122
) ;
104
123
} ;
105
124
106
- export default withVanillaControlProps (
107
- withTranslateProps ( withJsonFormsLayoutProps ( CategorizationRenderer ) )
125
+ export default withAjvProps (
126
+ withVanillaControlProps (
127
+ withTranslateProps ( withJsonFormsLayoutProps ( CategorizationRenderer ) )
128
+ )
108
129
) ;
0 commit comments