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

Fix bug of Accordion inside Form #790

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 12 additions & 6 deletions src/lib/components/accordion/Accordion.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,43 @@ const AccordionHeader = styled.button`
-moz-appearance: none;
appearance: none;
border: none;
gap: ${spacing.r8};
width: 100%;
cursor: pointer;
background-color: transparent;
color: ${(props) => props.theme.textPrimary};
padding: ${spacing.r4};
width: 100%;
padding: 0;
`;
const AccordionContainer = styled.div<{
isOpen: boolean;
}>`
overflow: hidden;
opacity: ${(props) => (props.isOpen ? 1 : 0)};
transition: height 0.3s ease-in, opacity 0.3s ease-in, visibility 0.3s;
transition:
height 0.3s ease-in,
opacity 0.3s ease-in,
visibility 0.3s;
visibility: ${(props) => (props.isOpen ? 'visible' : 'hidden')};
`;
const Wrapper = styled.div`
padding-block: ${spacing.r8};
padding: ${spacing.r8} 0 ${spacing.r8} ${spacing.r20};
`;

export const Accordion = ({ title, id, style, children }: AccordionProps) => {
const [isOpen, setIsOpen] = useState(false);

const handleToggleContent = () => {
const handleToggleContent = (
e:
| React.MouseEvent<HTMLButtonElement>
| React.KeyboardEvent<HTMLButtonElement>,
) => {
setIsOpen((prev) => !prev);
};

return (
<Box style={{ width: '100%', height: 'auto' }}>
<h3 style={{ margin: 0 }}>
<AccordionHeader
type="button"
id={`Accordion-header-${id}`}
onClick={handleToggleContent}
aria-controls={id}
Expand Down
7 changes: 7 additions & 0 deletions stories/Accordion/accordion.guideline.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import * as Stories from './accordion.stories';
Accordions are used to toggle the visibility of content.
It is used to hide non essential information or to reduce the amount of information displayed on the screen.

## Style

Accordion component comes with a style prop that can be used to customize the appearance of the accordion content.
The default style of the accordion container only includes padding.

<Canvas of={Stories.WithCustomStyle} />

## Playground

<Canvas of={Stories.Playground} layout="fullscreen" />
Expand Down
30 changes: 27 additions & 3 deletions stories/Accordion/accordion.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Meta, StoryObj } from '@storybook/react';
import React from 'react';

import { useTheme } from 'styled-components';
import {
Accordion,
AccordionProps,
} from '../../src/lib/components/accordion/Accordion.component';
import { Stack } from '../../src/lib/spacing';
import { Button } from '../../src/lib/components/buttonv2/Buttonv2.component';
import { spacing, Stack } from '../../src/lib/spacing';
import { margin } from 'styled-system';
import { Text } from '../../src/lib';

type AccordionStory = StoryObj<AccordionProps>;

Expand Down Expand Up @@ -59,7 +61,29 @@ export const Stacked: AccordionStory = {
<Stack direction="vertical" gap="r8">
<Accordion {...args} />
<Accordion {...args} />
<Accordion {...args} style={{ backgroundColor: 'grey' }} />
<Accordion {...args} />
</Stack>
),
};

export const WithCustomStyle: AccordionStory = {
render: (args) => {
const { title } = args;
const theme = useTheme();
console.log(theme);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this console.log

const style = {
backgroundColor: theme.statusHealthy,
borderRadius: spacing.r4,
padding: spacing.r16,
margin: spacing.r8,
};
return (
<Accordion {...args} style={style} title={title}>
<Text>The container of this accordion has a custom style</Text>
</Accordion>
);
},
args: {
title: 'Accordion with custom style',
},
};
179 changes: 179 additions & 0 deletions stories/form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Text } from '../src/lib/components/text/Text.component';
import { Toggle } from '../src/lib/components/toggle/Toggle.component';
import { Stack } from '../src/lib/spacing';
import { iconArgType } from './controls';
import { Accordion } from '../src/lib/next';

