Skip to content

Commit

Permalink
feat: add support for buttonSizeMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Soc Sieng committed Sep 10, 2020
1 parent 95bc7ce commit 0e85f04
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 9 deletions.
19 changes: 19 additions & 0 deletions examples/html/dynamic-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ const googlePayButtons = [
};
},
},
{
title: 'Button Size',
get props() {
return {
buttonColor: controls.buttonColor.value,
buttonType: controls.buttonType.value,
buttonSizeMode: 'fill',
paymentRequest: {
...defaultPaymentRequest,
transactionInfo: {
...defaultPaymentRequest.transactionInfo,
totalPrice: controls.amount.value,
},
},
existingPaymentRequired: controls.existingPaymentRequired.value === 'true',
onLoadPaymentData,
};
},
},
];

window.controls = controls;
Expand Down
2 changes: 2 additions & 0 deletions examples/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import OnCancelExample from './examples/OnCancelExample';
import DisplayItemsExample from './examples/DisplayItemsExample';
import DynamicPriceUpdateExample from './examples/DynamicPriceUpdateExample';
import DirectIntegrationExample from './examples/DirectIntegrationExample';
import ButtonSizeExample from './examples/ButtonSizeExample';

const App: React.FC = () => {
const [amount, setAmount] = useState('100.00');
Expand Down Expand Up @@ -107,6 +108,7 @@ const App: React.FC = () => {
<OnCancelExample {...props} />
<DisplayItemsExample {...props} />
<DynamicPriceUpdateExample {...props} />
<ButtonSizeExample {...props} />
</div>
);
};
Expand Down
67 changes: 67 additions & 0 deletions examples/react/src/examples/ButtonSizeExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React from 'react';
import Example from './Example';
import GooglePayButton from '@google-pay/button-react';

export default (props: any) => {
return (
<Example title="Button Size">
<GooglePayButton
paymentRequest={{
apiVersion: 2,
apiVersionMinor: 0,
allowedPaymentMethods: [
{
type: 'CARD',
parameters: {
allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
allowedCardNetworks: ['MASTERCARD', 'VISA'],
},
tokenizationSpecification: {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'example',
gatewayMerchantId: 'exampleGatewayMerchantId',
},
},
},
],
merchantInfo: {
merchantId: '12345678901234567890',
merchantName: 'Demo Merchant',
},
transactionInfo: {
totalPriceStatus: 'FINAL',
totalPriceLabel: 'Total',
totalPrice: props.amount,
currencyCode: 'USD',
countryCode: 'US',
},
}}
onLoadPaymentData={paymentRequest => {
console.log('Success', paymentRequest);
}}
existingPaymentMethodRequired={props.existingPaymentMethodRequired}
buttonColor={props.buttonColor}
buttonType={props.buttonType}
style={{ width: 400, height: 60 }}
buttonSizeMode="fill"
/>
</Example>
);
};
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"author": "socsieng@google.com",
"license": "Apache-2.0",
"dependencies": {
"@types/googlepay": "^0.4.2"
"@types/googlepay": "^0.5.0"
},
"peerDependencies": {
"@types/react": ">=16.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/button-element/GooglePayButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class GooglePayButton extends HTMLElement {
@Alias('buttontype')
buttonType?: google.payments.api.ButtonType;

@NotifyAttribute()
@Alias('buttonsizemode')
buttonSizeMode?: google.payments.api.ButtonSizeMode;

@Notify()
@Alias('paymentDataChangedCallback')
@Alias('paymentdatachangedcallback')
Expand Down Expand Up @@ -142,6 +146,7 @@ class GooglePayButton extends HTMLElement {
onPaymentAuthorized: this.onPaymentAuthorized,
buttonColor: this.buttonColor,
buttonType: this.buttonType,
buttonSizeMode: this.buttonSizeMode,
onReadyToPayChange: result => {
if (this.onReadyToPayChange) {
this.onReadyToPayChange(result);
Expand Down
10 changes: 10 additions & 0 deletions src/button-element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ integrating Google Pay into your website.
<p>Default value <code>"default"</code>.</p>
</td>
</tr>
<tr>
<td><p>buttonSizeMode</p></td>
<td><p><code>"static" | "fill"</code></p></td>
<td>
<p>Optional.</p>
<p><code>"static"</code> buttons will be sized according to the translated <code>buttonType</code>.</p>
<p><code>"fill"</code> buttons will be styled according the the element's size. Use this mode when resizing the button with CSS <code>width</code> and <code>height</code> properties.</p>
<p>Default value <code>"static"</code>.</p>
</td>
</tr>
<tr>
<td><p>buttonType</p></td>
<td><p><code>"buy" | "plain" | "donate" | "long" | "short"</code></p></td>
Expand Down
10 changes: 10 additions & 0 deletions src/button-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ integrating Google Pay into your website.
<p>Default value <code>"default"</code>.</p>
</td>
</tr>
<tr>
<td><p>buttonSizeMode</p></td>
<td><p><code>"static" | "fill"</code></p></td>
<td>
<p>Optional.</p>
<p><code>"static"</code> buttons will be sized according to the translated <code>buttonType</code>.</p>
<p><code>"fill"</code> buttons will be styled according the the element's size. Use this mode when resizing the button with CSS <code>width</code> and <code>height</code> properties.</p>
<p>Default value <code>"static"</code>.</p>
</td>
</tr>
<tr>
<td><p>buttonType</p></td>
<td><p><code>"buy" | "plain" | "donate" | "long" | "short"</code></p></td>
Expand Down
8 changes: 3 additions & 5 deletions src/lib/button-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface Config {
onReadyToPayChange?: (result: ReadyToPayChangeResponse) => void;
buttonColor?: google.payments.api.ButtonColor;
buttonType?: google.payments.api.ButtonType;
buttonSizeMode?: google.payments.api.ButtonSizeMode;
}

interface ButtonManagerOptions {
Expand Down Expand Up @@ -220,6 +221,7 @@ export class ButtonManager {
const button = this.client.createButton({
buttonType: this.config.buttonType,
buttonColor: this.config.buttonColor,
buttonSizeMode: this.config.buttonSizeMode,
onClick: this.handleClick,
});

Expand Down Expand Up @@ -333,11 +335,6 @@ export class ButtonManager {
height: 0;
overflow: hidden;
}
${this.options.cssSelector}.fill > div,
${this.options.cssSelector}.fill > div > button, ${this.options.cssSelector}.fill > div > button.long, ${this.options.cssSelector}.fill > div > button.short {
width: 100%;
height: inherit;
}
`;

if (rootNode instanceof Document && rootNode.head) {
Expand Down Expand Up @@ -393,6 +390,7 @@ export class ButtonManager {
!!config.onPaymentAuthorized,
config.buttonColor,
config.buttonType,
config.buttonSizeMode,
config.paymentRequest.merchantInfo.merchantId,
config.paymentRequest.merchantInfo.merchantName,
config.paymentRequest.merchantInfo.softwareInfo?.id,
Expand Down

0 comments on commit 0e85f04

Please sign in to comment.