Skip to content

Commit c4e2530

Browse files
Fatimahzzacharo
authored andcommitted
ui: added toggle and keyboard focus for accordion
1 parent 2f76b0b commit c4e2530

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/lib/forms/AccordionField.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
// React-Invenio-Forms is free software; you can redistribute it and/or modify it
66
// under the terms of the MIT License; see LICENSE file for more details.
77

8-
import React, { Component } from "react";
8+
import React, { Component, useState } from "react";
99
import PropTypes from "prop-types";
1010
import { Field, FastField } from "formik";
11-
import { Accordion, Container } from "semantic-ui-react";
11+
import { Accordion, Container, Icon } from "semantic-ui-react";
1212
import _omit from "lodash/omit";
1313
import _get from "lodash/get";
1414

@@ -44,7 +44,6 @@ export class AccordionField extends Component {
4444
key: `panel-${label}`,
4545
title: {
4646
content: label,
47-
icon: "angle right",
4847
},
4948
content: {
5049
content: <Container>{children}</Container>,
@@ -53,15 +52,40 @@ export class AccordionField extends Component {
5352
];
5453

5554
const errorClass = hasError ? "error secondary" : "";
55+
const [activeIndex, setActiveIndex] = useState(0);
56+
57+
const handleTitleClick = (e, { index }) => {
58+
setActiveIndex(activeIndex === index ? -1 : index);
59+
};
5660

5761
return (
5862
<Accordion
59-
defaultActiveIndex={active ? 0 : null}
60-
panels={panels}
6163
inverted
6264
className={`invenio-accordion-field ${errorClass}`}
6365
{...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>
6589
);
6690
};
6791

0 commit comments

Comments
 (0)