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

Modal btn layout #1570

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
ActionConfirmationModalContextProvider,
ActionConfirmationModalContext,
ActionConfirmationButtonOption,
} from '@kaoto/kaoto/testing';
import { Meta, StoryFn } from '@storybook/react';
import { FunctionComponent, useContext, useState } from 'react';
import { ButtonVariant } from '@patternfly/react-core';

export default {
title: 'Modal/ActionConfirmationModal',
component: ActionConfirmationModalContextProvider,
} as Meta<typeof ActionConfirmationModalContextProvider>;

type TestComponentProps = {
title: string;
btnTitle?: string;
text?: string;
additionalModalText?: string;
buttonOptions?: Record<string, ActionConfirmationButtonOption>;
};

const TestComponent: FunctionComponent<TestComponentProps> = (props) => {
const [confirmationResult, setConfirmationResult] = useState<string>('');
const { actionConfirmation: deleteConfirmation } = useContext(ActionConfirmationModalContext)!;
const handleDelete = async () => {
const res = await deleteConfirmation(props);
setConfirmationResult(res);
};

return (
<p>
<button onClick={handleDelete}>{props.btnTitle ?? props.title}</button>
<br />
<h4>{confirmationResult}</h4>
</p>
);
};

const Template: StoryFn<typeof ActionConfirmationModalContextProvider> = (_args) => {
return (
<ActionConfirmationModalContextProvider>
<TestComponent title="Confirm / Cancel" />
<TestComponent
title="Custom Title"
btnTitle="Delete step & file / Delete step only / Cancel"
text="Modal text: Delete step & file, Delete step only or Cancel."
additionalModalText="Additional text for 3 options modal text"
buttonOptions={{
'del-step-and-file': {
buttonText: 'Delete both step and file',
variant: ButtonVariant.danger,
},
'del-step-only': {
buttonText: 'Delete the step, but keep the file',
variant: ButtonVariant.secondary,
isDanger: true,
},
}}
/>
</ActionConfirmationModalContextProvider>
);
};

export const ActionConfirmationModal = Template.bind({});
ActionConfirmationModal.args = {};
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,40 @@ exports[`ActionConfirmationModalProvider should allow consumers to update the mo
<footer
class="pf-v5-c-modal-box__footer"
>
<button
aria-disabled="false"
class="pf-v5-c-button pf-m-danger"
data-ouia-component-id="OUIA-Generated-Button-danger-3"
data-ouia-component-type="PF5/Button"
data-ouia-safe="true"
data-testid="action-confirmation-modal-btn-confirm"
type="button"
<div
class="pf-v5-l-split pf-m-gutter pf-m-wrap"
>
Confirm
</button>
<button
aria-disabled="false"
class="pf-v5-c-button pf-m-link"
data-ouia-component-id="OUIA-Generated-Button-link-3"
data-ouia-component-type="PF5/Button"
data-ouia-safe="true"
data-testid="action-confirmation-modal-btn-cancel"
type="button"
>
Cancel
</button>
<div
class="pf-v5-l-split__item"
>
<button
aria-disabled="false"
class="pf-v5-c-button pf-m-danger"
data-ouia-component-id="OUIA-Generated-Button-danger-3"
data-ouia-component-type="PF5/Button"
data-ouia-safe="true"
data-testid="action-confirmation-modal-btn-confirm"
type="button"
>
Confirm
</button>
</div>
<div
class="pf-v5-l-split__item"
>
<button
aria-disabled="false"
class="pf-v5-c-button pf-m-link"
data-ouia-component-id="OUIA-Generated-Button-link-3"
data-ouia-component-type="PF5/Button"
data-ouia-safe="true"
data-testid="action-confirmation-modal-btn-cancel"
type="button"
>
Cancel
</button>
</div>
</div>
</footer>
</div>
`;
52 changes: 30 additions & 22 deletions packages/ui/src/providers/action-confirmation-modal.provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, ButtonVariant, Modal, ModalVariant } from '@patternfly/react-core';
import { Button, ButtonVariant, Modal, ModalVariant, Split, SplitItem } from '@patternfly/react-core';
import { FunctionComponent, PropsWithChildren, createContext, useCallback, useMemo, useRef, useState } from 'react';

export const ACTION_ID_CANCEL = 'cancel';
Expand Down Expand Up @@ -81,6 +81,34 @@ export const ActionConfirmationModalContextProvider: FunctionComponent<PropsWith
[actionConfirmation],
);

const footer = (
<Split hasGutter isWrappable>
{...Object.entries(buttonOptions).map(([actionId, option]) => (
<SplitItem key={actionId}>
<Button
key={actionId}
variant={option.variant}
onClick={() => handleAction(actionId)}
data-testid={`action-confirmation-modal-btn-${actionId}`}
isDanger={option.isDanger}
>
{option.buttonText}
</Button>
</SplitItem>
))}
<SplitItem key="cancel">
<Button
key="cancel"
variant="link"
onClick={handleCloseModal}
data-testid={`action-confirmation-modal-btn-${ACTION_ID_CANCEL}`}
>
Cancel
</Button>
</SplitItem>
</Split>
);

return (
<ActionConfirmationModalContext.Provider value={value}>
{props.children}
Expand All @@ -93,27 +121,7 @@ export const ActionConfirmationModalContextProvider: FunctionComponent<PropsWith
titleIconVariant={'warning'}
onClose={handleCloseModal}
ouiaId="ActionConfirmationModal"
actions={[
...Object.entries(buttonOptions).map(([actionId, option]) => (
<Button
key={actionId}
variant={option.variant}
onClick={() => handleAction(actionId)}
data-testid={`action-confirmation-modal-btn-${actionId}`}
isDanger={option.isDanger}
>
{option.buttonText}
</Button>
)),
<Button
key="cancel"
variant="link"
onClick={handleCloseModal}
data-testid={`action-confirmation-modal-btn-${ACTION_ID_CANCEL}`}
>
Cancel
</Button>,
]}
footer={footer}
>
{textParagraphs.length === 1
? textParagraphs[0]
Expand Down
Loading