Skip to content

Commit

Permalink
refactor: rename legacy lifecycle with UNSAFE prefix and fix lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoyadmoon committed Jun 2, 2023
1 parent b57ae00 commit 0990121
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 76 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/EditableBasicRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class EditableBasicRow extends PureComponent {
focused: false,
};

// eslint-disable-next-line react/no-deprecated
componentWillReceiveProps(nextProps) {
// eslint-disable-next-line react/no-deprecated, camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.props.value) {

Check warning on line 98 in packages/core/src/EditableBasicRow.js

View workflow job for this annotation

GitHub Actions / Lint and test

Must use destructuring props assignment
this.setState({ currentValue: nextProps.value });
}
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/EditableTextLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ class EditableTextLabel extends PureComponent {
dblTouchTimeout: null,
};

// eslint-disable-next-line react/no-deprecated
componentWillReceiveProps(nextProps) {
/**
* If the edit-state of <EditableTextLabel> is *controlled* by `inEdit` prop.
* If the prop is `undefined`, this component became *uncontrolled*
* and should run itself.
*/
if (this.getEditabilityControlled(nextProps)) {
this.setState({ inEdit: nextProps.inEdit });
}
}

getEditabilityControlled(fromProps = this.props) {
return fromProps.inEdit !== undefined;
}
Expand All @@ -86,6 +74,18 @@ class EditableTextLabel extends PureComponent {
});
}

// eslint-disable-next-line react/no-deprecated, camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
/**
* If the edit-state of <EditableTextLabel> is *controlled* by `inEdit` prop.
* If the prop is `undefined`, this component became *uncontrolled*
* and should run itself.
*/
if (this.getEditabilityControlled(nextProps)) {
this.setState({ inEdit: nextProps.inEdit });
}
}

