Merge pull request #819 from RickCole21/master

nested-select 和 tree-select 补充 labelField 和 valueField
This commit is contained in:
liaoxuezhi 2020-08-05 19:34:58 +08:00 committed by GitHub
commit 802d3d3ad3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 26 deletions

View File

@ -450,7 +450,7 @@ a {
} }
table:not(.table) { table:not(.table) {
word-break: normal; word-break: break-word;
} }
} }
} }

View File

@ -203,7 +203,6 @@ export default class NestedSelectControl extends React.Component<
onChange, onChange,
selectedOptions, selectedOptions,
joinValues, joinValues,
valueField,
delimiter, delimiter,
extractValue, extractValue,
withChildren, withChildren,
@ -212,6 +211,8 @@ export default class NestedSelectControl extends React.Component<
} = this.props; } = this.props;
const {stack} = this.state; const {stack} = this.state;
let valueField = this.props.valueField || 'value';
if ( if (
!Array.isArray(option) && !Array.isArray(option) &&
option.children && option.children &&
@ -219,7 +220,7 @@ export default class NestedSelectControl extends React.Component<
typeof index === 'number' typeof index === 'number'
) { ) {
const checked = selectedOptions.some( const checked = selectedOptions.some(
o => o.value == (option as Option).value o => o[valueField] == (option as Option)[valueField]
); );
const uncheckable = cascade const uncheckable = cascade
? false ? false
@ -245,60 +246,61 @@ export default class NestedSelectControl extends React.Component<
newValue = items.length === option.length ? [] : option; newValue = items.length === option.length ? [] : option;
} else if (Array.isArray(option.children)) { } else if (Array.isArray(option.children)) {
if (cascade) { if (cascade) {
newValue = xorBy(items, [option], valueField || 'value'); newValue = xorBy(items, [option], valueField);
} else if (withChildren) { } else if (withChildren) {
option = flattenTree([option]); option = flattenTree([option]);
const fn = option.every( const fn = option.every(
(opt: Option) => !!~items.findIndex(item => item.value === opt.value) (opt: Option) =>
!!~items.findIndex(item => item[valueField] === opt[valueField])
) )
? xorBy ? xorBy
: unionBy; : unionBy;
newValue = fn(items, option as any, valueField || 'value'); newValue = fn(items, option as any, valueField);
} else { } else {
newValue = items.filter( newValue = items.filter(
item => item =>
!~flattenTree([option], i => (i as Option).value).indexOf( !~flattenTree([option], i => (i as Option)[valueField]).indexOf(
item.value item[valueField]
) )
); );
!~items.map(item => item.value).indexOf(option.value) && !~items.map(item => item[valueField]).indexOf(option[valueField]) &&
newValue.push(option); newValue.push(option);
} }
} else { } else {
newValue = xorBy(items, [option], valueField || 'value'); newValue = xorBy(items, [option], valueField);
} }
if (joinValues) { if (joinValues) {
newValue = (newValue as Options) newValue = (newValue as Options)
.map(item => item[valueField || 'value']) .map(item => item[valueField])
.join(delimiter || ','); .join(delimiter || ',');
} else if (extractValue) { } else if (extractValue) {
newValue = (newValue as Options).map(item => item[valueField || 'value']); newValue = (newValue as Options).map(item => item[valueField]);
} }
onChange(newValue); onChange(newValue);
} }
allChecked(options: Options): boolean { allChecked(options: Options): boolean {
const {selectedOptions, withChildren} = this.props; const {selectedOptions, withChildren, valueField} = this.props;
return options.every(option => { return options.every(option => {
if (withChildren && option.children) { if (withChildren && option.children) {
return this.allChecked(option.children); return this.allChecked(option.children);
} }
return selectedOptions.some( return selectedOptions.some(
selectedOption => selectedOption.value == option.value item => item[valueField || 'value'] == option[valueField || 'value']
); );
}); });
} }
partialChecked(options: Options): boolean { partialChecked(options: Options): boolean {
const {selectedOptions, withChildren} = this.props; const {selectedOptions, withChildren, valueField} = this.props;
return options.some(option => { return options.some(option => {
if (withChildren && option.children) { if (withChildren && option.children) {
return this.partialChecked(option.children); return this.partialChecked(option.children);
} }
return selectedOptions.some( 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, disabled,
searchable, searchable,
searchPromptText, searchPromptText,
translate: __ translate: __,
labelField
} = this.props; } = this.props;
const valueField = this.props.valueField || 'value';
const stack = this.state.stack; const stack = this.state.stack;
@ -443,7 +447,7 @@ export default class NestedSelectControl extends React.Component<
{options.map((option: Option, idx: number) => { {options.map((option: Option, idx: number) => {
const checked = selectedOptions.some( const checked = selectedOptions.some(
o => o.value == option.value o => o[valueField] == option[valueField]
); );
const selfChecked = !!option.uncheckable || checked; const selfChecked = !!option.uncheckable || checked;
let nodeDisabled = !!option.uncheckable || !!disabled; let nodeDisabled = !!option.uncheckable || !!disabled;
@ -452,7 +456,7 @@ export default class NestedSelectControl extends React.Component<
<div <div
key={idx} key={idx}
className={cx('NestedSelect-option', { className={cx('NestedSelect-option', {
'is-active': value && value === option.value 'is-active': value && value === option[valueField]
})} })}
onClick={this.handleOptionClick.bind(this, option)} onClick={this.handleOptionClick.bind(this, option)}
onMouseEnter={this.onMouseEnter.bind(this, option, index)} onMouseEnter={this.onMouseEnter.bind(this, option, index)}
@ -461,15 +465,15 @@ export default class NestedSelectControl extends React.Component<
<Checkbox <Checkbox
className={cx('NestedSelect-optionLabel')} className={cx('NestedSelect-optionLabel')}
onChange={this.handleCheck.bind(this, option, index)} onChange={this.handleCheck.bind(this, option, index)}
trueValue={option.value} trueValue={option[valueField]}
checked={selfChecked} checked={selfChecked}
disabled={nodeDisabled} disabled={nodeDisabled}
> >
{option.label} {option[labelField || 'label']}
</Checkbox> </Checkbox>
) : ( ) : (
<div className={cx('NestedSelect-optionLabel')}> <div className={cx('NestedSelect-optionLabel')}>
<span>{option.label}</span> <span>{option[labelField || 'label']}</span>
</div> </div>
)} )}
@ -496,11 +500,13 @@ export default class NestedSelectControl extends React.Component<
onMouseEnter(option: Option, index: number, e: MouseEvent) { onMouseEnter(option: Option, index: number, e: MouseEvent) {
let {stack} = this.state; let {stack} = this.state;
let {cascade, multiple, selectedOptions} = this.props; let {cascade, multiple, selectedOptions, valueField} = this.props;
index = index + 1; index = index + 1;
if (option.children && option.children.length) { 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 const uncheckable = cascade
? false ? false
: option.uncheckable || (multiple && checked); : option.uncheckable || (multiple && checked);

View File

@ -70,6 +70,7 @@ export default class TreeControl extends React.Component<TreeProps> {
onDelete, onDelete,
rootCreatable, rootCreatable,
rootCreateTip, rootCreateTip,
labelField,
translate: __ translate: __
} = this.props; } = this.props;
@ -79,6 +80,7 @@ export default class TreeControl extends React.Component<TreeProps> {
{loading ? null : ( {loading ? null : (
<TreeSelector <TreeSelector
classPrefix={ns} classPrefix={ns}
labelField={labelField}
valueField={valueField} valueField={valueField}
disabled={disabled} disabled={disabled}
onChange={onChange} onChange={onChange}
@ -100,7 +102,6 @@ export default class TreeControl extends React.Component<TreeProps> {
cascade={cascade} cascade={cascade}
foldedField="collapsed" foldedField="collapsed"
value={value || ''} value={value || ''}
labelField="label"
selfDisabledAffectChildren={false} selfDisabledAffectChildren={false}
onAdd={onAdd} onAdd={onAdd}
creatable={creatable} creatable={creatable}

View File

@ -382,6 +382,7 @@ export default class TreeSelectControl extends React.Component<
autoComplete, autoComplete,
maxLength, maxLength,
minLength, minLength,
labelField,
translate: __ translate: __
} = this.props; } = this.props;
@ -410,6 +411,7 @@ export default class TreeSelectControl extends React.Component<
<TreeSelector <TreeSelector
classPrefix={ns} classPrefix={ns}
onlyChildren={onlyChildren} onlyChildren={onlyChildren}
labelField={labelField}
valueField={valueField} valueField={valueField}
disabled={disabled} disabled={disabled}
onChange={this.handleChange} onChange={this.handleChange}
@ -431,7 +433,6 @@ export default class TreeSelectControl extends React.Component<
foldedField="collapsed" foldedField="collapsed"
hideRoot hideRoot
value={value || ''} value={value || ''}
labelField="label"
maxLength={maxLength} maxLength={maxLength}
minLength={minLength} minLength={minLength}
/> />