export default {
title: 'Templates/Form',
Expand Down Expand Up @@ -251,3 +252,181 @@ export const PageFormWithIcon = {
},
},
};

export const FormWithAccordion = {
render: ({ kind, title, subTitle, icon, requireMode }) => {
const layout = {
kind,
title,
subTitle,
icon,
};
const [toggle, setToggle] = useState(true);
return (
<>
<Form
layout={layout}
requireMode={requireMode}
rightActions={
<Stack gap={'r16'}>
<Button variant="outline" label="Cancel" />
<Button
variant="primary"
label="Save"
icon={<Icon name="Save" />}
/>
</Stack>
}
banner={
<Banner
variant="danger"
icon={<Icon name="Exclamation-circle" />}
title={'Error'}
>
There is an error
</Banner>
}
>
<FormSection
title={{
name: 'First part entity data',
helpTooltip: 'Tooltip of the first entity',
icon: 'Search',
}}
>
<FormGroup
direction="vertical"
label="Name"
id="name"
labelHelpTooltip="Name Tooltip"
content={<Input id="name" />}
help="Optional helper text"
required
disabled
></FormGroup>
<FormGroup
direction="horizontal"
label="Email"
id="email"
labelHelpTooltip="Email Tooltip"
content={<Input id="email" />}
error="Invalid email format. Try with a better format."
helpErrorPosition="right"
required
></FormGroup>
</FormSection>
<FormSection
title={{
name: 'Second part entity data',
helpTooltip: 'Tooltip of the Second entity',
icon: 'Search',
}}
>
<FormGroup
direction="horizontal"
label="Name"
id="name1"
labelHelpTooltip="Name Tooltip"
content={
<Toggle
onChange={() => {
setToggle(!toggle);
}}
toggle={toggle}
name="toggle"
/>
}
help="Optional helper text"
required={false}
></FormGroup>
<FormGroup
direction="horizontal"
label="Email"
id="email1"
labelHelpTooltip="Email Tooltip"
content={<Input id="email1" />}
error="Invalid email format. Try with a better format."
helpErrorPosition="right"
required={false}
></FormGroup>
<FormGroup
direction="horizontal"
label="Email long long long"
id="email-long1"
labelHelpTooltip="Email Tooltip"
content={<Input id="email-long1" />}
help="optional helper text"
helpErrorPosition="bottom"
required={false}
></FormGroup>
</FormSection>
<FormSection>
<Accordion title="Advanced Settings" id="advanced-settings">
<FormGroup
direction="vertical"
label="Object Lock Mode"
id="object_lock_mode"
labelHelpTooltip="S3 Object Lock Retention"
content={
<Stack direction="vertical">
<Stack direction="vertical">
<Stack>
<input
type="radio"
name="locktype"
id="locktype-governance"
value="governance"
/>
<label htmlFor="locktype-governance">Governance</label>
</Stack>
<Text isEmphazed color="textSecondary" variant="Smaller">
An user with a specific IAM permissions can
overwrite/delete protected object versions during the
retention period.
</Text>
</Stack>
<Stack>
<input
type="radio"
name="locktype"
id="locktype-compliance"
value="compliance"
/>
<label htmlFor="locktype-compliance">Compliance</label>
</Stack>
<Text isEmphazed color="textSecondary" variant="Smaller">
No one can overwrite protected object versions during the
retention period.
</Text>
</Stack>
}
required={true}
></FormGroup>
<FormGroup
id="value-example"
label="Choose a value"
helpErrorPosition="bottom"
required
content={
<Select
id="value-example"
placeholder="Select an option..."
onChange={() => {}}
value={'value-1'}
>
<Select.Option value={'value-1'}>Value 1</Select.Option>
<Select.Option value={'value-2'}>Value 2</Select.Option>
<Select.Option value={'value-3'}>Value 3</Select.Option>
</Select>
}
/>
</Accordion>
</FormSection>
</Form>
</>
);
},
args: {
requireMode: 'partial',
},
};
Loading