From 818ba6f347dba67e0b35e7505d00716c25e8fbc2 Mon Sep 17 00:00:00 2001 From: jinye Date: Fri, 29 Nov 2024 17:32:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=93=BE=E5=BC=8F=E4=B8=8B=E6=8B=89?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E4=B8=8D=E6=94=AF=E6=8C=81=20valueField?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amis/src/renderers/Form/ChainedSelect.tsx | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/amis/src/renderers/Form/ChainedSelect.tsx b/packages/amis/src/renderers/Form/ChainedSelect.tsx index f95ea9e7c7a..7d0396d4910 100644 --- a/packages/amis/src/renderers/Form/ChainedSelect.tsx +++ b/packages/amis/src/renderers/Form/ChainedSelect.tsx @@ -105,7 +105,7 @@ export default class ChainedSelectControl extends React.Component< } array2value(arr: Array, isExtracted: boolean = false) { - const {delimiter, joinValues, extractValue} = this.props; + const {delimiter, joinValues, extractValue, valueField} = this.props; // 判断arr的项是否已抽取 return isExtracted ? joinValues @@ -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; } @@ -125,6 +125,7 @@ export default class ChainedSelectControl extends React.Component< onChange, joinValues, extractValue, + valueField, source, data, env, @@ -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++; } @@ -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({ @@ -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 || @@ -234,6 +240,7 @@ export default class ChainedSelectControl extends React.Component< joinValues, extractValue, dispatchEvent, + valueField, data } = this.props; @@ -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); } @@ -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, @@ -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(' > ');