-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Create component class in main.
- Loading branch information
Showing
7 changed files
with
38 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,24 @@ | ||
'use strict'; | ||
|
||
import initInstance, {removeInstance} from '../main'; // for direct API usage | ||
import MyComponent from '../main'; // for direct API usage | ||
|
||
function init() { | ||
|
||
// Demo both the use of eventing and direct API | ||
|
||
// Demo eventing API | ||
document.body.dispatchEvent(new CustomEvent('o.InitMyComponent', { | ||
detail: { | ||
elementId: 'demo-target1', | ||
greeting: 'Hello world!' | ||
} | ||
})); | ||
|
||
document.body.dispatchEvent(new CustomEvent('o.InitMyComponent', { | ||
detail: { | ||
elementId: 'demo-target2', | ||
greeting: 'Bonjour le monde!', | ||
locale: 'fr-CA' | ||
} | ||
})); | ||
|
||
initInstance({ | ||
elementId: 'demo-target3', | ||
// Demo direct API | ||
new MyComponent({ | ||
elementId: 'demo-target2', | ||
greeting: 'Bonjour le monde!', | ||
locale: 'fr-CA' | ||
}); | ||
|
||
// Remove after some delay | ||
setTimeout(function() { | ||
removeInstance('demo-target3'); | ||
}, 3000); | ||
|
||
} | ||
|
||
window.onload = init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,45 @@ | ||
// | ||
// Change all references to 'MyComponent' in this file to your real component name! | ||
// | ||
|
||
// bundled component styling | ||
import './main.scss'; | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import ComponentOwner from './src/js/component-owner'; | ||
|
||
// i18n, set up for French out of the box | ||
// i18n, set up for French out-of-the-box | ||
import {addLocaleData, IntlProvider} from 'react-intl'; | ||
import frLocaleData from 'react-intl/locale-data/fr'; | ||
import frJson from './translations/fr.json'; | ||
const translations = { | ||
'fr-CA' : frJson | ||
}; | ||
addLocaleData(frLocaleData); | ||
|
||
// | ||
// Change initInstance to initYourComponentName | ||
// | ||
export default function initInstance(config) { | ||
export default class MyComponent { | ||
|
||
const locale = config.locale ? config.locale : 'en'; | ||
constructor(config) { | ||
|
||
ReactDOM.render( | ||
<IntlProvider locale={locale} messages={translations[locale]}> | ||
<ComponentOwner data={config} /> | ||
</IntlProvider>, | ||
document.getElementById(config.elementId) | ||
); | ||
} | ||
addLocaleData(frLocaleData); | ||
this.init(config); | ||
} | ||
|
||
init(config) { | ||
|
||
const locale = config.locale ? config.locale : 'en'; | ||
|
||
ReactDOM.render( | ||
<IntlProvider locale={locale} messages={translations[locale]}> | ||
<ComponentOwner data={config}/> | ||
</IntlProvider>, | ||
document.getElementById(config.elementId) | ||
); | ||
} | ||
|
||
// | ||
// Change removeInstance to initYourComponentName | ||
// | ||
export function removeInstance(elementId) { | ||
ReactDOM.unmountComponentAtNode(document.getElementById(elementId)) | ||
} | ||
|
||
// Event Listeners for evented interface | ||
// | ||
// Change any reference to 'MyComponent' to your real component name! | ||
// For events, use the Origami naming convention of pre-pending with 'o.' | ||
// | ||
document.body.addEventListener('o.InitMyComponent', e => initInstance(e.detail)); | ||
document.body.addEventListener('o.UnmountMyComponent', e => removeInstance(e.detail)); | ||
document.body.addEventListener('o.InitMyComponent', e => new MyComponent(e.detail)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters