{"action":"created","issue":{"url":"https://api.github.com/repos/Semantic-Org/Semantic-UI-React/issues/1066","repository_url":"https://api.github.com/repos/Semantic-Org/Semantic-UI-React","labels_url":"https://api.github.com/repos/Semantic-Org/Semantic-UI-React/issues/1066/labels{/name}","comments_url":"https://api.github.com/repos/Semantic-Org/Semantic-UI-React/issues/1066/comments","events_url":"https://api.github.com/repos/Semantic-Org/Semantic-UI-React/issues/1066/events","html_url":"https://github.com/Semantic-Org/Semantic-UI-React/pull/1066","id":196743876,"number":1066,"title":"feat(AutoControlledComponent): Default values","user":{"login":"jcarbo","id":847027,"avatar_url":"https://avatars.githubusercontent.com/u/847027?v=3","gravatar_id":"","url":"https://api.github.com/users/jcarbo","html_url":"https://github.com/jcarbo","followers_url":"https://api.github.com/users/jcarbo/followers","following_url":"https://api.github.com/users/jcarbo/following{/other_user}","gists_url":"https://api.github.com/users/jcarbo/gists{/gist_id}","starred_url":"https://api.github.com/users/jcarbo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jcarbo/subscriptions","organizations_url":"https://api.github.com/users/jcarbo/orgs","repos_url":"https://api.github.com/users/jcarbo/repos","events_url":"https://api.github.com/users/jcarbo/events{/privacy}","received_events_url":"https://api.github.com/users/jcarbo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-20T18:23:31Z","updated_at":"2016-12-20T18:29:14Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/Semantic-Org/Semantic-UI-React/pulls/1066","html_url":"https://github.com/Semantic-Org/Semantic-UI-React/pull/1066","diff_url":"https://github.com/Semantic-Org/Semantic-UI-React/pull/1066.diff","patch_url":"https://github.com/Semantic-Org/Semantic-UI-React/pull/1066.patch"},"body":"Fixes #764\r\n\r\nI noticed a bug using `AutoControlledComponent` where initial state for non-AutoControlled props was getting clobbered in the `componentWillMount` of the ACC. This is because ACC uses `this.state = ...` instead of `this.setState` to set the state.\r\n\r\nExample of the bug:\r\n```js\r\nclass MyComponent extends AutoControlledComponent {\r\n  state = { someField: 'default' }\r\n\r\n  render () => <div />\r\n}\r\n\r\n// Actual\r\nshallow(<MyComponent />).state() === {}\r\n\r\n// Expected\r\nshallow(<MyComponent />).state() === { someField: 'default' }\r\n```\r\n\r\nIn fixing that bug, I came across the #764 issue which I realized could be solved simultaneously. The initial state is set within the constructor which means it's available in the ACC's `componentWillMount`.  I updated the `componentWillMount` to use the initial state if the value of the prop was not defined via props.\r\n\r\nFor example:\r\n```js\r\nclass MyComponent extends AutoControlledComponent {\r\n  state = { someField: 'default' }\r\n\r\n  static autoControlledProps = ['someField']\r\n\r\n  render () => <div />\r\n}\r\n\r\nshallow(<MyComponent />).state() === { someField: 'default' }\r\nshallow(<MyComponent defaultSomeField='propDefault' />).state() === { someField: 'propDefault' }\r\nshallow(<MyComponent someField='prop' />).state() === { someField: 'prop' }\r\n```\r\n\r\nThis seems pretty intuitive to me. The props are being passed directly to state, so to specify a \"global default\" outside of props, you set the state yourself in the constructor (or use the es7 property initializer, which is basically just sugar on the same thing).\r\n\r\n----\r\n\r\n_Edit_ As an aside, I've been using `AutoControlledComponent` as a base for some of my own components via:\r\n```js\r\nimport AutoControlledComponent from 'semantic-ui-react/dist/commonjs/lib/AutoControlledComponent'\r\n```\r\n\r\nI think it's a really cool abstraction and lets you write super flexible/reusable components that either do all the work or let the user do it.\r\n\r\n@levithomason - Thoughts on open-sourcing it as a separate package? Its only dependency is `lodash` (which you could probably even drop) so would probably be pretty simple."},"comment":{"url":"https://api.github.com/repos/Semantic-Org/Semantic-UI-React/issues/comments/268319532","html_url":"https://github.com/Semantic-Org/Semantic-UI-React/pull/1066#issuecomment-268319532","issue_url":"https://api.github.com/repos/Semantic-Org/Semantic-UI-React/issues/1066","id":268319532,"user":{"login":"codecov-io","id":8655789,"avatar_url":"https://avatars.githubusercontent.com/u/8655789?v=3","gravatar_id":"","url":"https://api.github.com/users/codecov-io","html_url":"https://github.com/codecov-io","followers_url":"https://api.github.com/users/codecov-io/followers","following_url":"https://api.github.com/users/codecov-io/following{/other_user}","gists_url":"https://api.github.com/users/codecov-io/gists{/gist_id}","starred_url":"https://api.github.com/users/codecov-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codecov-io/subscriptions","organizations_url":"https://api.github.com/users/codecov-io/orgs","repos_url":"https://api.github.com/users/codecov-io/repos","events_url":"https://api.github.com/users/codecov-io/events{/privacy}","received_events_url":"https://api.github.com/users/codecov-io/received_events","type":"User","site_admin":false},"created_at":"2016-12-20T18:29:14Z","updated_at":"2016-12-20T18:29:14Z","body":"## [Current coverage](https://codecov.io/gh/Semantic-Org/Semantic-UI-React/pull/1066?src=pr) is 95.80% (diff: 100%)\n> Merging [#1066](https://codecov.io/gh/Semantic-Org/Semantic-UI-React/pull/1066?src=pr) into [master](https://codecov.io/gh/Semantic-Org/Semantic-UI-React/branch/master?src=pr) will decrease coverage by **0.01%**\n\n```diff\n@@             master      #1066   diff @@\n==========================================\n  Files           873        866     -7   \n  Lines          4813       4791    -22   \n  Methods           0          0          \n  Messages          0          0          \n  Branches          0          0          \n==========================================\n- Hits           4612       4590    -22   \n  Misses          201        201          \n  Partials          0          0          \n```\n\n> Powered by [Codecov](https://codecov.io?src=pr). Last update [ae21628...34ccfa4](https://codecov.io/gh/Semantic-Org/Semantic-UI-React/compare/ae21628a696c017c18ffd2480a99ec1f62e73dd8...34ccfa47b321f7f15b69578357e3b12b4e13e960?src=pr)"}}
{"actor":{"display_login":"codecov-io"}}