Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: added toggle and keyboard focus for accordion #217

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/lib/forms/AccordionField.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -44,7 +44,6 @@ export class AccordionField extends Component {
key: `panel-${label}`,
title: {
content: label,
icon: "angle right",
},
content: {
content: <Container>{children}</Container>,
Expand All @@ -53,15 +52,40 @@ export class AccordionField extends Component {
];

const errorClass = hasError ? "error secondary" : "";
const [activeIndex, setActiveIndex] = useState(0);

const handleTitleClick = (e, { index }) => {
setActiveIndex(activeIndex === index ? -1 : index);
};

return (
<Accordion
defaultActiveIndex={active ? 0 : null}
panels={panels}
inverted
className={`invenio-accordion-field ${errorClass}`}
{...uiProps}
/>
>
{panels.map((panel, index) => (
<React.Fragment key={panel.key}>
<Accordion.Title
active={activeIndex === index}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default active index was removed, can we still set if the accordion is opened or closed on the page load ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we can by changing the state activeIndex, if you set it to -1, on reload the accordions are closed and on 0 they are all open

index={index}
onClick={handleTitleClick}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
0einstein0 marked this conversation as resolved.
Show resolved Hide resolved
handleTitleClick(e, { index });
}
}}
tabIndex={0}
>
{panel.title.content}
<Icon name="angle right" />
</Accordion.Title>
<Accordion.Content active={activeIndex === index}>
{panel.content.content}
</Accordion.Content>
</React.Fragment>
))}
</Accordion>
);
};

Expand Down
Loading