leaveEditModeIfNotControlled() {
if (!this.getEditabilityControlled(this.props)) {
this.setState({ inEdit: false });
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/StatusIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ class StatusIcon extends PureComponent {
hideIcon: false,
};

// eslint-disable-next-line react/no-deprecated
componentWillMount() {
componentWillUnmount() {
clearTimeout(this.hideIconTimeout);
}

// eslint-disable-next-line react/no-deprecated, camelcase
UNSAFE_componentWillMount() {
this.autoToggleStatusIcon();
}

// eslint-disable-next-line react/no-deprecated
componentWillReceiveProps(nextProps) {
// eslint-disable-next-line react/no-deprecated, camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.status !== this.props.status) {

Check warning on line 62 in packages/core/src/StatusIcon.js

View workflow job for this annotation

GitHub Actions / Lint and test

Must use destructuring props assignment
this.autoToggleStatusIcon(nextProps.status);
}
Expand All @@ -65,10 +69,6 @@ class StatusIcon extends PureComponent {
}
}

componentWillUnmount() {
clearTimeout(this.hideIconTimeout);
}

/**
* Auto hides status icon after being SUCCESS for 2 secs,
* or shows icon when component leaves SUCCESS state.
Expand Down
38 changes: 19 additions & 19 deletions packages/form/src/SelectList.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,6 @@ class SelectList extends PureComponent {
),
};

// eslint-disable-next-line react/no-deprecated
componentWillReceiveProps(nextProps) {
warning(
this.getIsControlled(this.props) === this.getIsControlled(nextProps),
'<SelectList> should not switch from controlled to uncontrolled (or vice versa).'
);

if (this.getIsControlled(nextProps)) {
this.setState({
checkedState: getInitialCheckedState(nextProps.value, nextProps.multiple),
});
} else if (this.props.multiple !== nextProps.multiple) {
warning(false, '<SelectList>: you should not change `multiple` prop while it is uncontrolled. Its value will be reset now.');
this.setState({
checkedState: getInitialCheckedState([]),
});
}
}

getInitialValue() {
const { value, defaultValue, multiple } = this.props;

Expand Down Expand Up @@ -129,6 +110,25 @@ class SelectList extends PureComponent {
.map(option => option.value);
}

// eslint-disable-next-line react/no-deprecated, camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
warning(
this.getIsControlled(this.props) === this.getIsControlled(nextProps),
'<SelectList> should not switch from controlled to uncontrolled (or vice versa).'
);

if (this.getIsControlled(nextProps)) {
this.setState({
checkedState: getInitialCheckedState(nextProps.value, nextProps.multiple),
});
} else if (this.props.multiple !== nextProps.multiple) {
warning(false, '<SelectList>: you should not change `multiple` prop while it is uncontrolled. Its value will be reset now.');
this.setState({
checkedState: getInitialCheckedState([]),
});
}
}

handleChange(nextCheckedState) {
const { onChange, multiple } = this.props;
const nextValues = this.getValues(nextCheckedState);
Expand Down
38 changes: 19 additions & 19 deletions packages/form/src/SelectRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,6 @@ class SelectRow extends PureComponent {
cachedValue: this.getInitialValue(),
};

// eslint-disable-next-line react/no-deprecated
componentWillReceiveProps(nextProps) {
this.setState({
valueLabelMap: getValueToLabelAvatarMap(nextProps.children),
});

warning(
this.getIsControlled(this.props) === this.getIsControlled(nextProps),
'<SelectRow> should not switch from controlled to uncontrolled (or vice versa).'
);

if (this.getIsControlled(nextProps)) {
this.setState({ cachedValue: nextProps.value });
} else if (this.props.multiple !== nextProps.multiple) {
warning(false, '<SelectRow>: you should not change `multiple` prop while it is uncontrolled. Its value will be reset now.');
this.setState({ cachedValue: (nextProps.multiple) ? [] : null });
}
}

getInitialValue() {
const { value, defaultValue, multiple } = this.props;

Expand All @@ -143,6 +124,25 @@ class SelectRow extends PureComponent {
return (multiple) ? cachedValue : [cachedValue].filter(val => val !== undefined);
}

// eslint-disable-next-line react/no-deprecated, camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
this.setState({
valueLabelMap: getValueToLabelAvatarMap(nextProps.children),
});

warning(
this.getIsControlled(this.props) === this.getIsControlled(nextProps),
'<SelectRow> should not switch from controlled to uncontrolled (or vice versa).'
);

if (this.getIsControlled(nextProps)) {
this.setState({ cachedValue: nextProps.value });
} else if (this.props.multiple !== nextProps.multiple) {
warning(false, '<SelectRow>: you should not change `multiple` prop while it is uncontrolled. Its value will be reset now.');
this.setState({ cachedValue: (nextProps.multiple) ? [] : null });
}
}

handleButtonClick = () => {
this.setState({ isPopoverOpen: true });
}
Expand Down
10 changes: 5 additions & 5 deletions packages/form/src/SwitchRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ class SwitchRow extends PureComponent {
checked: this.props.defaultChecked || this.props.checked,
};

// eslint-disable-next-line react/no-deprecated
componentWillReceiveProps(nextProps) {
this.setState({ checked: nextProps.checked });
}

getIsControlled() {
const isControlled = this.props.checked !== undefined
&& this.props.checked !== null;
Expand All @@ -64,6 +59,11 @@ class SwitchRow extends PureComponent {
return this.state.checked ? asideOn : asideOff;
}

// eslint-disable-next-line react/no-deprecated, camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
this.setState({ checked: nextProps.checked });
}

handleSwitchButtonChange = (event) => {
if (!this.getIsControlled()) {
this.setState({ checked: event.target.checked });
Expand Down
22 changes: 11 additions & 11 deletions packages/imageeditor/src/ImageEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ class ImageEditor extends PureComponent {

editorRef = React.createRef();

// eslint-disable-next-line react/no-deprecated
componentWillReceiveProps(nextProps) {
// Consider current `scale`, `position` and `initCropRect` outdated when image changes
if (nextProps.image !== this.props.image) {
this.setState({
scale: DEFAULT_SCALE,
position: DEFAULT_POSITION,
});
}
}

getImageCanvas = ({ originalSize = false } = {}) => {
if (originalSize) {
return this.editorRef.current.getImage();
Expand All @@ -128,6 +117,17 @@ class ImageEditor extends PureComponent {
return this.editorRef.current.getCroppingRect();
}

// eslint-disable-next-line react/no-deprecated, camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
// Consider current `scale`, `position` and `initCropRect` outdated when image changes
if (nextProps.image !== this.props.image) {
this.setState({
scale: DEFAULT_SCALE,
position: DEFAULT_POSITION,
});
}
}

handleSliderChange = (event) => {
const newScale = Number(event.target.value);

Expand Down

0 comments on commit 0990121

Please sign in to comment.