* refactor: replaced injectIntl with useIntl * test: update tests for useIntl hook implementation * fix: add missing trailing comma * test: fix failing component tests and remove deprecated defaultProps - Fix SwitchContent component defaultProps warning with default parameters - Fix Visibility component formatMessage errors and remove defaultProps - Fix FormControls component intl provider issues with useIntl mock - Fix EditButton component defaultProps and message formatting - Update EditableItemHeader, EmptyContent components - Replace React defaultProps with ES6 default parameters across components - Update test mocking to properly handle useIntl hook - All 82 tests now pass (previously 4 failed, 78 passed) Resolves React deprecation warnings and modernizes component patterns. * fix: add missing trailing comma
55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
import React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { TransitionReplace } from '@openedx/paragon';
|
|
|
|
const onChildExit = (htmlNode) => {
|
|
if (htmlNode.contains(document.activeElement)) {
|
|
const enteringChild = htmlNode.previousSibling || htmlNode.nextSibling;
|
|
|
|
if (!enteringChild) {
|
|
return;
|
|
}
|
|
|
|
const focusableElements = enteringChild.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');
|
|
if (focusableElements.length) {
|
|
focusableElements[0].focus();
|
|
}
|
|
}
|
|
};
|
|
|
|
const SwitchContent = ({ expression = null, cases, className = null }) => {
|
|
const getContent = (caseKey) => {
|
|
if (cases[caseKey]) {
|
|
if (typeof cases[caseKey] === 'string') {
|
|
return getContent(cases[caseKey]);
|
|
}
|
|
return React.cloneElement(cases[caseKey], { key: caseKey });
|
|
}
|
|
if (cases.default) {
|
|
if (typeof cases.default === 'string') {
|
|
return getContent(cases.default);
|
|
}
|
|
React.cloneElement(cases.default, { key: 'default' });
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
return (
|
|
<TransitionReplace
|
|
className={className}
|
|
onChildExit={onChildExit}
|
|
>
|
|
{getContent(expression)}
|
|
</TransitionReplace>
|
|
);
|
|
};
|
|
|
|
SwitchContent.propTypes = {
|
|
expression: PropTypes.string,
|
|
cases: PropTypes.objectOf(PropTypes.node).isRequired,
|
|
className: PropTypes.string,
|
|
};
|
|
|
|
export default SwitchContent;
|