Skip to content

feat: introduce AlertButtonWithConfig with variant from config #789

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions env.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Also note that in an actual application this file would be added to .gitignore.
const config = {
JS_FILE_VAR: 'JS_FILE_VAR_VALUE_FOR_EXAMPLE_APP',
styleOverrides: {
alertButtonVariant: 'brand',
},
};

export default config;
13 changes: 12 additions & 1 deletion example/ExamplePage.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useContext, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { Alert } from '@openedx/paragon';

/* eslint-disable import/no-extraneous-dependencies */
import { injectIntl, useIntl } from '@edx/frontend-platform/i18n';
import { logInfo } from '@edx/frontend-platform/logging';
import { AppContext } from '@edx/frontend-platform/react';
import { AppContext, DefaultAlertButton } from '@edx/frontend-platform/react';
import { ensureConfig, mergeConfig, getConfig } from '@edx/frontend-platform';
/* eslint-enable import/no-extraneous-dependencies */
import messages from './messages';
Expand Down Expand Up @@ -51,6 +52,16 @@ function ExamplePage() {
<p>JS_FILE_VAR var came through: <strong>{getConfig().JS_FILE_VAR}</strong></p>
<p>Visit <Link to="/authenticated">authenticated page</Link>.</p>
<p>Visit <Link to="/error_example">error page</Link>.</p>
<div>
<Alert
variant="error"
actions={[
<DefaultAlertButton>Hello</DefaultAlertButton>,
]}
>
Lorem ispum dolar sit amet.
</Alert>
</div>
</div>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* @module React
*/

export { useAppEvent } from './hooks';
export { default as AppContext } from './AppContext';
export { default as AppProvider } from './AppProvider';
export { default as AuthenticatedPageRoute } from './AuthenticatedPageRoute';
export { default as ErrorBoundary } from './ErrorBoundary';
export { default as ErrorPage } from './ErrorPage';
export { default as LoginRedirect } from './LoginRedirect';
export { default as PageWrap } from './PageWrap';
export { useAppEvent } from './hooks';
export { default as DefaultAlertButton } from './paragon/DefaultAlertButton';
47 changes: 47 additions & 0 deletions src/react/paragon/DefaultAlertButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { forwardRef } from 'react';
import PropTypes from 'prop-types';
import { Button } from '@openedx/paragon';
import { getConfig } from '../../config';

function useAlertButtonVariant(props) {

Check warning on line 6 in src/react/paragon/DefaultAlertButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/react/paragon/DefaultAlertButton.jsx#L6

Added line #L6 was not covered by tests
if (props.variant) {
// If a variant is explicitly set in the props, use that.
return props.variant;

Check warning on line 9 in src/react/paragon/DefaultAlertButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/react/paragon/DefaultAlertButton.jsx#L9

Added line #L9 was not covered by tests
}
// Otherwise, check for a configured style override for the alert button variant.
const { styleOverrides } = getConfig();

Check warning on line 12 in src/react/paragon/DefaultAlertButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/react/paragon/DefaultAlertButton.jsx#L12

Added line #L12 was not covered by tests
if (styleOverrides?.alertButtonVariant) {
return styleOverrides.alertButtonVariant;

Check warning on line 14 in src/react/paragon/DefaultAlertButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/react/paragon/DefaultAlertButton.jsx#L14

Added line #L14 was not covered by tests
}

// If no variant is set in props and no style override is configured, use Paragon's default variant.
return undefined;

Check warning on line 18 in src/react/paragon/DefaultAlertButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/react/paragon/DefaultAlertButton.jsx#L18

Added line #L18 was not covered by tests
}

/**
* A default alert button component that utilizes a button variant override, if configured. Otherwise,
* it defaults to the ``primary`` button variant. By creating this wrapper component, consuming projects
* can more readily import and use a consistent alert button style across their application without having
* to re-integrate the button variant logic across every usage.
* @memberof module:React
* @param {Object} props
*/
const DefaultAlertButton = forwardRef(({ children, ...props }, ref) => {
const alertButtonVariant = useAlertButtonVariant(props);
return (

Check warning on line 31 in src/react/paragon/DefaultAlertButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/react/paragon/DefaultAlertButton.jsx#L29-L31

Added lines #L29 - L31 were not covered by tests
<Button
variant={alertButtonVariant}
ref={ref}
{...props}
>
{children}
</Button>
);
});
DefaultAlertButton.displayName = 'DefaultAlertButton';

Check warning on line 41 in src/react/paragon/DefaultAlertButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/react/paragon/DefaultAlertButton.jsx#L41

Added line #L41 was not covered by tests

DefaultAlertButton.propTypes = {

Check warning on line 43 in src/react/paragon/DefaultAlertButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/react/paragon/DefaultAlertButton.jsx#L43

Added line #L43 was not covered by tests
children: PropTypes.node.isRequired,
};

export default DefaultAlertButton;
1 change: 1 addition & 0 deletions src/react/paragon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as DefaultAlertButton } from './DefaultAlertButton';