| 1 |
/** |
| 2 |
* SettingsCard Component |
| 3 |
* |
| 4 |
* Standardized settings card component extracted from Site Identity patterns. |
| 5 |
* Provides consistent card structure with integrated optimization controls, |
| 6 |
* toggle functionality, and notice display across all Essential SEO tabs. |
| 7 |
* |
| 8 |
* @package ThinkRank |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
import { Card, CardHeader, CardBody, Flex, FlexItem, ToggleControl, Notice } from '@wordpress/components'; |
| 13 |
import { __ } from '@wordpress/i18n'; |
| 14 |
|
| 15 |
/** |
| 16 |
* SettingsCard Component |
| 17 |
* |
| 18 |
* @param {Object} props Component props |
| 19 |
* @param {string} props.title Card title displayed in header |
| 20 |
* @param {boolean} props.enabled Enable/disable state for the feature |
| 21 |
* @param {Function} props.onToggle Toggle handler function |
| 22 |
* @param {string} props.toggleLabel Custom toggle label (overrides default) |
| 23 |
* @param {Array} props.optimizationButtons Array of optimization button components |
| 24 |
* @param {Object} props.notice Notice object with status and message |
| 25 |
* @param {Function} props.onNoticeRemove Notice removal handler |
| 26 |
* @param {React.ReactNode} props.children Card content |
| 27 |
* @param {string} props.className Additional CSS classes |
| 28 |
* @param {string} props.size Card size: 'small', 'medium', 'large' |
| 29 |
* @param {boolean} props.showDisabledWarning Show warning when disabled |
| 30 |
* @param {string} props.disabledWarningMessage Custom disabled warning message |
| 31 |
*/ |
| 32 |
const SettingsCard = ({ |
| 33 |
title, |
| 34 |
enabled = true, |
| 35 |
onToggle, |
| 36 |
toggleLabel, |
| 37 |
optimizationButtons = [], |
| 38 |
notice, |
| 39 |
onNoticeRemove, |
| 40 |
children, |
| 41 |
className = '', |
| 42 |
size = 'medium', |
| 43 |
showDisabledWarning = true, |
| 44 |
disabledWarningMessage, |
| 45 |
...rest |
| 46 |
}) => { |
| 47 |
/** |
| 48 |
* Handle notice dismissal |
| 49 |
* Following Site Identity notice management pattern |
| 50 |
*/ |
| 51 |
const handleNoticeRemove = () => { |
| 52 |
if (onNoticeRemove) { |
| 53 |
onNoticeRemove(); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
/** |
| 58 |
* Get default disabled warning message |
| 59 |
*/ |
| 60 |
const getDisabledWarningMessage = () => { |
| 61 |
if (disabledWarningMessage) { |
| 62 |
return disabledWarningMessage; |
| 63 |
} |
| 64 |
return __('This feature is disabled. Enable it to access configuration options.', 'thinkrank'); |
| 65 |
}; |
| 66 |
|
| 67 |
// Combine CSS classes |
| 68 |
const cardClasses = [ |
| 69 |
'thinkrank-settings-card', |
| 70 |
`thinkrank-settings-card--${size}`, |
| 71 |
className |
| 72 |
].filter(Boolean).join(' '); |
| 73 |
|
| 74 |
return ( |
| 75 |
<Card className={cardClasses} size={size} {...rest}> |
| 76 |
<CardHeader> |
| 77 |
<Flex justify="space-between" align="center"> |
| 78 |
<FlexItem> |
| 79 |
<h2>{title}</h2> |
| 80 |
</FlexItem> |
| 81 |
<FlexItem> |
| 82 |
<Flex gap={2}> |
| 83 |
{/* Render optimization buttons */} |
| 84 |
{optimizationButtons.map((button, index) => ( |
| 85 |
<div key={index}>{button}</div> |
| 86 |
))} |
| 87 |
|
| 88 |
{/* Render toggle control if onToggle is provided */} |
| 89 |
{onToggle && ( |
| 90 |
<ToggleControl |
| 91 |
label={toggleLabel || __('Enable', 'thinkrank')} |
| 92 |
checked={enabled} |
| 93 |
onChange={onToggle} |
| 94 |
__nextHasNoMarginBottom={true} |
| 95 |
/> |
| 96 |
)} |
| 97 |
</Flex> |
| 98 |
</FlexItem> |
| 99 |
</Flex> |
| 100 |
</CardHeader> |
| 101 |
<CardBody> |
| 102 |
{/* Show disabled warning if feature is disabled */} |
| 103 |
{!enabled && showDisabledWarning && ( |
| 104 |
<Notice status="warning" isDismissible={false}> |
| 105 |
{getDisabledWarningMessage()} |
| 106 |
</Notice> |
| 107 |
)} |
| 108 |
|
| 109 |
{/* Show notice if provided */} |
| 110 |
{notice && ( |
| 111 |
<Notice |
| 112 |
status={notice.status} |
| 113 |
isDismissible={true} |
| 114 |
onRemove={handleNoticeRemove} |
| 115 |
className="thinkrank-mb-md" |
| 116 |
> |
| 117 |
{notice.message} |
| 118 |
</Notice> |
| 119 |
)} |
| 120 |
|
| 121 |
{/* Card content */} |
| 122 |
{children} |
| 123 |
</CardBody> |
| 124 |
</Card> |
| 125 |
); |
| 126 |
}; |
| 127 |
|
| 128 |
export default SettingsCard; |
| 129 |
|