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