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(explore): fix clearing select data causes popover dismiss #14221

Merged
merged 11 commits into from
May 20, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const propTypes = {
addAnnotationLayer: PropTypes.func,
removeAnnotationLayer: PropTypes.func,
close: PropTypes.func,

onPopoverClear: PropTypes.func,
};

const defaultProps = {
Expand All @@ -93,6 +95,7 @@ const defaultProps = {
addAnnotationLayer: () => {},
removeAnnotationLayer: () => {},
close: () => {},
onPopoverClear: () => {},
};

export default class AnnotationLayer extends React.PureComponent {
Expand Down Expand Up @@ -169,6 +172,7 @@ export default class AnnotationLayer extends React.PureComponent {
);
this.handleValue = this.handleValue.bind(this);
this.isValidForm = this.isValidForm.bind(this);
this.popoverClearWrapper = this.popoverClearWrapper.bind(this);
}

componentDidMount() {
Expand Down Expand Up @@ -238,6 +242,15 @@ export default class AnnotationLayer extends React.PureComponent {
return !errors.filter(x => x).length;
}

popoverClearWrapper(value, actionMeta, callback) {
if (callback) {
callback(value);
}
if (actionMeta?.action === 'clear') {
this.props.onPopoverClear(true);
}
}

handleAnnotationType(annotationType) {
this.setState({
annotationType,
Expand Down Expand Up @@ -409,7 +422,9 @@ export default class AnnotationLayer extends React.PureComponent {
options={valueOptions}
isLoading={isLoadingOptions}
value={value}
onChange={this.handleValue}
onChange={(value, _, actionMeta) =>
this.popoverClearWrapper(value, actionMeta, this.handleValue)
}
validationErrors={!value ? ['Mandatory'] : []}
optionRenderer={this.renderOption}
/>
Expand Down Expand Up @@ -490,7 +505,11 @@ export default class AnnotationLayer extends React.PureComponent {
validationErrors={!intervalEndColumn ? ['Mandatory'] : []}
options={columns}
value={intervalEndColumn}
onChange={v => this.setState({ intervalEndColumn: v })}
onChange={(value, _, actionMeta) =>
this.popoverClearWrapper(value, actionMeta, v =>
this.setState({ intervalEndColumn: v }),
)
}
/>
)}
<SelectControl
Expand All @@ -500,7 +519,11 @@ export default class AnnotationLayer extends React.PureComponent {
description="Pick a title for you annotation."
options={[{ value: '', label: 'None' }].concat(columns)}
value={titleColumn}
onChange={v => this.setState({ titleColumn: v })}
onChange={(value, _, actionMeta) =>
this.popoverClearWrapper(value, actionMeta, v =>
this.setState({ titleColumn: v }),
)
}
/>
{annotationType !== ANNOTATION_TYPES.TIME_SERIES && (
<SelectControl
Expand All @@ -512,7 +535,11 @@ export default class AnnotationLayer extends React.PureComponent {
multi
options={columns}
value={descriptionColumns}
onChange={v => this.setState({ descriptionColumns: v })}
onChange={(value, _, actionMeta) =>
this.popoverClearWrapper(value, actionMeta, v =>
this.setState({ descriptionColumns: v }),
)
}
/>
)}
<div style={{ marginTop: '1rem' }}>
Expand Down Expand Up @@ -628,7 +655,11 @@ export default class AnnotationLayer extends React.PureComponent {
{ value: 'opacityHigh', label: '0.8' },
]}
value={opacity}
onChange={v => this.setState({ opacity: v })}
onChange={(value, _, actionMeta) =>
this.popoverClearWrapper(value, actionMeta, v =>
this.setState({ opacity: v }),
)
}
/>
<div>
<ControlHeader label={t('Color')} />
Expand Down Expand Up @@ -734,7 +765,13 @@ export default class AnnotationLayer extends React.PureComponent {
name="annotation-source-type"
options={supportedSourceTypes}
value={sourceType}
onChange={this.handleAnnotationSourceType}
onChange={(value, _, actionMeta) =>
this.popoverClearWrapper(
value,
actionMeta,
this.handleAnnotationSourceType,
)
}
validationErrors={!sourceType ? [t('Mandatory')] : []}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ const defaultProps = {
class AnnotationLayerControl extends React.PureComponent {
constructor(props) {
super(props);
this.state = { popoverVisible: {}, addedAnnotationIndex: null };
this.state = {
popoverVisible: {},
addedAnnotationIndex: null,
popoverClear: false,
};
this.addAnnotationLayer = this.addAnnotationLayer.bind(this);
this.removeAnnotationLayer = this.removeAnnotationLayer.bind(this);
this.handleVisibleChange = this.handleVisibleChange.bind(this);
this.handlePopoverClear = this.handlePopoverClear.bind(this);
}

componentDidMount() {
Expand Down Expand Up @@ -100,9 +105,19 @@ class AnnotationLayerControl extends React.PureComponent {
}

handleVisibleChange(visible, popoverKey) {
this.setState(prevState => ({
popoverVisible: { ...prevState.popoverVisible, [popoverKey]: visible },
}));
if (!this.state.popoverClear) {
this.setState(prevState => ({
popoverVisible: { ...prevState.popoverVisible, [popoverKey]: visible },
}));
} else {
this.handlePopoverClear(false);
}
}

handlePopoverClear(popoverClear) {
this.setState({
popoverClear,
});
}

removeAnnotationLayer(annotation) {
Expand All @@ -128,6 +143,7 @@ class AnnotationLayerControl extends React.PureComponent {
this.handleVisibleChange(false, popoverKey);
this.setState({ addedAnnotationIndex: null });
}}
onPopoverClear={this.handlePopoverClear}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class SelectControl extends React.PureComponent {

// Beware: This is acting like an on-click instead of an on-change
// (firing every time user chooses vs firing only if a new option is chosen).
onChange(opt) {
onChange(opt, actionMeta) {
let optionValue = this.props.multi ? [] : null;
if (opt) {
if (this.props.multi) {
Expand All @@ -126,7 +126,7 @@ export default class SelectControl extends React.PureComponent {
}
}
// will eventually call `exploreReducer`: SET_FIELD_VALUE
this.props.onChange(optionValue);
this.props.onChange(optionValue, [], actionMeta);
}

getSelectRef(instance) {
Expand Down