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

Pr add observed attributes #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare namespace ReactWebComponent {
export const create: (app: JSX.Element, tagName: string, optOutFromShadowRoot?: boolean) => void;
export const create: (app: JSX.Element, tagName: string, optOutFromShadowRoot?: boolean, observedAttributes?: string[]) => void;
}

export default ReactWebComponent;
1 change: 1 addition & 0 deletions src/camelCasedAttribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";module.exports=function(a){return a.replace(/-([a-z])/g,function(a){return a[1].toUpperCase()})};
8 changes: 8 additions & 0 deletions src/dev/camelCasedAttribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Takes in a node attributes map and returns an object with camelCased properties and values
* @param attribute
* @returns {{}}
*/
module.exports = function camelCasedAttribute(attribute) {
return attribute.replace(/-([a-z])/g, (g) => g[1].toUpperCase())
};
4 changes: 3 additions & 1 deletion src/dev/extractAttributes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const camelCasedAttribute = require('./camelCasedAttribute');

/**
* Takes in a node attributes map and returns an object with camelCased properties and values
* @param nodeMap
Expand All @@ -15,7 +17,7 @@ module.exports = function extractAttributes(nodeMap) {

for (attribute of attributes) {
const key = Object.keys(attribute)[0];
const camelCasedKey = key.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
const camelCasedKey = camelCasedAttribute(key);
obj[camelCasedKey] = attribute[key];
}

Expand Down
9 changes: 7 additions & 2 deletions src/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const React = require('react');
const ReactDOM = require('react-dom');
const retargetEvents = require('react-shadow-dom-retarget-events');
const getStyleElementsFromReactWebComponentStyleLoader = require('./getStyleElementsFromReactWebComponentStyleLoader');
const camelCasedAttribute = require('./camelCasedAttribute');
const extractAttributes = require('./extractAttributes');

require('@webcomponents/shadydom');
Expand All @@ -12,8 +13,9 @@ module.exports = {
* @param {JSX.Element} app
* @param {string} tagName - The name of the web component. Has to be minus "-" delimited.
* @param {boolean} useShadowDom - If the value is set to "true" the web component will use the `shadowDom`. The default value is true.
* @param {string[]} observedAttributes - The observed attributes of the web component
*/
create: (app, tagName, useShadowDom = true) => {
create: (app, tagName, useShadowDom = true, observedAttributes = []) => {
let appInstance;

const lifeCycleHooks = {
Expand All @@ -40,6 +42,9 @@ module.exports = {
}

const proto = class extends HTMLElement {
static get observedAttributes() {
return observedAttributes;
}
connectedCallback() {
const webComponentInstance = this;
let mountPoint = webComponentInstance;
Expand Down Expand Up @@ -73,7 +78,7 @@ module.exports = {
callLifeCycleHook('disconnectedCallback');
}
attributeChangedCallback (attributeName, oldValue, newValue, namespace) {
callLifeCycleHook('attributeChangedCallback', [attributeName, oldValue, newValue, namespace]);
callLifeCycleHook('attributeChangedCallback', [camelCasedAttribute(attributeName), oldValue, newValue, namespace]);
}
adoptedCallback (oldDocument, newDocument) {
callLifeCycleHook('adoptedCallback', [oldDocument, newDocument]);
Expand Down
2 changes: 1 addition & 1 deletion src/extractAttributes.js

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

2 changes: 1 addition & 1 deletion src/index.js

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