Skip to content

Commit 0cb2487

Browse files
author
Rohit Bhati
committed
Fix focus issue of buttons. #6513
1 parent 9cd8492 commit 0cb2487

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

web/pgadmin/browser/server_groups/servers/databases/static/js/database.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ define('pgadmin.node.database', [
257257
t = pgBrowser.tree;
258258

259259
if (children) {
260-
pgAdmin.Browser.notifier.confirm(
260+
pgAdmin.Browser.notifier.confirmDelete(
261261
gettext('Disconnect from all databases'),
262262
gettext('Are you sure you want to disconnect from all databases?'),
263263
function() {
@@ -557,7 +557,7 @@ define('pgadmin.node.database', [
557557
});
558558
};
559559
if (notify) {
560-
pgAdmin.Browser.notifier.confirm(
560+
pgAdmin.Browser.notifier.confirmDelete(
561561
gettext('Disconnect from database'),
562562
gettext('Are you sure you want to disconnect from the database - <b>%s</b>?', d.label),
563563
function() {

web/pgadmin/browser/server_groups/servers/static/js/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ define('pgadmin.node.server', [
243243
obj = this,
244244
t = pgBrowser.tree;
245245
if (children) {
246-
pgAdmin.Browser.notifier.confirm(
246+
pgAdmin.Browser.notifier.confirmDelete(
247247
gettext('Disconnect from all servers'),
248248
gettext('Are you sure you want to disconnect from all servers?'),
249249
function() {
@@ -862,7 +862,7 @@ define('pgadmin.node.server', [
862862
};
863863

864864
if (notify) {
865-
pgAdmin.Browser.notifier.confirm(
865+
pgAdmin.Browser.notifier.confirmDelete(
866866
gettext('Disconnect from server'),
867867
gettext('Are you sure you want to disconnect from the server <b>%s</b>?', label),
868868
function() { disconnect(); },

web/pgadmin/static/js/helpers/ModalProvider.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import PropTypes from 'prop-types';
1818
import gettext from 'sources/gettext';
1919
import HTMLReactParser from 'html-react-parser';
2020
import CheckRoundedIcon from '@mui/icons-material/CheckRounded';
21+
import _ from 'lodash';
2122
import { Rnd } from 'react-rnd';
2223
import { ExpandDialogIcon, MinimizeDialogIcon } from '../components/ExternalIcon';
2324
import { styled } from '@mui/material/styles';
@@ -44,7 +45,7 @@ export function useModal() {
4445
function renderExtraButtons(button) {
4546
switch(button.type) {
4647
case 'primary':
47-
return <PrimaryButton className='Alert-margin' startIcon={button.icon} onClick={button.onClick}>{button.label}</PrimaryButton>;
48+
return <PrimaryButton className='Alert-margin' startIcon={button.icon} onClick={button.onClick} autoFocus>{button.label}</PrimaryButton>;
4849
case 'default':
4950
return <DefaultButton className='Alert-margin' startIcon={button.icon} onClick={button.onClick} color={button?.color}>{button.label}</DefaultButton>;
5051
default:
@@ -53,18 +54,20 @@ function renderExtraButtons(button) {
5354
}
5455

5556
function AlertContent({ text, confirm, okLabel = gettext('OK'), cancelLabel = gettext('Cancel'), onOkClick, onCancelClick, extraButtons }) {
57+
// Check if any extra buttons have an focus property to determine if autofocus should be applied.
58+
const hasAutoFocus = _.some(extraButtons, item => !_.isNil(item.focus));
5659
return (
5760
<StyledBox display="flex" flexDirection="column" height="100%">
5861
<Box flexGrow="1" p={2}>{typeof (text) == 'string' ? HTMLReactParser(text) : text}</Box>
5962
<Box className='Alert-footer'>
6063
{confirm &&
61-
<DefaultButton startIcon={<CloseIcon />} onClick={onCancelClick} autoFocus={true}>{cancelLabel}</DefaultButton>
64+
<DefaultButton startIcon={<CloseIcon />} onClick={onCancelClick} autoFocus={hasAutoFocus} >{cancelLabel}</DefaultButton>
6265
}
6366
{
6467
extraButtons?.length ?
6568
extraButtons.map(button=>renderExtraButtons(button))
6669
:
67-
<PrimaryButton className='Alert-margin' startIcon={<CheckRoundedIcon />} onClick={onOkClick}>{okLabel}</PrimaryButton>
70+
<PrimaryButton className='Alert-margin' startIcon={<CheckRoundedIcon/>} onClick={onOkClick} autoFocus={true}>{okLabel}</PrimaryButton>
6871
}
6972
</Box>
7073
</StyledBox>

web/pgadmin/static/js/helpers/Notifier.jsx

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SnackbarProvider, SnackbarContent } from 'notistack';
1111
import { styled } from '@mui/material/styles';
1212
import {Box} from '@mui/material';
1313
import CloseIcon from '@mui/icons-material/CloseRounded';
14+
import DeleteIcon from '@mui/icons-material/Delete';
1415
import { DefaultButton, PrimaryButton } from '../components/Buttons';
1516
import HTMLReactParser from 'html-react-parser';
1617
import CheckRoundedIcon from '@mui/icons-material/CheckRounded';
@@ -22,6 +23,7 @@ import gettext from 'sources/gettext';
2223
import _ from 'lodash';
2324
import { useModal } from './ModalProvider';
2425
import { parseApiError } from '../api_instance';
26+
import { DisonnectedIcon } from '../components/ExternalIcon';
2527

2628

2729
const Root = styled('div')(({theme}) => ({
@@ -68,6 +70,20 @@ AlertContent.propTypes = {
6870
cancelLabel: PropTypes.string,
6971
};
7072

73+
function createButtonConfig(icon, label, onOkClick, closeModal, type, color=null, focus=false){
74+
// Creates a configuration object for a button.
75+
return {
76+
icon,
77+
label,
78+
onClick: () => {
79+
onOkClick();
80+
closeModal();
81+
}, type,
82+
color,
83+
focus
84+
};
85+
}
86+
7187
// This can be called from iframe,
7288
// so need to separate the context to avoid hooks error
7389
class SnackbarNotifier {
@@ -182,20 +198,16 @@ class Notifier {
182198
}
183199

184200
confirmDelete(title, text, onOkClick, onCancelClick,okLabel = gettext('Yes'), cancelLabel = gettext('No')){
185-
186-
const extraButtons = (closeModal) => {
187-
return [
188-
{
189-
type: 'default',
190-
icon: <CheckRoundedIcon />,
191-
label: okLabel,
192-
onClick: ()=>{
193-
onOkClick();
194-
closeModal();
195-
},
196-
color: 'error',
197-
},
198-
];
201+
const extraButtons = (closeModal) => {
202+
// Button configurations for specific actions like "Delete" and "Disconnect".
203+
const buttonConfigs = {
204+
[gettext('Delete')]: () =>
205+
createButtonConfig(<DeleteIcon />, okLabel, onOkClick, closeModal,'default','error', true),
206+
[gettext('Disconnect')]: () =>
207+
createButtonConfig(<DisonnectedIcon />, okLabel, onOkClick, closeModal, 'primary'),
208+
};
209+
// Return the button configuration for the specified `okLabel`, if it exists.
210+
return buttonConfigs[okLabel] ? [buttonConfigs[okLabel]()] : [];
199211
};
200212
this.modal.confirm(title, text, onOkClick, onCancelClick, okLabel, cancelLabel, extraButtons);
201213
}

0 commit comments

Comments
 (0)