Skip to content

Commit 9a48125

Browse files
author
Mahesh Kothur
committed
feat(component): refactored the CTA and updated the README
1 parent 66e1f79 commit 9a48125

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

packages/react/src/components/Cta/CTA.js

+27-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { FeaturedLink } from '../../patterns/blocks/FeaturedLink';
1111
import { LinkWithIcon } from '../LinkWithIcon';
1212
import PropTypes from 'prop-types';
1313
import React from 'react';
14-
import { smoothScrolling } from '@carbon/ibmdotcom-utilities';
14+
import { smoothScroll } from '@carbon/ibmdotcom-utilities';
1515
/**
1616
* CTA component
1717
*
@@ -28,14 +28,16 @@ const CTA = ({ style, type, ...cta }) => {
2828
<CardLink
2929
{...cta}
3030
icon={iconSelector(type)}
31-
onClick={type === 'jump' ? smoothScrolling : null}
31+
target={external(type)}
32+
onClick={e => jump(e, type)}
3233
/>
3334
);
3435
case 'button':
3536
return (
3637
<ButtonGroup
3738
buttons={renderButtons(cta)}
38-
onClick={type === 'jump' ? smoothScrolling : null}
39+
target={external(type)}
40+
onClick={e => jump(e, type)}
3941
/>
4042
);
4143
case 'feature':
@@ -44,22 +46,39 @@ const CTA = ({ style, type, ...cta }) => {
4446
return (
4547
<LinkWithIcon
4648
href={cta.href}
47-
target={type === 'external' ? '_blank' : null}
48-
onClick={type === 'jump' ? smoothScrolling : null}>
49+
target={external(type)}
50+
onClick={e => jump(e, type)}>
4951
{cta.copy}
5052
{iconSelector(type)}
5153
</LinkWithIcon>
5254
);
5355
}
5456
};
57+
58+
/**
59+
* jump to target element onClick
60+
* @param {*} e event object
61+
* @param {*} type cta type ( external | jump | local)
62+
* * @returns {*} behaviour object
63+
*/
64+
const jump = (e, type) => (type === 'jump' ? smoothScroll(e) : null);
65+
66+
/**
67+
* sets target
68+
* @param {string} type cta type ( external | jump | local)
69+
* @returns {string} target value
70+
*/
71+
const external = type => (type === 'external' ? '_blank' : null);
72+
5573
/**
5674
* TEMPORARY sets icon based on link type
5775
*
5876
* @param {string} type cta type ( external | jump | local)
59-
* @returns {*} cta type component
77+
* @returns {*} type of icon component
6078
*/
6179
const TEMP_iconSelector = type =>
6280
type === 'external' ? Launch20 : type === 'jump' ? ArrowDown20 : ArrowRight20;
81+
6382
/**
6483
* sets icon based on link type
6584
*
@@ -74,6 +93,7 @@ const iconSelector = type =>
7493
) : (
7594
<ArrowRight20 />
7695
);
96+
7797
/**
7898
* sets button
7999
*
@@ -85,8 +105,8 @@ const renderButtons = ({ buttons }) =>
85105
button.renderIcon = TEMP_iconSelector(button.type);
86106
return button;
87107
});
108+
88109
CTA.propTypes = {
89-
cta: PropTypes.object,
90110
style: PropTypes.string,
91111
type: PropTypes.string,
92112
};

packages/react/src/components/Cta/README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ Here's a quick example to get you started.
2020
```javascript
2121
import React from 'react';
2222
import ReactDOM from 'react-dom';
23+
import { CTA } from '@carbon/ibmdotcom-react';
2324
import 'yourapplication.scss';
2425

2526
function App() {
26-
return <CTA style={style} type={type} {...cta} />;
27+
return (
28+
<CTA style="text" type="local" copy="IBM Homepage" href="www.ibm.com" />
29+
);
2730
}
2831

2932
ReactDOM.render(<App />, document.querySelector('#app'));
@@ -34,11 +37,19 @@ ReactDOM.render(<App />, document.querySelector('#app'));
3437
3538
## Props
3639

37-
| Name | Required | Data Type | Default Value | Description |
38-
| ------- | -------- | --------- | ------------- | ---------------------------------------------- |
39-
| `style` | NO | String | text | Deafult style is text (LinkWithIcon component) |
40-
| `cta` | NO | Object | null | Contains type, href,copy title, heading, image |
41-
| `type` | NO | String | local | Contains type, href,copy title, heading, image |
40+
| Name | Required | Data Type | Default Value | Description |
41+
| ------- | -------- | --------- | ------------- | -------------------------------------------------------------------- |
42+
| `style` | YES | String | text | Describes style type, for more information See `Style type's` below. |
43+
| `type` | YES | String | local | Describes which type (`local` | `jump` | `external`) icon. |
44+
45+
## Style type's
46+
47+
| Style Type | Component Name | Description |
48+
| ---------- | -------------- | ------------------------------------------------------------------------- |
49+
| `text` | LinkWithIcon | See the documentation of LinkWithIcon component and use thier props here. |
50+
| `button` | ButtonGroup | See the documentation of ButtonGroup component and use thier props here. |
51+
| `card` | Card | See the documentation of Card component and use thier props here. |
52+
| `feature` | FeatureCard | See the documentaion of FeatureCard component and user their props here. |
4253

4354
## 🙌 Contributing
4455

packages/utilities/src/utilities/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ export * from './markdownToHtml';
1414
export * from './sameHeight';
1515
export * from './serialize';
1616
export * from './settings';
17-
export * from './smoothScrolling';
17+
export * from './smoothScroll';

packages/utilities/src/utilities/smoothScrolling/index.js packages/utilities/src/utilities/smoothScroll/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
export { default as smoothScrolling } from './smoothScrolling';
8+
export { default as smoothScroll } from './smoothScroll';

packages/utilities/src/utilities/smoothScrolling/smoothScrolling.js packages/utilities/src/utilities/smoothScroll/smoothScroll.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
*
44
* @param {*} e event object
55
*/
6-
const smoothScrolling = e => {
6+
const smoothScroll = e => {
77
e.preventDefault();
88
const id = e.currentTarget.getAttribute('href');
99
document.querySelector(id).scrollIntoView({
1010
behavior: 'smooth',
1111
block: 'start',
1212
});
1313
};
14-
export default smoothScrolling;
14+
export default smoothScroll;

0 commit comments

Comments
 (0)