kgabryje commented on a change in pull request #16196:
URL: https://github.com/apache/superset/pull/16196#discussion_r686950267
##########
File path:
superset-frontend/src/explore/components/controls/MetricControl/MetricsControl.jsx
##########
@@ -123,295 +116,233 @@ function coerceAdhocMetrics(value) {
});
}
-class MetricsControl extends React.PureComponent {
- constructor(props) {
- super(props);
- this.onChange = this.onChange.bind(this);
- this.onMetricEdit = this.onMetricEdit.bind(this);
- this.onNewMetric = this.onNewMetric.bind(this);
- this.onRemoveMetric = this.onRemoveMetric.bind(this);
- this.moveLabel = this.moveLabel.bind(this);
- this.checkIfAggregateInInput = this.checkIfAggregateInInput.bind(this);
- this.optionsForSelect = this.optionsForSelect.bind(this);
- this.selectFilterOption = this.selectFilterOption.bind(this);
- this.isAutoGeneratedMetric = this.isAutoGeneratedMetric.bind(this);
- this.optionRenderer = option => <MetricDefinitionOption option={option} />;
- this.valueRenderer = (option, index) => (
- <MetricDefinitionValue
- key={index}
- index={index}
- option={option}
- onMetricEdit={this.onMetricEdit}
- onRemoveMetric={this.onRemoveMetric}
- columns={this.props.columns}
- datasource={this.props.datasource}
- savedMetrics={this.props.savedMetrics}
- savedMetricsOptions={getOptionsForSavedMetrics(
- this.props.savedMetrics,
- this.props.value,
- this.props.value?.[index],
- )}
- datasourceType={this.props.datasourceType}
- onMoveLabel={this.moveLabel}
- onDropLabel={() => this.props.onChange(this.state.value)}
- multi={this.props.multi}
- />
- );
- this.select = null;
- this.selectRef = ref => {
- if (ref) {
- this.select = ref.select;
- } else {
- this.select = null;
- }
- };
- this.state = {
- aggregateInInput: null,
- options: this.optionsForSelect(this.props),
- value: coerceAdhocMetrics(this.props.value),
- };
- }
+const emptySavedMetric = { metric_name: '', expression: '' };
- UNSAFE_componentWillReceiveProps(nextProps) {
- const { value } = this.props;
- if (
- !isEqual(this.props.columns, nextProps.columns) ||
- !isEqual(this.props.savedMetrics, nextProps.savedMetrics)
- ) {
- this.setState({ options: this.optionsForSelect(nextProps) });
+const MetricsControl = ({
+ onChange,
+ multi,
+ value: propsValue,
+ columns,
+ savedMetrics,
+ datasource,
+ datasourceType,
+ ...props
+}) => {
+ const [value, setValue] = useState(coerceAdhocMetrics(propsValue));
+ const theme = useTheme();
- // Remove all metrics if selected value no longer a valid column
- // in the dataset. Must use `nextProps` here because Redux reducers may
- // have already updated the value for this control.
- if (!columnsContainAllMetrics(nextProps.value, nextProps)) {
- this.props.onChange([]);
+ const handleChange = useCallback(
+ opts => {
+ // if clear out options
+ if (opts === null) {
+ onChange(null);
+ return;
}
- }
- if (value !== nextProps.value) {
- this.setState({ value: coerceAdhocMetrics(nextProps.value) });
- }
- }
-
- onNewMetric(newMetric) {
- this.setState(
- prevState => ({
- ...prevState,
- value: [...prevState.value, newMetric],
- }),
- () => {
- this.onChange(this.state.value);
- },
- );
- }
- onMetricEdit(changedMetric, oldMetric) {
- this.setState(
- prevState => ({
- value: prevState.value.map(value => {
- if (
- // compare saved metrics
- value === oldMetric.metric_name ||
- // compare adhoc metrics
- typeof value.optionName !== 'undefined'
- ? value.optionName === oldMetric.optionName
- : false
- ) {
- return changedMetric;
+ let transformedOpts;
+ if (Array.isArray(opts)) {
+ transformedOpts = opts;
+ } else {
+ transformedOpts = opts ? [opts] : [];
+ }
Review comment:
Good point, done!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]