refactor(explore): remove side effect in render from CalendarFrame for DatePicker (#15147)

This commit is contained in:
Matan Borenkraout 2021-06-15 13:26:42 +03:00 committed by GitHub
parent 9c3c3fa125
commit 53225e8d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
import React from 'react'; import React, { useEffect } from 'react';
import { t } from '@superset-ui/core'; import { t } from '@superset-ui/core';
import { Radio } from 'src/components/Radio'; import { Radio } from 'src/components/Radio';
import { import {
@ -29,12 +29,15 @@ import {
FrameComponentProps, FrameComponentProps,
} from '../types'; } from '../types';
export function CalendarFrame(props: FrameComponentProps) { export function CalendarFrame({ onChange, value }: FrameComponentProps) {
let calendarRange = PreviousCalendarWeek; useEffect(() => {
if (CALENDAR_RANGE_SET.has(props.value as CalendarRangeType)) { if (!CALENDAR_RANGE_SET.has(value as CalendarRangeType)) {
calendarRange = props.value; onChange(PreviousCalendarWeek);
} else { }
props.onChange(calendarRange); }, [onChange, value]);
if (!CALENDAR_RANGE_SET.has(value as CalendarRangeType)) {
return null;
} }
return ( return (
@ -43,8 +46,8 @@ export function CalendarFrame(props: FrameComponentProps) {
{t('Configure Time Range: Previous...')} {t('Configure Time Range: Previous...')}
</div> </div>
<Radio.Group <Radio.Group
value={calendarRange} value={value}
onChange={(e: any) => props.onChange(e.target.value)} onChange={(e: any) => onChange(e.target.value)}
> >
{CALENDAR_RANGE_OPTIONS.map(({ value, label }) => ( {CALENDAR_RANGE_OPTIONS.map(({ value, label }) => (
<Radio key={value} value={value} className="vertical-radio"> <Radio key={value} value={value} className="vertical-radio">