From 91004dcc7ca0650c61462e564489942109ea9a48 Mon Sep 17 00:00:00 2001 From: Fatimah Date: Fri, 29 Sep 2023 10:52:37 +0200 Subject: [PATCH] ui: added toggle and keyboard focus for accordion --- src/lib/forms/AccordionField.js | 58 ++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/lib/forms/AccordionField.js b/src/lib/forms/AccordionField.js index a32e9f73..61742c6d 100644 --- a/src/lib/forms/AccordionField.js +++ b/src/lib/forms/AccordionField.js @@ -5,10 +5,10 @@ // React-Invenio-Forms is free software; you can redistribute it and/or modify it // under the terms of the MIT License; see LICENSE file for more details. -import React, { Component } from "react"; +import React, { Component, useState } from "react"; import PropTypes from "prop-types"; import { Field, FastField } from "formik"; -import { Accordion, Container } from "semantic-ui-react"; +import { Accordion, Container, Icon } from "semantic-ui-react"; import _omit from "lodash/omit"; import _get from "lodash/get"; @@ -39,29 +39,55 @@ export class AccordionField extends Component { const hasError = status ? this.hasError(status) : this.hasError(errors) || this.hasError(initialErrors, initialValues, values); - const panels = [ - { - key: `panel-${label}`, - title: { - content: label, - icon: "angle right", + const panels = [ + { + key: `panel-${label}`, + title: { + content: label, + }, + content: { + content: {children}, + }, }, - content: { - content: {children}, - }, - }, - ]; + ]; const errorClass = hasError ? "error secondary" : ""; + const [activeIndex, setActiveIndex] = useState(0); + const handleTitleClick = (e, { index }) => { + setActiveIndex(activeIndex === index ? -1 : index); + }; + return ( + > + {panels.map((panel, index) => ( + + { + if (e.key === 'Enter' || e.key === ' ') { + handleTitleClick(e, { index }); + } + }} + tabIndex={0} + > + {panel.title.content} + + + + {panel.content.content} + + + ))} + ); };