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

fix(index.tsx): make width and height properties optional in Size int… #797

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ import { Resizable } from 're-resizable';
</Resizable>
```

If you only want to set the width, you can do so by providing just the width property.
The height property will automatically be set to auto, which means it will adjust 100% of its parent's height:

```javascript
import { Resizable } from 're-resizable';

<Resizable
defaultSize={{
width: 320
}}
>
Sample with default size
</Resizable>
```
### Example with `size`

If you use `size` props, please manage state by yourself.
Expand All @@ -92,15 +106,15 @@ import { Resizable } from 're-resizable';

## Props

#### `defaultSize?: { width: (number | string), height: (number | string) };`
#### `defaultSize?: { width?: (number | string), height?: (number | string) };`

Specifies the `width` and `height` that the dragged item should start at.
For example, you can set `300`, `'300px'`, `50%`.
If both `defaultSize` and `size` omitted, set `'auto'`.

`defaultSize` will be ignored when `size` set.

#### `size?: { width: (number | string), height: (number | string) };`
#### `size?: { width?: (number | string), height?: (number | string) };`

The `size` property is used to set the size of the component.
For example, you can set `300`, `'300px'`, `50%`.
Expand Down
20 changes: 7 additions & 13 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export interface HandleClassName {
}

export interface Size {
width: string | number;
height: string | number;
width?: string | number;
height?: string | number;
}

export interface NumberSize {
Expand Down Expand Up @@ -325,7 +325,7 @@ export class Resizable extends React.PureComponent<ResizableProps, State> {
if (typeof this.state[key] === 'undefined' || this.state[key] === 'auto') {
return 'auto';
}
if (this.propsSize && this.propsSize[key] && this.propsSize[key].toString().endsWith('%')) {
if (this.propsSize && this.propsSize[key] && this.propsSize[key]?.toString().endsWith('%')) {
if (this.state[key].toString().endsWith('%')) {
return this.state[key].toString();
}
Expand Down Expand Up @@ -389,14 +389,8 @@ export class Resizable extends React.PureComponent<ResizableProps, State> {
super(props);
this.state = {
isResizing: false,
width:
typeof (this.propsSize && this.propsSize.width) === 'undefined'
? 'auto'
: this.propsSize && this.propsSize.width,
height:
typeof (this.propsSize && this.propsSize.height) === 'undefined'
? 'auto'
: this.propsSize && this.propsSize.height,
width: this.propsSize?.width ?? 'auto',
height: this.propsSize?.height ?? 'auto',
direction: 'right',
original: {
x: 0,
Expand Down Expand Up @@ -870,7 +864,7 @@ export class Resizable extends React.PureComponent<ResizableProps, State> {
this.props.onResizeStop(event, direction, this.resizable, delta);
}
if (this.props.size) {
this.setState(this.props.size);
this.setState({ width: this.props.size.width ?? 'auto', height: this.props.size.height ?? 'auto' });
}
this.unbindEvents();
this.setState({
Expand All @@ -880,7 +874,7 @@ export class Resizable extends React.PureComponent<ResizableProps, State> {
}

updateSize(size: Size) {
this.setState({ width: size.width, height: size.height });
this.setState({ width: size.width ?? 'auto', height: size.height ?? 'auto' });
}

renderResizer() {
Expand Down