import { useEffect, useState } from 'react';
import Koji from '@withkoji/core';
import CustomVcc from '@withkoji/custom-vcc-sdk'; // (1)
import './App.css';
// Create an instance of CustomVcc.
const myCustomCtrl = new CustomVcc(); // (2)
function App() {
// A place to store the value entered.
let myStr = '';
// What to do after the user is done.
const finish = async () => {
// When a user clicks Save, update the value of the custom control, and save the file.
await myCustomCtrl.change(myStr); // (5)
await myCustomCtrl.save(); // (6)
// alert( "Finished!" );
};
/*
// (Optional) Listen for changes to the consumer/parent app theme and match it in your custom control.
const [theme, setTheme] = ...;
useEffect(() => {
myCustomCtrl.onTheme((theme) => { // (3)
setTheme(theme);
});
}, []);
*/
// Listen for changes to the custom control's current state.
useState(() => {
myCustomCtrl.onUpdate((myProps) => { // (3)
// alert( "myProps.value is " + myProps.value ); // (7)
});
});
// When the custom control has loaded, register it to trigger events from the consumer app.
useEffect(() => {
myCustomCtrl.register(); // (4)
}, []);
// Call the ready method once (with an empty dependency array)
// to let the platform know we have our listeners set up and
// to unblock the rendering and lookups for the rest of our application.
useEffect(() => {
Koji.ready();
}, []);
return (
<div className="App">
<header className="App-header">
<h1>My Custom Control</h1>
<p>Enter any text:</p>
<input
onChange={(e) => {
myStr = e.currentTarget.value;
}}
/>
<button onClick={finish}>{'Save'}</button>
</header>
</div>
);
}
export default App;