5
5
// React-Invenio-Forms is free software; you can redistribute it and/or modify it
6
6
// under the terms of the MIT License; see LICENSE file for more details.
7
7
8
- import React , { Component } from "react" ;
8
+ import React , { Component , useState } from "react" ;
9
9
import PropTypes from "prop-types" ;
10
10
import { Field , FastField } from "formik" ;
11
- import { Accordion , Container } from "semantic-ui-react" ;
11
+ import { Accordion , Container , Icon } from "semantic-ui-react" ;
12
12
import _omit from "lodash/omit" ;
13
13
import _get from "lodash/get" ;
14
14
@@ -44,7 +44,6 @@ export class AccordionField extends Component {
44
44
key : `panel-${ label } ` ,
45
45
title : {
46
46
content : label ,
47
- icon : "angle right" ,
48
47
} ,
49
48
content : {
50
49
content : < Container > { children } </ Container > ,
@@ -53,15 +52,40 @@ export class AccordionField extends Component {
53
52
] ;
54
53
55
54
const errorClass = hasError ? "error secondary" : "" ;
55
+ const [ activeIndex , setActiveIndex ] = useState ( 0 ) ;
56
+
57
+ const handleTitleClick = ( e , { index } ) => {
58
+ setActiveIndex ( activeIndex === index ? - 1 : index ) ;
59
+ } ;
56
60
57
61
return (
58
62
< Accordion
59
- defaultActiveIndex = { active ? 0 : null }
60
- panels = { panels }
61
63
inverted
62
64
className = { `invenio-accordion-field ${ errorClass } ` }
63
65
{ ...uiProps }
64
- />
66
+ >
67
+ { panels . map ( ( panel , index ) => (
68
+ < React . Fragment key = { panel . key } >
69
+ < Accordion . Title
70
+ active = { activeIndex === index }
71
+ index = { index }
72
+ onClick = { handleTitleClick }
73
+ onKeyDown = { ( e ) => {
74
+ if ( e . key === "Enter" || e . key === " " ) {
75
+ handleTitleClick ( e , { index } ) ;
76
+ }
77
+ } }
78
+ tabIndex = { 0 }
79
+ >
80
+ { panel . title . content }
81
+ < Icon name = "angle right" />
82
+ </ Accordion . Title >
83
+ < Accordion . Content active = { activeIndex === index } >
84
+ { panel . content . content }
85
+ </ Accordion . Content >
86
+ </ React . Fragment >
87
+ ) ) }
88
+ </ Accordion >
65
89
) ;
66
90
} ;
67
91
0 commit comments