Skip to content
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

feat: allow updating props #144

Merged
merged 1 commit into from
Apr 18, 2024
Merged
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
14 changes: 9 additions & 5 deletions public/scripts/commit-button-example.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>

<head>
<script src="./zoid/zoid.js"></script>
<script src="./zoid/zoid.min.js"></script>
<script src="../../scripts/commit-button.js"></script>
<style>
html,
Expand All @@ -20,13 +20,14 @@ <h3>Commit button example</h3>
<div id="container"></div>

<script>
let disabled = false;
const instance = CommitButton({
configId: "testing-80002-0",
productUuid: "086b32-3fcd-00d1-0624-67513e85415c",
sellerId: "138",
modalMargin: "2%",
lookAndFeel: "modal",
disabled: false,
disabled,
buttonStyle: {
minWidth: "100px",
minHeight: "200px",
Expand All @@ -36,15 +37,18 @@ <h3>Commit button example</h3>
},
onGetDimensions: function (dimensions) {
const { offsetHeight, offsetWidth } = dimensions;
document.querySelector(
"#container"
).style.height = `${offsetHeight}px`;
document.querySelector("#container").style.height = `${offsetHeight}px`;
document.querySelector(
"#container"
).style.minWidth = `${offsetWidth}px`;
}
});

instance.render("#container");
function toggleDisableState() {
disabled = !disabled;
instance.updateProps({ disabled });
}
</script>
<button onclick="toggleDisableState()" style="margin: 20px;">toggle disable state</button>
</body>
11 changes: 9 additions & 2 deletions src/components/widgets/commitButton/CommitButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { CommitButtonView } from "@bosonprotocol/react-kit";
import { ElementRef, useCallback, useEffect, useMemo, useRef } from "react";
import { useState } from "react";
import * as yup from "yup";

import { GlobalStyle } from "../styles";
Expand All @@ -11,7 +12,6 @@ export const commitButtonPath = "/commit-button";
declare const CommitWidgetModal: (props: Record<string, unknown>) => any;
declare const PurchaseOverviewModal: (props: Record<string, unknown>) => any;

const emptyObject = {};
const yupStringOrNumber = yup
.mixed<string | number>()
.test(
Expand All @@ -24,8 +24,15 @@ const yupStringOrNumber = yup
);
export function CommitButton() {
const ref = useRef<ElementRef<"div">>(null);
const props = window.xprops ?? emptyObject;
const [props, setProps] = useState(window.xprops ?? {});
const { renderToSelector, buttonStyle, ...commitWidgetProps } = props;
useEffect(() => {
if ("xprops" in window && typeof window.xprops.onProps === "function") {
window.xprops.onProps((newProps: typeof props) => {
setProps({ ...newProps });
});
}
}, []);
const modalMargin = props.modalMargin || "2%";
const sendDimensions = useCallback(() => {
if (
Expand Down
12 changes: 9 additions & 3 deletions src/components/widgets/purchaseOverview/PurchaseOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
/* eslint-disable no-var */

import { PurchaseOverview as PurchaseOverviewReactKit } from "@bosonprotocol/react-kit";
import { useMemo } from "react";
import { useEffect, useMemo, useState } from "react";
import * as yup from "yup";

import { GlobalStyle } from "../styles";
export const purchaseOverviewPath = "/purchase-overview";
const emptyObject = {};
export const PurchaseOverview = () => {
const props = window.xprops ?? emptyObject;
const [props, setProps] = useState(window.xprops ?? {});
useEffect(() => {
if ("xprops" in window && typeof window.xprops.onProps === "function") {
window.xprops.onProps((newProps: typeof props) => {
setProps({ ...newProps });
});
}
}, []);
const validatedProps = useMemo(() => {
return yup
.object({
Expand Down
Loading