|
1 | 1 | import PropTypes from 'prop-types';
|
2 | 2 | import React from 'react';
|
3 | 3 | import isNil from 'lodash/isNil';
|
| 4 | +import omit from 'lodash/omit'; |
4 | 5 | import { css } from '@emotion/core';
|
5 | 6 | import vars from '../../../../materials/custom-properties';
|
6 |
| -import filterAriaAttributes from '../../../utils/filter-aria-attributes'; |
7 |
| -import filterDataAttributes from '../../../utils/filter-data-attributes'; |
| 7 | +import filterInvalidAttributes from '../../../utils/filter-invalid-attributes'; |
8 | 8 | import Spacings from '../../spacings';
|
9 | 9 | import AccessibleButton from '../accessible-button';
|
10 |
| -import { |
11 |
| - getButtonLayoutStyles, |
12 |
| - getButtonStyles, |
13 |
| -} from './primary-button.styles'; |
| 10 | +import { getButtonStyles } from './primary-button.styles'; |
| 11 | + |
| 12 | +const propsToOmit = ['type']; |
14 | 13 |
|
15 | 14 | const PrimaryButton = props => {
|
16 | 15 | const dataProps = {
|
17 | 16 | 'data-track-component': 'PrimaryButton',
|
18 |
| - ...filterAriaAttributes(props), |
19 |
| - ...filterDataAttributes(props), |
| 17 | + ...filterInvalidAttributes(omit(props, propsToOmit)), |
| 18 | + // if there is a divergence between `isDisabled` and `disabled`, |
| 19 | + // we fall back to `isDisabled` |
| 20 | + disabled: props.isDisabled, |
20 | 21 | };
|
21 | 22 |
|
22 | 23 | const isActive = props.isToggleButton && props.isToggled;
|
23 | 24 | return (
|
24 |
| - <div css={getButtonLayoutStyles(props.size)}> |
25 |
| - <AccessibleButton |
26 |
| - type={props.type} |
27 |
| - buttonAttributes={dataProps} |
28 |
| - label={props.label} |
29 |
| - onClick={props.onClick} |
30 |
| - isToggleButton={props.isToggleButton} |
31 |
| - isToggled={props.isToggled} |
32 |
| - isDisabled={props.isDisabled} |
33 |
| - css={getButtonStyles(props.isDisabled, isActive, props.tone)} |
34 |
| - > |
35 |
| - <Spacings.Inline alignItems="center" scale="xs"> |
36 |
| - {Boolean(props.iconLeft) && ( |
37 |
| - <span |
38 |
| - css={css` |
39 |
| - margin: 0 ${vars.spacingXs} 0 0; |
40 |
| - display: flex; |
41 |
| - align-items: center; |
42 |
| - justify-content: center; |
43 |
| - `} |
44 |
| - > |
45 |
| - {React.cloneElement(props.iconLeft, { |
46 |
| - color: props.isDisabled ? 'neutral60' : 'surface', |
47 |
| - size: props.size === 'small' ? 'medium' : 'big', |
48 |
| - })} |
49 |
| - </span> |
50 |
| - )} |
51 |
| - <span>{props.label}</span> |
52 |
| - </Spacings.Inline> |
53 |
| - </AccessibleButton> |
54 |
| - </div> |
| 25 | + <AccessibleButton |
| 26 | + as={props.as} |
| 27 | + type={props.type} |
| 28 | + buttonAttributes={dataProps} |
| 29 | + label={props.label} |
| 30 | + onClick={props.onClick} |
| 31 | + isToggleButton={props.isToggleButton} |
| 32 | + isToggled={props.isToggled} |
| 33 | + isDisabled={props.isDisabled} |
| 34 | + css={getButtonStyles(props.isDisabled, isActive, props.tone, props.size)} |
| 35 | + > |
| 36 | + <Spacings.Inline alignItems="center" scale="xs"> |
| 37 | + {Boolean(props.iconLeft) && ( |
| 38 | + <span |
| 39 | + css={css` |
| 40 | + margin: 0 ${vars.spacingXs} 0 0; |
| 41 | + display: flex; |
| 42 | + align-items: center; |
| 43 | + justify-content: center; |
| 44 | + `} |
| 45 | + > |
| 46 | + {React.cloneElement(props.iconLeft, { |
| 47 | + color: props.isDisabled ? 'neutral60' : 'surface', |
| 48 | + size: props.size === 'small' ? 'medium' : 'big', |
| 49 | + })} |
| 50 | + </span> |
| 51 | + )} |
| 52 | + <span>{props.label}</span> |
| 53 | + </Spacings.Inline> |
| 54 | + </AccessibleButton> |
55 | 55 | );
|
56 | 56 | };
|
57 | 57 |
|
58 | 58 | PrimaryButton.propTypes = {
|
| 59 | + as: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]), |
59 | 60 | type: PropTypes.oneOf(['submit', 'reset', 'button']),
|
60 | 61 | label: PropTypes.string.isRequired,
|
61 | 62 | buttonAttributes: PropTypes.object,
|
|
0 commit comments