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: issue with the prompt component #897

Merged
merged 2 commits into from
Jan 23, 2025
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
4 changes: 2 additions & 2 deletions src/custom/Prompt/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import PromptComponent from './promt-component';

import PromptComponent, { PROMPT_VARIANTS } from './promt-component';
export { PROMPT_VARIANTS, PromptComponent };
export default PromptComponent;
46 changes: 40 additions & 6 deletions src/custom/Prompt/promt-component.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { Typography } from '../../base';
import { Checkbox, FormControlLabel, Typography } from '../../base';
import { useTheme } from '../../theme';
import { Modal, ModalBody, ModalButtonPrimary, ModalButtonSecondary, ModalFooter } from '../Modal';
import { ActionComponent, Subtitle } from './style';
Expand All @@ -26,6 +26,8 @@ interface State {
showInfoIcon?: string;
variant?: PromptVariant;
headerIcon?: React.ReactNode;
showCheckbox?: boolean;
isChecked?: boolean;
}

interface ShowParams {
Expand All @@ -34,11 +36,13 @@ interface ShowParams {
primaryOption: string;
variant: PromptVariant;
showInfoIcon?: string;
showCheckbox?: boolean;
headerIcon?: React.ReactNode;
}

export interface PromptRef {
show: (params: ShowParams) => Promise<string>;
getCheckboxState: () => boolean;
}

const PromptComponent = forwardRef<PromptRef, PromptProps>(({ variant }, ref) => {
Expand All @@ -49,7 +53,9 @@ const PromptComponent = forwardRef<PromptRef, PromptProps>(({ variant }, ref) =>
primaryOption: '',
showInfoIcon: '',
variant,
headerIcon: undefined
headerIcon: undefined,
isChecked: false,
showCheckbox: false
});

/* This ref is used to store the resolve and reject functions of the promise returned by the show method */
Expand All @@ -67,7 +73,8 @@ const PromptComponent = forwardRef<PromptRef, PromptProps>(({ variant }, ref) =>
setState({
...params,
isOpen: true,
showInfoIcon: params.showInfoIcon || ''
showInfoIcon: params.showInfoIcon || '',
showCheckbox: !!params.showCheckbox
});
});
};
Expand All @@ -77,11 +84,20 @@ const PromptComponent = forwardRef<PromptRef, PromptProps>(({ variant }, ref) =>
setState((prevState) => ({ ...prevState, isOpen: false }));
};

const handleCheckboxChange = () => {
setState((prevState) => ({ ...prevState, isChecked: !prevState.isChecked }));
};

const getCheckboxState = () => {
return !!state.isChecked;
};

useImperativeHandle(ref, () => ({
show
show,
getCheckboxState
}));

const { isOpen, primaryOption, title, subtitle, showInfoIcon, headerIcon } = state;
const { isOpen, primaryOption, title, subtitle, showInfoIcon, headerIcon, showCheckbox } = state;
const { resolve } = promiseInfoRef.current;

return (
Expand All @@ -96,10 +112,28 @@ const PromptComponent = forwardRef<PromptRef, PromptProps>(({ variant }, ref) =>
{subtitle && (
<ModalBody>
<Subtitle id="alert-dialog-description" variant="body1" component="div">
<Typography variant="body1" component="div">
<Typography
variant="body1"
component="div"
style={{
color: theme.palette.text.primary
}}
>
{subtitle}
</Typography>
</Subtitle>
{showCheckbox && (
<FormControlLabel
control={
<Checkbox
checked={getCheckboxState()}
onChange={handleCheckboxChange}
color="primary"
/>
}
label={<span style={{ fontSize: '1rem' }}>Do not show again</span>}
/>
)}
</ModalBody>
)}
<ModalFooter variant="filled" helpText={showInfoIcon}>
Expand Down
3 changes: 2 additions & 1 deletion src/custom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { LearningCard } from './LearningCard';
import { BasicMarkdown, RenderMarkdown } from './Markdown';
import { ModalCard } from './ModalCard';
import PopperListener, { IPopperListener } from './PopperListener';
import PromptComponent from './Prompt';
import { PROMPT_VARIANTS, PromptComponent } from './Prompt';
import ResponsiveDataTable, {
DataTableEllipsisMenu,
ResponsiveDataTableProps
Expand Down Expand Up @@ -90,6 +90,7 @@ export {
InfoTooltip,
LearningCard,
ModalCard,
PROMPT_VARIANTS,
PopperListener,
PromptComponent,
ResponsiveDataTable,
Expand Down
Loading