From c111c1cf646e84ba1b6c0276d198cf9333a6792a Mon Sep 17 00:00:00 2001 From: rickcole Date: Tue, 4 Aug 2020 20:40:16 +0800 Subject: [PATCH 1/2] https://github.com/baidu/amis/issues/817 --- src/renderers/Form/NestedSelect.tsx | 52 ++++++++++++++++------------- src/renderers/Form/Tree.tsx | 3 +- src/renderers/Form/TreeSelect.tsx | 3 +- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/renderers/Form/NestedSelect.tsx b/src/renderers/Form/NestedSelect.tsx index 3f3a2fcf7..f1bcb353e 100644 --- a/src/renderers/Form/NestedSelect.tsx +++ b/src/renderers/Form/NestedSelect.tsx @@ -203,7 +203,6 @@ export default class NestedSelectControl extends React.Component< onChange, selectedOptions, joinValues, - valueField, delimiter, extractValue, withChildren, @@ -212,6 +211,8 @@ export default class NestedSelectControl extends React.Component< } = this.props; const {stack} = this.state; + let valueField = this.props.valueField || 'value'; + if ( !Array.isArray(option) && option.children && @@ -219,7 +220,7 @@ export default class NestedSelectControl extends React.Component< typeof index === 'number' ) { const checked = selectedOptions.some( - o => o.value == (option as Option).value + o => o[valueField] == (option as Option)[valueField] ); const uncheckable = cascade ? false @@ -245,60 +246,61 @@ export default class NestedSelectControl extends React.Component< newValue = items.length === option.length ? [] : option; } else if (Array.isArray(option.children)) { if (cascade) { - newValue = xorBy(items, [option], valueField || 'value'); + newValue = xorBy(items, [option], valueField); } else if (withChildren) { option = flattenTree([option]); const fn = option.every( - (opt: Option) => !!~items.findIndex(item => item.value === opt.value) + (opt: Option) => + !!~items.findIndex(item => item[valueField] === opt[valueField]) ) ? xorBy : unionBy; - newValue = fn(items, option as any, valueField || 'value'); + newValue = fn(items, option as any, valueField); } else { newValue = items.filter( item => - !~flattenTree([option], i => (i as Option).value).indexOf( - item.value + !~flattenTree([option], i => (i as Option)[valueField]).indexOf( + item[valueField] ) ); - !~items.map(item => item.value).indexOf(option.value) && + !~items.map(item => item[valueField]).indexOf(option[valueField]) && newValue.push(option); } } else { - newValue = xorBy(items, [option], valueField || 'value'); + newValue = xorBy(items, [option], valueField); } if (joinValues) { newValue = (newValue as Options) - .map(item => item[valueField || 'value']) + .map(item => item[valueField]) .join(delimiter || ','); } else if (extractValue) { - newValue = (newValue as Options).map(item => item[valueField || 'value']); + newValue = (newValue as Options).map(item => item[valueField]); } onChange(newValue); } allChecked(options: Options): boolean { - const {selectedOptions, withChildren} = this.props; + const {selectedOptions, withChildren, valueField} = this.props; return options.every(option => { if (withChildren && option.children) { return this.allChecked(option.children); } return selectedOptions.some( - selectedOption => selectedOption.value == option.value + item => item[valueField || 'value'] == option[valueField || 'value'] ); }); } partialChecked(options: Options): boolean { - const {selectedOptions, withChildren} = this.props; + const {selectedOptions, withChildren, valueField} = this.props; return options.some(option => { if (withChildren && option.children) { return this.partialChecked(option.children); } return selectedOptions.some( - selectedOption => selectedOption.value == option.value + item => item[valueField || 'value'] == option[valueField || 'value'] ); }); } @@ -394,8 +396,10 @@ export default class NestedSelectControl extends React.Component< disabled, searchable, searchPromptText, - translate: __ + translate: __, + labelField } = this.props; + const valueField = this.props.valueField || 'value'; const stack = this.state.stack; @@ -443,7 +447,7 @@ export default class NestedSelectControl extends React.Component< {options.map((option: Option, idx: number) => { const checked = selectedOptions.some( - o => o.value == option.value + o => o[valueField] == option[valueField] ); const selfChecked = !!option.uncheckable || checked; let nodeDisabled = !!option.uncheckable || !!disabled; @@ -452,7 +456,7 @@ export default class NestedSelectControl extends React.Component<
- {option.label} + {option[labelField || 'label']} ) : (
- {option.label} + {option[labelField || 'label']}
)} @@ -496,11 +500,13 @@ export default class NestedSelectControl extends React.Component< onMouseEnter(option: Option, index: number, e: MouseEvent) { let {stack} = this.state; - let {cascade, multiple, selectedOptions} = this.props; + let {cascade, multiple, selectedOptions, valueField} = this.props; index = index + 1; if (option.children && option.children.length) { - const checked = selectedOptions.some(o => o.value == option.value); + const checked = selectedOptions.some( + o => o[valueField || 'value'] == option[valueField || 'value'] + ); const uncheckable = cascade ? false : option.uncheckable || (multiple && checked); diff --git a/src/renderers/Form/Tree.tsx b/src/renderers/Form/Tree.tsx index d1b811472..3b00292e8 100644 --- a/src/renderers/Form/Tree.tsx +++ b/src/renderers/Form/Tree.tsx @@ -70,6 +70,7 @@ export default class TreeControl extends React.Component { onDelete, rootCreatable, rootCreateTip, + labelField, translate: __ } = this.props; @@ -79,6 +80,7 @@ export default class TreeControl extends React.Component { {loading ? null : ( { cascade={cascade} foldedField="collapsed" value={value || ''} - labelField="label" selfDisabledAffectChildren={false} onAdd={onAdd} creatable={creatable} diff --git a/src/renderers/Form/TreeSelect.tsx b/src/renderers/Form/TreeSelect.tsx index 3da3833c9..0d076df3a 100644 --- a/src/renderers/Form/TreeSelect.tsx +++ b/src/renderers/Form/TreeSelect.tsx @@ -382,6 +382,7 @@ export default class TreeSelectControl extends React.Component< autoComplete, maxLength, minLength, + labelField, translate: __ } = this.props; @@ -410,6 +411,7 @@ export default class TreeSelectControl extends React.Component< From c03305abddaa2124ed32650b14231b856a0ed97d Mon Sep 17 00:00:00 2001 From: rickcole Date: Wed, 5 Aug 2020 11:25:06 +0800 Subject: [PATCH 2/2] fix style --- examples/style.scss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/style.scss b/examples/style.scss index 4b239d56d..92357d32c 100644 --- a/examples/style.scss +++ b/examples/style.scss @@ -447,7 +447,7 @@ a { } table:not(.table) { - word-break: normal; + word-break: break-word; } } }