Skip to content
This repository has been archived by the owner on Feb 4, 2018. It is now read-only.

feat(h): make h set attributes on custom-element via attributes prop #15

Merged
merged 6 commits into from
Feb 2, 2017

Conversation

Hotell
Copy link
Contributor

@Hotell Hotell commented Jan 30, 2017

  • this is a breaking change by how attributes are set ( via attrs )
  • adds event setup via events

Closes #14

Copy link
Member

@treshugart treshugart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see if we can make the h behaviour same to what's being proposed in facebook/react#7249 as well as what's in skatejs/dom-diff and also being proposed in google/incremental-dom#316.

Additionally, I think the test can be a lot simpler for this use case.

test/unit.js Outdated
@@ -26,7 +26,62 @@ describe('bore', () => {
expect(<Fn />.localName).to.equal('div');
});

it('setting attributes', () => {
it('setting attributes on CustomElement', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to test a custom element here. It's all just DOM. You should just be able to test a <div /> with something like:

const el = mount(<div prop={true} attributes={{ test: 'test' }} />);
expect(el.prop).to.equal(true);
expect(el.hasAttribute('prop')).to.equal(false);
expect(el.attr).to.equal(undefined);
expect(el.getAttribute('attr')).to.equal('test');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, actually that is in next test. totally missed that ( also because non obvious naming of attrs test1 and so on :P )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also because non obvious naming of attrs test1 and so on

Not sure if I understand that statement.

If it's already being tested, it'd be good if we can just remove this test. It's quite complex and difficult to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that numbers suffix confused me I guess, so I didn't pay close attention to them, but my bad. sry nevermind

src/index.js Outdated
}

function handleFunction (Fn) {
return Fn.prototype instanceof HTMLElement ? new Fn() : Fn();
}

function setAttr (node, attrName, attrValue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be good to just make this a breaking change and require attributes to set attributes (or attrs?). Everything else just sets props. Maybe at the same time we can do events? I'd like to split this out into a separate library for declaratively constructing real DOM eventually, also.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@Hotell Hotell Jan 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be good to just make this a breaking change

why BC? it's clearly a feature. Currently there is no option to set attributes via bore within test, only via strings ( which doesnt work either #12 )

Overall as I re-thought the approach, overriding attributes feels just wrong ( although google/incremental-dom#316 is going to use it ) . I would rather go as you've suggested with attrs ( also because from Typescript perspective, native HTLMElement.attributes cannot be overriden via just object ( because under the hood it's not, it's NamedNodeMap ) - with attrs smooth typechecking can be provided )

Maybe at the same time we can do events?

I agree, but that should be addressed in next PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's calling node.setAttribute() if attributes is not defined. What I'm saying is that it should be setting a prop if no attributes is defined.

I agree, but that should be addressed in next PR

Sure, but it'd be nice if it could happen soon after.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's calling node.setAttribute() if attributes is not defined. What I'm saying is that it should be setting a prop if no attributes is defined.

Oh right. But I like your idea https://github.com/skatejs/dom-diff#hname-props-childrenortext which may not be consistent with future changes within iDom.

So ok, let's make it BC then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So as I recap.

I vote for attrs only for setting attributes. events for setting events.

Pls lemme know your decision so I can implement it, and merge ASAP.

thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do eeet!

BREAKING CHANGE:

Before you could set attributes via bore's h via data-* or aria-* otherwise the attribute would be set to element property.
Now if you wanna set attributes you have to explicitly do it via `attrs` property which accepts a object map.

Before:
```js
<my-element aria-role="button" data-foo="hello" />
```

After:
```js
<my-element attrs={{ 'aria-role': 'button', 'data-foo': 'hello' }} />
```
@Hotell
Copy link
Contributor Author

Hotell commented Feb 1, 2017

changes added. re - review pls @treshugart

If LGTM I will update the docs as well as part of this PR, then we can merge

src/index.js Outdated
}

function handleFunction (Fn) {
return Fn.prototype instanceof HTMLElement ? new Fn() : Fn();
}

function setAttrs (node, attrValue) {
Object.keys(attrValue)
.forEach((key) => { node.setAttribute(key, attrValue[key]); });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary arrow fn braces.

@treshugart
Copy link
Member

Hey @Hotell instead of adding nits / codestyle stuff (that semistandard didn't seem to catch) I've just created a PR for some minor things rather than going back and forth or pushing directly to your branch. Cheers!

@treshugart
Copy link
Member

Approved pending other PR is either merged here, or this is merged and that is retargeted to master.

@Hotell Hotell merged commit e031e57 into master Feb 2, 2017
@Hotell Hotell deleted the attribute-aware-h branch February 2, 2017 07:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants