Skip to content

Commit 7086c55

Browse files
committed
feat(ResizableBox): Accept a "style" param
1 parent ef2a922 commit 7086c55

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,13 @@ These props apply to both `<Resizable>` and `<ResizableBox>`.
6565
resizeHandles?: ?Array<'s' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne'> = ['se']
6666
};
6767
```
68+
69+
The following props can also be used on `<ResizableBox>`:
70+
71+
```js
72+
{
73+
style?: Object
74+
}
75+
```
76+
77+
If a `width` or `height` is passed to `<ResizableBox>`'s `style` prop, it will be ignored as it is required for internal function.

__tests__/ResizableBox.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('render ResizableBox', () => {
2121
resizeHandles: ['w'],
2222
width: 50,
2323
};
24-
const children = <span className={'children'} />;
24+
const children = <span className="children" />;
2525

2626
beforeEach(() => {
2727
jest.clearAllMocks();
@@ -60,4 +60,21 @@ describe('render ResizableBox', () => {
6060
resizable.simulate('resizeStop', fakeEvent, data);
6161
expect(props.onResizeStop).toHaveBeenCalledWith(fakeEvent, data);
6262
});
63+
64+
test('style prop', () => {
65+
const element = shallow(<ResizableBox {...props} style={{backgroundColor: 'red'}}>{children}</ResizableBox>);
66+
expect(element.find('div').at(0).prop('style')).toEqual({
67+
width: '50px',
68+
height: '50px',
69+
backgroundColor: 'red'
70+
});
71+
});
72+
73+
test('style prop width and height ignored', () => {
74+
const element = shallow(<ResizableBox {...props} style={{width: 10, height: 10}}>{children}</ResizableBox>);
75+
expect(element.find('div').at(0).prop('style')).toEqual({
76+
width: '50px',
77+
height: '50px',
78+
});
79+
});
6380
});

lib/ResizableBox.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import type {ResizeCallbackData, ResizableBoxState} from './propTypes';
99
// ElementConfig gives us an object type where all items present in `defaultProps` are made optional.
1010
// <ResizableBox> does not have defaultProps, so we can use this type to tell Flow that we don't
1111
// care about that and will handle it in <Resizable> instead.
12-
type ResizableBoxProps = React.ElementConfig<typeof Resizable>;
12+
// A <ResizableBox> can also have a `style` property.
13+
type ResizableBoxProps = {|...React.ElementConfig<typeof Resizable>, style?: Object|};
1314

1415
export default class ResizableBox extends React.Component<ResizableBoxProps, ResizableBoxState> {
1516
static propTypes = resizableProps;
@@ -62,6 +63,7 @@ export default class ResizableBox extends React.Component<ResizableBoxProps, Res
6263
width,
6364
height,
6465
resizeHandles,
66+
style,
6567
transformScale,
6668
...props
6769
} = this.props;
@@ -83,7 +85,7 @@ export default class ResizableBox extends React.Component<ResizableBoxProps, Res
8385
transformScale={transformScale}
8486
width={this.state.width}
8587
>
86-
<div style={{width: this.state.width + 'px', height: this.state.height + 'px'}} {...props} />
88+
<div {...props} style={{...style, width: this.state.width + 'px', height: this.state.height + 'px'}} />
8789
</Resizable>
8890
);
8991
}

0 commit comments

Comments
 (0)