-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Select component refactoring - SelectAsyncControl - Iteration 5 (
#16609) * Refactor Select * Implement onError * Separate async request * Lint * Allow clear * Improve type * Reconcile with Select latest changes * Fix handleOnChange * Clean up * Add placeholder * Accept null values * Update superset-frontend/src/explore/components/controls/SelectAsyncControl/index.tsx Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com> * Clean up * Implement type guard * Fix lint * Catch error within loadOptions Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
127 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
superset-frontend/src/explore/components/controls/SelectAsyncControl/index.jsx
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
superset-frontend/src/explore/components/controls/SelectAsyncControl/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React, { useEffect, useState } from 'react'; | ||
import { t, SupersetClient } from '@superset-ui/core'; | ||
import ControlHeader from 'src/explore/components/ControlHeader'; | ||
import { Select } from 'src/components'; | ||
import { SelectProps, OptionsType } from 'src/components/Select/Select'; | ||
import { SelectValue, LabeledValue } from 'antd/lib/select'; | ||
import withToasts from 'src/components/MessageToasts/withToasts'; | ||
import { getClientErrorObject } from 'src/utils/getClientErrorObject'; | ||
|
||
type SelectAsyncProps = Omit<SelectProps, 'options' | 'ariaLabel' | 'onChange'>; | ||
|
||
interface SelectAsyncControlProps extends SelectAsyncProps { | ||
addDangerToast: (error: string) => void; | ||
ariaLabel?: string; | ||
dataEndpoint: string; | ||
default?: SelectValue; | ||
mutator?: (response: Record<string, any>) => OptionsType; | ||
multi?: boolean; | ||
onChange: (val: SelectValue) => void; | ||
// ControlHeader related props | ||
description?: string; | ||
hovered?: boolean; | ||
label?: string; | ||
} | ||
|
||
function isLabeledValue(arg: any): arg is LabeledValue { | ||
return arg.value !== undefined; | ||
} | ||
|
||
const SelectAsyncControl = ({ | ||
addDangerToast, | ||
allowClear = true, | ||
ariaLabel, | ||
dataEndpoint, | ||
multi = true, | ||
mutator, | ||
onChange, | ||
placeholder, | ||
value, | ||
...props | ||
}: SelectAsyncControlProps) => { | ||
const [options, setOptions] = useState<OptionsType>([]); | ||
|
||
const handleOnChange = (val: SelectValue) => { | ||
let onChangeVal = val; | ||
if (Array.isArray(val)) { | ||
const values = val.map(v => (isLabeledValue(v) ? v.value : v)); | ||
onChangeVal = values; | ||
} | ||
if (isLabeledValue(val)) { | ||
onChangeVal = val.value; | ||
} | ||
onChange(onChangeVal); | ||
}; | ||
|
||
useEffect(() => { | ||
const onError = (response: Response) => | ||
getClientErrorObject(response).then(e => { | ||
const { error } = e; | ||
addDangerToast(`${t('Error while fetching data')}: ${error}`); | ||
}); | ||
const loadOptions = () => | ||
SupersetClient.get({ | ||
endpoint: dataEndpoint, | ||
}) | ||
.then(response => { | ||
const data = mutator ? mutator(response.json) : response.json.result; | ||
setOptions(data); | ||
}) | ||
.catch(onError); | ||
loadOptions(); | ||
}, [addDangerToast, dataEndpoint, mutator]); | ||
|
||
return ( | ||
<Select | ||
allowClear={allowClear} | ||
ariaLabel={ariaLabel || t('Select ...')} | ||
value={value || (props.default !== undefined ? props.default : undefined)} | ||
header={<ControlHeader {...props} />} | ||
mode={multi ? 'multiple' : 'single'} | ||
onChange={handleOnChange} | ||
options={options} | ||
placeholder={placeholder} | ||
/> | ||
); | ||
}; | ||
|
||
export default withToasts(SelectAsyncControl); |