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: 链式下拉组件不支持 valueField #11293

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions packages/amis/src/renderers/Form/ChainedSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class ChainedSelectControl extends React.Component<
}

array2value(arr: Array<any>, isExtracted: boolean = false) {
const {delimiter, joinValues, extractValue} = this.props;
const {delimiter, joinValues, extractValue, valueField} = this.props;
// 判断arr的项是否已抽取
return isExtracted
? joinValues
Expand All @@ -114,7 +114,7 @@ export default class ChainedSelectControl extends React.Component<
: joinValues
? arr.join(delimiter || ',')
: extractValue
? arr.map(item => item.value || item)
? arr.map(item => item[valueField || 'value'] || item)
: arr;
}

Expand All @@ -125,6 +125,7 @@ export default class ChainedSelectControl extends React.Component<
onChange,
joinValues,
extractValue,
valueField,
source,
data,
env,
Expand All @@ -142,7 +143,9 @@ export default class ChainedSelectControl extends React.Component<
idx < len &&
arr[idx] &&
this.state.stack[idx].parentId ==
(joinValues || extractValue ? arr[idx] : arr[idx].value)
(joinValues || extractValue
? arr[idx]
: arr[idx][valueField || 'value'])
) {
idx++;
}
Expand All @@ -151,7 +154,8 @@ export default class ChainedSelectControl extends React.Component<
return;
}

const parentId = joinValues || extractValue ? arr[idx] : arr[idx].value;
const parentId =
joinValues || extractValue ? arr[idx] : arr[idx][valueField || 'value'];
const stack = this.state.stack.concat();
stack.splice(idx, stack.length - idx);
stack.push({
Expand All @@ -177,7 +181,9 @@ export default class ChainedSelectControl extends React.Component<
// todo 没有检测 response.ok

const stack = this.state.stack.concat();
const remoteValue = ret.data ? ret.data.value : undefined;
const remoteValue = ret.data
? ret.data[valueField || 'value']
: undefined;
let options =
ret?.data?.options ||
ret?.data?.items ||
Expand Down Expand Up @@ -234,6 +240,7 @@ export default class ChainedSelectControl extends React.Component<
joinValues,
extractValue,
dispatchEvent,
valueField,
data
} = this.props;

Expand All @@ -244,7 +251,9 @@ export default class ChainedSelectControl extends React.Component<
: [];
arr.splice(index, arr.length - index);

const pushValue = joinValues ? currentValue.value : currentValue;
const pushValue = joinValues
? currentValue[valueField || 'value']
: currentValue;
if (pushValue !== undefined) {
arr.push(pushValue);
}
Expand All @@ -271,8 +280,8 @@ export default class ChainedSelectControl extends React.Component<
renderStatic(displayValue = '-') {
const {
options = [],
labelField = 'label',
valueField = 'value',
labelField,
valueField,
classPrefix,
classnames: cx,
className,
Expand All @@ -298,8 +307,8 @@ export default class ChainedSelectControl extends React.Component<
return value;
}
const selectedOption =
find(options, o => value === o[valueField]) || {};
return selectedOption[labelField] ?? value;
find(options, o => value === o[valueField || 'value']) || {};
return selectedOption[labelField || 'label'] ?? value;
})
.filter(v => v != null)
.join(' > ');
Expand Down
Loading