Skip to content

Commit

Permalink
ui: added toggle and keyboard focus for accordion
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatimah committed Sep 29, 2023
1 parent 630ebd2 commit 91004dc
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 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 @@ -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 = [

Check failure on line 42 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Delete `··`

Check failure on line 42 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Delete `··`
{

Check failure on line 43 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Delete `··`

Check failure on line 43 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Delete `··`
key: `panel-${label}`,

Check failure on line 44 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Replace `··········` with `········`

Check failure on line 44 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Replace `··········` with `········`
title: {

Check failure on line 45 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Delete `··`

Check failure on line 45 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Delete `··`
content: label,

Check failure on line 46 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Replace `············` with `··········`

Check failure on line 46 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Replace `············` with `··········`
},

Check failure on line 47 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Delete `··`

Check failure on line 47 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Delete `··`
content: {

Check failure on line 48 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Replace `··········` with `········`

Check failure on line 48 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Replace `··········` with `········`
content: <Container>{children}</Container>,

Check failure on line 49 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Delete `··`

Check failure on line 49 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Delete `··`
},

Check failure on line 50 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Replace `··········` with `········`

Check failure on line 50 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Replace `··········` with `········`
},

Check failure on line 51 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (18.x)

Delete `··`

Check failure on line 51 in src/lib/forms/AccordionField.js

View workflow job for this annotation

GitHub Actions / Tests (16.x)

Delete `··`
content: {
content: <Container>{children}</Container>,
},
},
];
];

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}
index={index}
onClick={handleTitleClick}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
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

0 comments on commit 91004dc

Please sign in to comment.