Skip to content

Commit

Permalink
fix: 链式下拉组件不支持 valueField
Browse files Browse the repository at this point in the history
  • Loading branch information
jinye authored and CheshireJCat committed Dec 2, 2024
1 parent 7bb8ccd commit 397e609
Showing 1 changed file with 19 additions and 10 deletions.
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

0 comments on commit 397e609

Please sign in to comment.