Skip to content

Commit

Permalink
Merge branch 'master' into fixes-for-flow-v0.47.0
Browse files Browse the repository at this point in the history
  • Loading branch information
k15a authored Jun 6, 2017
2 parents c1a845c + 07c8317 commit 9fa6040
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file. If a contri

## [Unreleased]

- Fixed `extend` not working with 3 or more inheritances, thanks to [@brunolemos](https://twitter.com/brunolemos). (see [#871](https://github.com/styled-components/styled-components/pull/871))
- Added a test for `withComponent` followed by `attrs`, thanks to [@btmills](https://github.com/btmills). (see [#851](https://github.com/styled-components/styled-components/pull/851))
- Upgrade stylis to v3.0.4. (see [#829](https://github.com/styled-components/styled-components/pull/829))

## [v2.0.0] - 2017-05-25
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ npm install --save styled-components
```

[![npm](https://img.shields.io/npm/v/styled-components.svg)](https://www.npmjs.com/package/styled-components)
[![Travis Build Status](https://travis-ci.org/styled-components/styled-components.svg?branch=master)](https://travis-ci.org/styled-components/styled-components)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/gruntjs/grunt?branch=master&svg=true)](https://ci.appveyor.com/project/mxstbr/styled-components)
[![Supported by Thinkmill](https://thinkmill.github.io/badge/heart.svg)](http://thinkmill.com.au/?utm_source=github&utm_medium=badge&utm_campaign=styled-components)
[![gitter](https://camo.githubusercontent.com/54dc79dc7da6b76b17bc8013342da9b4266d993c/68747470733a2f2f6261646765732e6769747465722e696d2f6d78737462722f72656163742d626f696c6572706c6174652e737667)](https://gitter.im/styled-components/styled-components)
[![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/styled-components)

![gzip size](http://img.badgesize.io/https://unpkg.com/styled-components/dist/styled-components.min.js?compression=gzip&label=gzip%20size)
![size](http://img.badgesize.io/https://unpkg.com/styled-components/dist/styled-components.min.js?label=size)
Expand Down Expand Up @@ -110,6 +107,7 @@ We could use your help to get syntax highlighting support to other editors! If y
- [styled-tools](https://github.com/diegohaz/styled-tools): Useful interpolated functions for styled-components.
- [styled-ax](https://github.com/Lokua/styled-ax): Functional theme property accessor(s)
- [react-create-component-from-tag-prop](https://github.com/jameslnewell/react-create-component-from-tag-prop): Create a react component from a tag prop. Lets your users to choose which HTML elements get styled by your 💅 styled-components.
- [styled-components-theme](https://github.com/erikras/styled-components-theme): A library for refering to theme colors and modifying them inline. e.g. `color: ${primary.lighten(0.3)};`

### Boilerplates
- [react-redux-styled-hot-universal](https://github.com/krasevych/react-redux-styled-hot-universal) (SSR, Universal Webpack, Redux, React-router, Webpack 2, Babel, Styled Components and more...)
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
"jsnext:main": "dist/styled-components.es.js",
"module": "dist/styled-components.es.js",
"scripts": {
"build": "npm run build:lib && npm run build:dist && npm run build:flow",
"build": "npm run build:lib && npm run build:dist",
"prebuild:lib": "rimraf lib/*",
"build:lib": "babel --out-dir lib src",
"build:flow": "flow-copy-source -v -i '{**/test/*.js,**/*.test.js}' src lib",
"prebuild:dist": "rimraf dist/*",
"build:dist": "rollup -c && rollup -c --environment ESBUNDLE && rollup -c --environment PRODUCTION",
"build:watch": "npm run build:lib -- --watch",
Expand Down
19 changes: 17 additions & 2 deletions src/models/StyledComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,23 @@ export default (ComponentStyle: Function, constructWithOptions: Function) => {
}

static get extend() {
const { displayName: _, componentId: __, ...optionsToCopy } = options
const newOptions = { ...optionsToCopy, rules, ParentComponent: StyledComponent }
const {
displayName: _,
componentId: __,
rules: rulesFromOptions,
...optionsToCopy
} = options

const newRules = rulesFromOptions === undefined
? rules
: rulesFromOptions.concat(rules)

const newOptions = {
...optionsToCopy,
rules: newRules,
ParentComponent: StyledComponent,
}

return constructWithOptions(createStyledComponent, target, newOptions)
}
}
Expand Down
25 changes: 22 additions & 3 deletions src/models/StyledNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,28 @@ export default (constructWithOptions: Function) => {
}

static get extend() {
const { displayName: _, componentId: __, ...optionsToCopy } = options
const newOptions = { ...optionsToCopy, rules, ParentComponent: StyledNativeComponent }
return constructWithOptions(createStyledNativeComponent, target, newOptions)
const {
displayName: _,
componentId: __,
rules: rulesFromOptions,
...optionsToCopy
} = options

const newRules = rulesFromOptions === undefined
? rules
: rulesFromOptions.concat(rules)

const newOptions = {
...optionsToCopy,
rules: newRules,
ParentComponent: StyledNativeComponent,
}

return constructWithOptions(
createStyledNativeComponent,
target,
newOptions,
)
}
}

Expand Down
49 changes: 49 additions & 0 deletions src/native/test/native.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,55 @@ describe('native', () => {
}, undefined
])
})

it('should combine styles of extending components in >= 3 inheritances', () => {
const GrandGrandParent = styled.View`background-color: red;`
const GrandParent = GrandGrandParent.extend`borderWidth: 10;`
const Parent = GrandParent.extend`opacity: 0.9;`
const Child = Parent.extend`padding: 10px;`

const grandGrandParent = shallow(<GrandGrandParent />)
const grandParent = shallow(<GrandParent />)
const parent = shallow(<Parent />)
const child = shallow(<Child />)

expect(grandGrandParent.find('View').prop('style')).toEqual([
{
backgroundColor: 'red',
},
undefined,
])

expect(grandParent.find('View').prop('style')).toEqual([
{
backgroundColor: 'red',
borderWidth: 10,
},
undefined,
])

expect(parent.find('View').prop('style')).toEqual([
{
backgroundColor: 'red',
borderWidth: 10,
opacity: 0.9,
},
undefined,
])

expect(child.find('View').prop('style')).toEqual([
{
backgroundColor: 'red',
borderWidth: 10,
opacity: 0.9,
paddingTop: 10,
paddingRight: 10,
paddingBottom: 10,
paddingLeft: 10,
},
undefined,
])
})
})

describe('attrs', () => {
Expand Down
45 changes: 45 additions & 0 deletions src/test/extending.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,40 @@ describe('extending', () => {
expect(Child.fetchData()).toEqual(1)
})

it('should keep styles in >= 3 inheritances', () => {
const GrandGrandParent = styled.div`
background-color: red;
`

const GrandParent = GrandGrandParent.extend`
color: blue;
`

const Parent = GrandParent.extend`
border: 2px solid black;
`

const Child = Parent.extend`
border-width: 10;
`

shallow(<GrandGrandParent />)
shallow(<GrandParent />)
shallow(<Parent />)
shallow(<Child />)

expectCSSMatches(`
.sc-a { }
.e { background-color: red; }
.sc-b { }
.f { background-color: red; color: blue; }
.sc-c { }
.g { background-color: red; color: blue; border: 2px solid black; }
.sc-d { }
.h { background-color: red; color: blue; border: 2px solid black; border-width: 10; }
`)
})

it('should allow changing component', () => {
const Parent = styled.div`color: red;`
const Child = Parent.withComponent('span')
Expand All @@ -158,4 +192,15 @@ describe('extending', () => {
.sc-c {} .d { color: red; color: green; }
`)
})

it('should allow changing component and adding attributes', () => {
const Parent = styled.button`
color: red;
`
const Child = Parent.withComponent('a').extend.attrs({
href: '/test'
})``

expect(shallow(<Child />).html()).toEqual('<a href="/test" class="sc-c d"></a>')
})
})

0 comments on commit 9fa6040

Please sign in to comment.