Skip to content

Commit

Permalink
feat: disable CreditCardSubmitButton (square#99)
Browse files Browse the repository at this point in the history
fixes square#96
  • Loading branch information
maxbeatty authored Jan 14, 2021
1 parent 166db10 commit bd06544
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
10 changes: 7 additions & 3 deletions docs/components/CreditCardSubmitButton.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
id:CreditCardSubmitButton
title:CreditCardSubmitButton
---

Renders a button that will create a card nonce using Square's SqPaymentForm JS library and calls
`onCardNonceResponseReceived` afterwards.

When accepting credit card payments, you **must** have this component inside your `SquarePaymentForm`.

## Props
|Name|Type|Description|Default Value|
|---|---|---|---|
|children|React.ReactNode|Input field label||

| Name | Type | Description | Default Value |
| -------- | --------------- | ------------------- | ------------- |
| children | React.ReactNode | children components | |
| disabled | Boolean | disable button | false |
9 changes: 7 additions & 2 deletions src/components/CreditCardSubmitButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import React, { useContext } from 'react';
import Context from './Context';

interface Props {
/** Input field label */
children?: React.ReactNode;
disabled?: boolean;
}

/**
* Renders a button that will create a card nonce using Square's SqPaymentForm JS library and calls
* `onCardNonceResponseReceived` afterwards.
Expand All @@ -15,8 +16,12 @@ interface Props {
export const CreditCardSubmitButton: React.FC<Props> = (props: Props) => {
const context = useContext(Context);
return (
<button className="sq-creditcard" onClick={context.onCreateNonce}>
<button disabled={props.disabled} className="sq-creditcard" onClick={context.onCreateNonce}>
{props.children ? props.children : 'Pay'}
</button>
);
};

CreditCardSubmitButton.defaultProps = {
disabled: false,
};
12 changes: 12 additions & 0 deletions src/components/__tests__/CreditCardSubmitButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ describe('CreditCardPostalCodeInput', () => {
expect(wrapper.find('button').text()).to.eql(test);
});
});

describe('disabled', () => {
it('should not be disabled by default', () => {
const wrapper = mount(<CreditCardSubmitButton />);
expect(wrapper.find('button').prop('disabled')).to.eql(false);
});

it('can be disabled', () => {
const wrapper = mount(<CreditCardSubmitButton disabled />);
expect(wrapper.find('button').prop('disabled')).to.eql(true);
});
});
});

0 comments on commit bd06544

Please sign in to comment.