Skip to content

Commit

Permalink
fix(react): remove componentWillMount methods for React 17
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljeter committed Sep 25, 2019
1 parent 825c3b0 commit 2e112eb
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 91 deletions.
14 changes: 14 additions & 0 deletions react/src/app/containers/KitchenSink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CheckboxKitchenSink from '../../../lib/Checkbox/examples/KitchenSink';
import CardKitchenSink from '../../../lib/Card/examples/KitchenSink';
import InputKitchenSink from '../../../lib/Input/examples/KitchenSink';
import RadioKitchenSink from '../../../lib/Radio/examples/KitchenSink';
import SliderKitchenSink from '../../../lib/Slider/examples/KitchenSink';
import EventOverlay from '../EventOverlay';

export default class KitchenSink extends React.PureComponent {
Expand Down Expand Up @@ -60,6 +61,13 @@ export default class KitchenSink extends React.PureComponent {
keyboardKey="radio"
title="radio"
/>
<SidebarNavItem
key={`SliderKitchenSink`}
customAnchorNode={<NavLink activeClassName="md-active-nav" to={`/slider`} />}
className="md-list-item--primary"
keyboardKey="slider"
title="slider"
/>
<SidebarNavItem
key={`EventOverlay`}
customAnchorNode={
Expand Down Expand Up @@ -100,6 +108,12 @@ export default class KitchenSink extends React.PureComponent {
path={`/radio`}
render={() => <RadioKitchenSink />}
/>
<Route
key={`SliderKitchenSink`}
exact
path={`/slider`}
render={() => <SliderKitchenSink />}
/>
<Route
key={`EventOverlay`}
exact
Expand Down
4 changes: 1 addition & 3 deletions react/src/lib/Accordion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ class Accordion extends React.Component {
focus: false,
}

componentWillMount () {
componentDidMount () {
if(!this.verifyChildren()) {
throw new Error('Accordion should contain one or more AccordionGroup as children.');
}
}

componentDidMount() {
this.determineInitialFocus();
}

Expand Down
2 changes: 1 addition & 1 deletion react/src/lib/AccordionGroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AccordionGroup extends React.Component {
};
}

componentWillMount () {
componentDidMount () {
if(!this.verifyChildren()) {
throw new Error('AccordionGroup should contain 2 children ' +
'AccordionHeader and AccordionContent respectively.');
Expand Down
13 changes: 5 additions & 8 deletions react/src/lib/CheckboxGroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import PropTypes from 'prop-types';
class CheckboxGroup extends React.Component {
static displayName = 'CheckboxGroup';

state = {
values: []
}

componentWillMount() {
if (this.props.values) {
this.setState({ values: this.props.values });
}
constructor(props){
super(props);
this.state = {
values: props.values || []
};
}

handleToggle = value => {
Expand Down
2 changes: 1 addition & 1 deletion react/src/lib/DatePicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DatePicker extends React.Component {
};
};

componentWillMount() {
componentDidMount() {
const selectedDate = moment(this.props.selectedDate);
const isValid =
selectedDate.isValid()
Expand Down
4 changes: 2 additions & 2 deletions react/src/lib/Lightbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Lightbox extends React.Component {
window.addEventListener('resize', this.handleResize, true);
}

componentWillReceiveProps(nextProps) {
if (nextProps.index !== this.props.index && this.state.zoom > 1 && this.imgWrapper) {
componentDidUpdate(prevProps) {
if (prevProps.index !== this.props.index && this.state.zoom > 1 && this.imgWrapper) {
const viewportNode = this.viewport;
viewportNode.scrollTop = 0;
viewportNode.scrollLeft = (this.imgWrapper.offsetWidth - viewportNode.offsetWidth) / 2;
Expand Down
2 changes: 1 addition & 1 deletion react/src/lib/Radio/examples/Default.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Radio, RadioGroup } from '@momentum-ui/react';
export default class DefaultRadio extends React.Component {
render() {
return (
<RadioGroup>
<RadioGroup values={['me']}>
<Radio
value="me"
label="me"
Expand Down
14 changes: 5 additions & 9 deletions react/src/lib/RadioGroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import React from 'react';
import PropTypes from 'prop-types';

class RadioGroup extends React.Component {

state = {
values: [],
};

componentWillMount() {
if (this.props.values) {
this.setState({ values: this.props.values });
}
constructor(props){
super(props);
this.state = {
values: props.values || []
};
}

handleToggle = value => {
Expand Down
6 changes: 3 additions & 3 deletions react/src/lib/Slider/examples/Cross.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default class SliderCross extends React.Component {
}
render() {
return (
<div className="row" key="child-2">
<div>
<div>
<h5>
Low: {this.state.slider1.low} High: {this.state.slider1.high}
</div>
</h5>
<Slider
min={0}
max={500}
Expand Down
24 changes: 17 additions & 7 deletions react/src/lib/Slider/examples/Default.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import React from 'react';
import { Slider } from '@momentum-ui/react';
export default class DefaultSlider extends React.Component {
state = {
value: 200
}

handleChange = value => this.setState({ value })

render() {
return (
<Slider
min={0}
max={500}
tick={100}
value={200}
step={1}
/>
<div>
<h5>Value: {this.state.value}</h5>
<Slider
min={0}
max={500}
tick={100}
value={200}
step={1}
onChange={this.handleChange}
/>
</div>
);
}
}
34 changes: 34 additions & 0 deletions react/src/lib/Slider/examples/KitchenSink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import {
SliderCross,
SliderDefault,
SliderTwoHandles,
SliderStep
} from './index';
import { Slider } from '@momentum-ui/react';
export default class SliderKitchenSink extends React.Component {
render() {
return (
<div className='medium-6 columns'>
<div>
<Slider
min={0}
max={100}
value={{
low: 20,
high: 40
}}
/>
</div>
<br/>
<SliderCross />
<br/>
<SliderDefault />
<br/>
<SliderTwoHandles />
<br/>
<SliderStep />
</div>
);
}
}
24 changes: 5 additions & 19 deletions react/src/lib/Slider/examples/Step.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import React from 'react';
import { Slider } from '@momentum-ui/react';
export default class SliderStep extends React.Component {
state = {
slider1: { low: 100, high: 200 },
slider2: { low: 100, high: 200 },
slider1: { low: 100, high: 200 }
}
render() {
return (
<span>
<div className="row" key='child-0'>
<div>Low: {this.state.slider1.low} High: {this.state.slider1.high}</div>
<React.Fragment>
<div>
<h5>Low: {this.state.slider1.low} High: {this.state.slider1.high}</h5>
<Slider
min={0}
max={500}
Expand All @@ -20,20 +19,7 @@ export default class SliderStep extends React.Component {
onChange={value => this.setState({ slider1: value })}
/>
</div>
<br key='child-1' />
<div className="row" key='child-2'>
<div>Low: {this.state.slider2.low} High: {this.state.slider2.high}</div>
<Slider
min={0}
max={500}
tick={100}
value={this.state.slider2}
step={50}
canCross
onChange={value => this.setState({ slider2: value })}
/>
</div>
</span>
</React.Fragment>
);
}
}
6 changes: 3 additions & 3 deletions react/src/lib/Slider/examples/TwoHandles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default class SliderTwoHandles extends React.Component {
}
render() {
return (
<div className="row" key="child-0">
<div>
<div>
<h5>
Low: {this.state.slider1.low} High: {this.state.slider1.high}
</div>
</h5>
<Slider
min={0}
max={500}
Expand Down
1 change: 1 addition & 0 deletions react/src/lib/Slider/examples/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as SliderCross } from './Cross';
export { default as SliderDefault } from './Default';
export { default as SliderKitchenSink } from './KitchenSink';
export { default as SliderStep } from './Step';
export { default as SliderTwoHandles } from './TwoHandles';
59 changes: 30 additions & 29 deletions react/src/lib/Slider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@

import React from 'react';
import PropTypes from 'prop-types';
import SliderPointer from '@momentum-ui/react/Slider/SliderPointer';
import SliderPointer from './SliderPointer';

class Slider extends React.Component {

state = {
sliderLow: this.props.value.low || this.props.min,
sliderHigh: this.props.value.high || this.props.value,
scale: [this.props.min, this.props.max],
selectionWidth: null,
}

componentWillMount() {
const { min, max, tick } = this.props;

tick && this.getScale(min, max, tick);
this.getSelectionWidth();
constructor(props) {
super(props);

this.state = {
sliderLow: props.value.low || props.min,
sliderHigh: props.value.high || props.value,
scale: this.getScale(),
selectionWidth: null,
};
}

componentDidMount() {
const { min, max } = this.props;
const { scale } = this.state;

this.getSelectionWidth();
this.setScalePos(scale, min, max);
}

Expand All @@ -33,17 +31,22 @@ class Slider extends React.Component {
});
};

getScale = (low = 0, high, tick) => {
let value = high;
let ticksArray = [high];
getScale = () => {
const { min, max, tick } = this.props;
if (tick) {
let value = max;
let ticksArray = [max];

while (value > 0 && (value - tick) >= min) {
value -= tick;

while (value > 0 && (value - tick) >= low) {
value -= tick;
ticksArray.unshift(Math.abs(Math.round(value / tick) * tick));
}

ticksArray.unshift(Math.abs(Math.round(value / tick) * tick));
return ticksArray;
} else {
return [min, max];
}

this.setState({ scale: ticksArray });
};

getSliderWidth = () => {
Expand Down Expand Up @@ -155,7 +158,7 @@ class Slider extends React.Component {
};

return (
<div
<div
className={
`md-slider` +
`${(disabled && ` md-slider--disabled`) || ''}` +
Expand All @@ -165,13 +168,13 @@ class Slider extends React.Component {
aria-valuemin={min}
aria-valuemax={max}
aria-valuenow={
typeof this.props.value !== 'object'
?
typeof this.props.value !== 'object'
?
sliderHigh : undefined
}
aria-valuetext={
typeof this.props.value === 'object'
?
typeof this.props.value === 'object'
?
`Low is ${Math.min(sliderLow, sliderHigh)}, high is ${Math.max(sliderLow, sliderHigh)}` : undefined
}
>
Expand All @@ -183,13 +186,11 @@ class Slider extends React.Component {
<SliderPointer
position={((sliderLow - min) / (max - min)) * 100}
onMove={(b) => this.onSliderMove('sliderLow', b)}
ref={ref => this.sliderLow = ref}
/>
}
<SliderPointer
position={((sliderHigh - min) / (max - min)) * 100}
onMove={(b) => this.onSliderMove('sliderHigh', b)}
ref={ref => this.sliderHigh = ref}
/>
{renderTicks()}
</div>
Expand Down
Loading

0 comments on commit 2e112eb

Please sign in to comment.