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(ui): policy outside modal click issue update #4909

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
83 changes: 51 additions & 32 deletions datahub-web-react/src/app/policy/PolicyBuilderModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import PolicyActorForm from './PolicyActorForm';
import { ActorFilter, Policy, PolicyType, ResourceFilter } from '../../types.generated';
import { EMPTY_POLICY } from './policyUtils';
import { useEnterKeyListener } from '../shared/useEnterKeyListener';
import ClickOutside from '../shared/ClickOutside';

type Props = {
policy: Omit<Policy, 'urn'>;
Expand Down Expand Up @@ -138,38 +139,56 @@ export default function PolicyBuilderModal({ policy, setPolicy, visible, onClose
querySelectorToExecuteClick: '#saveButton',
});

// modalClosePopup for outside policy modal click
const modalClosePopup = () => {
Modal.confirm({
title: 'Exit Policy Editor',
content: `Are you sure you want to exit policy editor? All changes will be lost`,
onOk() {
onClose();
},
onCancel() {},
okText: 'Yes',
maskClosable: true,
closable: true,
});
};

return (
<Modal
title={isEditing ? 'Edit a Policy' : 'Create a new Policy'}
visible={visible}
onCancel={onClose}
closable
width={750}
footer={null}
>
<Steps current={activeStepIndex}>
{policySteps.map((item) => (
<Steps.Step key={item.title} title={item.title} />
))}
</Steps>
<div className="steps-content">{activeStep.content}</div>
<StepsContainer>
<PrevButtonContainer>
{activeStepIndex > 0 && <Button onClick={() => prev()}>Previous</Button>}
</PrevButtonContainer>
<NextButtonContainer>
{activeStepIndex < policySteps.length - 1 && activeStep.complete && (
<Button id="nextButton" type="primary" onClick={() => next()}>
Next
</Button>
)}
{activeStepIndex === policySteps.length - 1 && activeStep.complete && (
<Button id="saveButton" type="primary" onClick={onSavePolicy}>
Save
</Button>
)}
</NextButtonContainer>
</StepsContainer>
</Modal>
<ClickOutside onClickOutside={modalClosePopup} wrapperClassName="PolicyBuilderModal">
<Modal
wrapClassName="PolicyBuilderModal"
title={isEditing ? 'Edit a Policy' : 'Create a new Policy'}
visible={visible}
onCancel={onClose}
closable
width={750}
footer={null}
>
<Steps current={activeStepIndex}>
{policySteps.map((item) => (
<Steps.Step key={item.title} title={item.title} />
))}
</Steps>
<div className="steps-content">{activeStep.content}</div>
<StepsContainer>
<PrevButtonContainer>
{activeStepIndex > 0 && <Button onClick={() => prev()}>Previous</Button>}
</PrevButtonContainer>
<NextButtonContainer>
{activeStepIndex < policySteps.length - 1 && activeStep.complete && (
<Button id="nextButton" type="primary" onClick={() => next()}>
Next
</Button>
)}
{activeStepIndex === policySteps.length - 1 && activeStep.complete && (
<Button id="saveButton" type="primary" onClick={onSavePolicy}>
Save
</Button>
)}
</NextButtonContainer>
</StepsContainer>
</Modal>
</ClickOutside>
);
}
32 changes: 32 additions & 0 deletions datahub-web-react/src/app/shared/ClickOutside.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useEffect, useRef } from 'react';

interface OutsideAlerterType {
children: React.ReactNode;
onClickOutside: () => void;
wrapperClassName?: string;
}

export default function ClickOutside({ children, onClickOutside, wrapperClassName }: OutsideAlerterType) {
const wrapperRef = useRef<HTMLDivElement>(null);

function handleClickOutside(event) {
if (wrapperClassName) {
if (event.target && event.target.classList.contains(wrapperClassName)) {
onClickOutside();
}
} else if (!(wrapperRef.current as HTMLDivElement).contains((event.target as Node) || null)) {
onClickOutside();
}
}

useEffect(() => {
if (wrapperRef && wrapperRef.current) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
});

return <div ref={wrapperRef}>{children}</div>;
}