I'm trying to extend the Use a feature layer (function) sample to use more than one Feature Layer, but I can't seem to make the FieldSelector component cooperate. It seems like it working part of the way, but I have these issues:
- Whenever I select a field in a data source that is not the first in the list it will pop back to the initial selection. Logging props.useDataSources show that the select was made despite this.
- When I try to switch to a data source other than the initial after having made a selection it triggers the onChange parameter, which will wipe out my selected fields because it registers as nothing being selected.
- In addition to #2 usually when I try to switch the data source it won't actually change despite the above behavor.
I'm not sure if these issues are something I'm missing or if there is an issue in the API itself but the documentation doesn't give any example of using FieldSelector with more than one data source.
I haven't made many changes to the code, but here is my setting.tsx file:
import { React, Immutable, IMFieldSchema, UseDataSource, AllDataSourceTypes, DataSource, ImmutableObject } from 'jimu-core'
import { AllWidgetSettingProps } from 'jimu-for-builder'
import { DataSourceSelector, FieldSelector } from 'jimu-ui/advanced/data-source-selector'
export default function Setting (props: AllWidgetSettingProps<{}>) {
const onFieldChange = (allSelectedFields: IMFieldSchema[], ds: DataSource, isSelectedFromRepeatedDataSourceContext: boolean) => {
props.onSettingChange({
id: props.id,
useDataSources: [...props.useDataSources].map(item => item.dataSourceId === ds.id ? { ...item, ...{ fields: allSelectedFields.map(f => f.jimuName) } } : item)
})
}
const onToggleUseDataEnabled = (useDataSourcesEnabled: boolean) => {
props.onSettingChange({
id: props.id,
useDataSourcesEnabled
})
}
const selectedFields = () => {
let newFields = Immutable({}) as ImmutableObject<{ [dataSourceId: string]: string[] }>
props.useDataSources.forEach(ds => {
newFields = newFields.set(ds.dataSourceId, ds.fields ? ds.fields : [])
})
return newFields
}
const onDataSourceChange = (useDataSources: UseDataSource[]) => {
props.onSettingChange({
id: props.id,
useDataSources: useDataSources
})
}
return <div className="use-feature-layer-setting p-2">
<DataSourceSelector
types={Immutable([AllDataSourceTypes.FeatureLayer])}
useDataSources={props.useDataSources}
useDataSourcesEnabled={props.useDataSourcesEnabled}
onToggleUseDataEnabled={onToggleUseDataEnabled}
onChange={onDataSourceChange}
widgetId={props.id}
isMultiple={true}
/>
{
props.useDataSources && props.useDataSources.length > 0 &&
<FieldSelector
useDataSources={props.useDataSources}
onChange={onFieldChange}
selectedFields={selectedFields() || Immutable([])}
/>
}
</div>
}