From 1c3c944ff039c0a00168d713468dbab0bdf6b4c4 Mon Sep 17 00:00:00 2001 From: jiangweiliu Date: Wed, 28 Feb 2018 16:48:17 +0800 Subject: [PATCH] passing labelKey and valueKey into optionRenderer and valueRenderer --- examples/src/components/CustomRender.js | 8 ++++---- src/Select.js | 10 +++++++--- src/utils/defaultMenuRenderer.js | 9 ++++++++- test/Select-test.js | 14 +++++++------- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/examples/src/components/CustomRender.js b/examples/src/components/CustomRender.js index 0a744a83ab..4c67b1a010 100644 --- a/examples/src/components/CustomRender.js +++ b/examples/src/components/CustomRender.js @@ -21,16 +21,16 @@ var DisabledUpsellOptions = createClass({ renderLink: function() { return Upgrade here!; }, - renderOption: function(option) { + renderOption: function({ option, labelKey }) { return ( ); }, - renderValue: function(option) { - return {option.label}; + renderValue: function({ option, labelKey }) { + return {option[labelKey]}; }, render: function() { var options = [ diff --git a/src/Select.js b/src/Select.js index d8b8542d20..583fd21f38 100644 --- a/src/Select.js +++ b/src/Select.js @@ -568,7 +568,7 @@ class Select extends React.Component { } getOptionLabel (op) { - return op[this.props.labelKey]; + return op.option[this.props.labelKey]; } /** @@ -815,6 +815,8 @@ class Select extends React.Component { return showPlaceholder ?
{this.props.placeholder}
: null; } let onClick = this.props.onValueClick ? this.handleValueClick : null; + const { labelKey, valueKey } = this.props; + if (this.props.multi) { return valueArray.map((value, i) => { return ( @@ -827,8 +829,10 @@ class Select extends React.Component { onRemove={this.removeValue} placeholder={this.props.placeholder} value={value} + valueKey + labelKey > - {renderLabel(value, i)} + {renderLabel({option: value, index: i, labelKey, valueKey})}   ); @@ -844,7 +848,7 @@ class Select extends React.Component { placeholder={this.props.placeholder} value={valueArray[0]} > - {renderLabel(valueArray[0])} + {renderLabel({option: valueArray[0], labelKey, valueKey})} ); } diff --git a/src/utils/defaultMenuRenderer.js b/src/utils/defaultMenuRenderer.js index d2fd6494ac..3c33efc724 100644 --- a/src/utils/defaultMenuRenderer.js +++ b/src/utils/defaultMenuRenderer.js @@ -18,6 +18,7 @@ const menuRenderer = ({ selectValue, valueArray, valueKey, + labelKey, }) => { let Option = optionComponent; @@ -49,7 +50,13 @@ const menuRenderer = ({ removeValue={removeValue} selectValue={selectValue} > - {optionRenderer(option, i, inputValue)} + {optionRenderer({ + option, + inputValue, + labelKey, + valueKey, + index: i, + })} ); }); diff --git a/test/Select-test.js b/test/Select-test.js index 946b1f7858..fb03d781ee 100644 --- a/test/Select-test.js +++ b/test/Select-test.js @@ -3580,9 +3580,9 @@ describe('Select', () => { beforeEach(() => { - optionRenderer = (option) => { + optionRenderer = ({ option, labelKey, valueKey }) => { return ( - {option.label.toUpperCase()} + {option[labelKey].toUpperCase()} ); }; @@ -3655,7 +3655,7 @@ describe('Select', () => { beforeEach(() => { window.location.href = startUrl; - optionRenderer = (option) => { + optionRenderer = ({ option }) => { return ( {option.label} {option.link} ); @@ -3958,9 +3958,9 @@ describe('Select', () => { beforeEach(() => { - valueRenderer = (option) => { + valueRenderer = ({ option, labelKey, valueKey }) => { return ( - {option.label.toUpperCase()} + {option[labelKey].toUpperCase()} ); }; @@ -3989,9 +3989,9 @@ describe('Select', () => { beforeEach(() => { - valueRenderer = (option) => { + valueRenderer = ({ option, labelKey, valueKey }) => { return ( - {option.label.toUpperCase()} + {option[labelKey].toUpperCase()} ); };