diff --git a/lib/components/Balloon/Balloon.tsx b/lib/components/Balloon/Balloon.tsx index a3562cd..ad2cbdf 100644 --- a/lib/components/Balloon/Balloon.tsx +++ b/lib/components/Balloon/Balloon.tsx @@ -75,10 +75,10 @@ export class Balloon extends React.Component { }; } - UNSAFE_componentWillReceiveProps(newProps: BalloonProps) { - this.setState({ - visible: this.state.hovered || newProps.expanded, - }); + static getDerivedStateFromProps(props: BalloonProps, state: BalloonState): Partial | null { + return { + visible: state.hovered || props.expanded + }; } shouldComponentUpdate(newProps: BalloonProps, newState: BalloonState): boolean { diff --git a/lib/components/DateTime/Calendar.tsx b/lib/components/DateTime/Calendar.tsx index d4bda68..795268c 100644 --- a/lib/components/DateTime/Calendar.tsx +++ b/lib/components/DateTime/Calendar.tsx @@ -78,26 +78,24 @@ export class Calendar extends React.Component 0) { currentDate.year = props.year; } @@ -120,33 +118,29 @@ export class Calendar extends React.Component | null { + const date = state.currentDate.copy(); let update = false; - if (newProps.year !== this.props.year && newProps.year > 0) { - date.year = newProps.year; + + if (props.year > 0) { + date.year = props.year; update = true; } - if ( - typeof (newProps.month) === 'number' && - newProps.month !== this.props.month && - (newProps.month === 0 || newProps.month > 0) + + if (typeof (props.month) === 'number' && + (props.month === 0 || props.month > 0) ) { - date.month = newProps.month; + date.month = props.month; update = true; } - if (update && !this.state.detached && date.isValid()) { - this.setState({ currentDate: date }); - } - if (this.props.value !== newProps.value || this.props.localTimezone !== newProps.localTimezone) { - if (typeof (newProps.value) === 'string') { - this.value = MethodDate.fromString(newProps.localTimezone, newProps.value); - } else if (newProps.value) { - this.value = MethodDate.fromDate(newProps.localTimezone, newProps.value); - } else { - this.value = new MethodDate(newProps.localTimezone); - } + + if (update && !state.detached && date.isValid()) { + return { + currentDate: date + }; } + + return null; } componentDidUpdate() { @@ -283,6 +277,15 @@ export class Calendar extends React.Component { return ( @@ -351,9 +354,9 @@ export class Calendar extends React.Component | null { + return DatePicker.getInitialState(props, state.value, state.initialValue); } componentDidMount() { @@ -227,7 +217,7 @@ export class DatePicker extends React.Component | null { + return DateTimeField.getInitialState(props); } onDatePaste = (newDate: string): boolean => { diff --git a/lib/components/DateTime/TimeInput.tsx b/lib/components/DateTime/TimeInput.tsx index dc21426..237e030 100644 --- a/lib/components/DateTime/TimeInput.tsx +++ b/lib/components/DateTime/TimeInput.tsx @@ -88,10 +88,6 @@ export class TimeInput extends React.Component { } }; - private hours: FormOption[]; - private minutes: FormOption[]; - private seconds: FormOption[]; - private hourInput: HTMLSelectElement; private minuteInput: HTMLSelectElement; private secondInput: HTMLSelectElement; @@ -99,27 +95,11 @@ export class TimeInput extends React.Component { constructor(props: TimeInputProps) { super(props); - this.state = this.handleState(props); - - const numHours = props.militaryTime ? 24 : 12; - this.hours = []; - for (let index = 1; index < numHours; index++) { - const value = index < 10 ? `0${index}` : `${index}`; - this.hours.push({label: value, value: value}); - } - this.hours.push({label: '12', value: '00'}); - - this.minutes = []; - this.seconds = []; - for (let index = 0; index < 60; index++) { - const value = index < 10 ? `0${index}` : `${index}`; - this.minutes.push({label: value, value: value}); - this.seconds.push({label: value, value: value}); - } + this.state = TimeInput.handleState(props); } - handleState(props: TimeInputProps): TimeInputState { - const time = this.handleTimezone(props.value); + static handleState(props: TimeInputProps): TimeInputState { + const time = TimeInput.handleTimezone(props.value, props.localTimezone); const hoursTz = props.localTimezone ? time.getHours() : time.getUTCHours(); const hours = !isNaN(hoursTz) ? hoursTz : 0; @@ -135,11 +115,11 @@ export class TimeInput extends React.Component { }; } - handleTimezone(value: string | Date): Date { + static handleTimezone(value: string | Date, localTimezone: boolean): Date { let time; if (value) { time = typeof(value) === 'string' ? new Date(value) : value; - time = this.props.localTimezone + time = localTimezone ? time : new Date(Date.UTC( time.getUTCFullYear(), @@ -151,7 +131,7 @@ export class TimeInput extends React.Component { )); } else { time = new Date(); - if (this.props.localTimezone) { + if (localTimezone) { time = new Date( time.getFullYear(), time.getMonth(), @@ -170,41 +150,8 @@ export class TimeInput extends React.Component { return time; } - UNSAFE_componentWillReceiveProps(newProps) { - let newState: any = {}; - let update = false; - let newHours = this.state.hours; - if (newProps.value !== this.props.value || this.props.localTimezone !== newProps.localTimezone) { - newState = this.handleState(newProps); - newHours = newState.hours; - update = true; - } - - if (this.props.militaryTime !== newProps.militaryTime) { - const numHours = newProps.militaryTime ? 24 : 12; - this.hours = []; - for (let index = 0; index < numHours; index++) { - const value = index < 10 ? `0${index}` : `${index}`; - this.hours.push({label: value, value: value}); - } - - const hours = !newProps.militaryTime && newHours > 11 - ? newHours - 12 : newHours; - const period = newProps.militaryTime ? '24H' - : (newHours > 11 ? 'PM' : 'AM'); - - newState.hours = hours; - newState.period = period; - update = true; - } - - if (update) { - this.setState(newState); - } - } - update(name: 'hours' | 'minutes' | 'seconds' | 'period', value: string | number, period?: 'AM' | 'PM' | '24H') { - const date = this.handleTimezone(this.props.value); + const date = TimeInput.handleTimezone(this.props.value, this.props.localTimezone); const newState = {...this.state}; if (name !== 'period') { newState[name] = typeof(value) === 'string' ? parseInt(value) : value; @@ -248,23 +195,39 @@ export class TimeInput extends React.Component { this.setState(newState); } + private optionMap(option, attr) { + return ; + } + render() { const hours = this.state.hours < 10 ? `0${this.state.hours}` : this.state.hours; const minutes = this.state.minutes < 10 ? `0${this.state.minutes}` : this.state.minutes; const seconds = this.state.seconds < 10 ? `0${this.state.seconds}` : this.state.seconds; const inputClassName = css('time-input', {'error': this.props.error}, this.props.inputClassName); - const optionMap = (option, attr) => { - return ; - }; + const hoursList = []; + const minutesList = []; + const secondsList = []; + + const numHours = this.props.militaryTime ? 24 : 12; + for (let index = 0; index < numHours; index++) { + const value = index < 10 ? `0${index}` : `${index}`; + hoursList.push(this.optionMap({label: value, value: value}, this.props.attr.hourOption)); + } + + for (let index = 0; index < 60; index++) { + const value = index < 10 ? `0${index}` : `${index}`; + minutesList.push(this.optionMap({label: value, value: value}, this.props.attr.minuteOption)); + secondsList.push(this.optionMap({label: value, value: value}, this.props.attr.secondOption)); + } const period = this.props.militaryTime ? '' : { className={inputClassName} attr={this.props.attr.secondSelect} > - {this.seconds.map(option => - optionMap(option, this.props.attr.secondOption) - )} + {secondsList} ; return ( @@ -317,9 +278,7 @@ export class TimeInput extends React.Component { className={inputClassName} attr={this.props.attr.hourSelect} > - {this.hours.map(option => - optionMap(option, this.props.attr.hourOption) - )} + {hoursList} { className={inputClassName} attr={this.props.attr.minuteSelect} > - {this.minutes.map(option => - optionMap(option, this.props.attr.minuteOption) - )} + {minutesList} {secondsInput} {period} diff --git a/lib/components/Input/NumberInput.tsx b/lib/components/Input/NumberInput.tsx index 23aeb9d..4fd179a 100644 --- a/lib/components/Input/NumberInput.tsx +++ b/lib/components/Input/NumberInput.tsx @@ -81,7 +81,7 @@ export class NumberInput extends React.Component { @@ -168,7 +168,7 @@ export class NumberInput extends React.Component