Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiSelect - Custom label #676

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/multiselect/MultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class MultiSelect extends Component {
style: null,
className: null,
scrollHeight: '200px',
label: null,
defaultLabel: 'Choose',
disabled: false,
filter: false,
Expand All @@ -38,6 +39,7 @@ export class MultiSelect extends Component {
style: PropTypes.object,
className: PropTypes.string,
scrollHeight: PropTypes.string,
label: PropTypes.string,
defaultLabel: PropTypes.string,
disabled: PropTypes.bool,
filter: PropTypes.bool,
Expand Down Expand Up @@ -346,7 +348,10 @@ export class MultiSelect extends Component {
}

renderLabel() {
if(this.props.selectedItemTemplate) {
if (this.props.label) {
return this.props.label;
}
else if(this.props.selectedItemTemplate) {
if(this.props.value && this.props.value.length) {
return this.props.value.map((val, index) => {
return (
Expand Down
37 changes: 33 additions & 4 deletions src/showcase/multiselect/MultiSelectDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export class MultiSelectDemo extends Component {
super();
this.state = {
cars1: [],
cars2: []
cars2: [],
cars3: []
};
this.carTemplate = this.carTemplate.bind(this);
this.selectedCarTemplate = this.selectedCarTemplate.bind(this);
Expand Down Expand Up @@ -72,7 +73,12 @@ export class MultiSelectDemo extends Component {

<h3>Templating</h3>
<MultiSelect value={this.state.cars2} options={cars} onChange={(e) => this.setState({cars2: e.value})}
style={{minWidth:'12em'}} filter={true} itemTemplate={this.carTemplate} selectedItemTemplate={this.selectedCarTemplate}/>
style={{minWidth:'12em'}} filter={true} itemTemplate={this.carTemplate} selectedItemTemplate={this.selectedCarTemplate}/>

<h3>Custom label</h3>
<MultiSelect value={this.state.cars3} options={cars} onChange={(e) => this.setState({cars3: e.value})}
label={this.state.cars3.length ? `${this.state.cars3.length} selected` : 'Choose'}
style={{minWidth: '12em'}} filter={true} itemTemplate={this.carTemplate} selectedItemTemplate={this.selectedCarTemplate} />
</div>

<MultiSelectDoc />
Expand Down Expand Up @@ -211,6 +217,17 @@ selectedCarTemplate(option) {
{`
<MultiSelect value={this.state.cars} options={cars} onChange={(e) => this.setState({cars: e.value})} filter={true}/>

`}
</CodeHighlight>

<h3>Custom label</h3>
<p>Label allows customize the label text instead of show the default label or the selected items. If label is not present defaultLabel and selections are shown.</p>

<CodeHighlight className="language-jsx">
{`
<MultiSelect value={this.state.cars} options={cars} onChange={(e) => this.setState({cars: e.value})}
label={this.state.cars.length ? \`\${this.state.cars.length} selected\` : 'Choose'}/>

`}
</CodeHighlight>

Expand Down Expand Up @@ -268,6 +285,12 @@ selectedCarTemplate(option) {
<td>200px</td>
<td>Height of the viewport in pixels, a scrollbar is defined if height of list exceeds this value.</td>
</tr>
<tr>
<td>label</td>
<td>string</td>
<td>null</td>
<td>Label to display instead of defaultLabel or selections.</td>
</tr>
<tr>
<td>defaultLabel</td>
<td>string</td>
Expand Down Expand Up @@ -415,7 +438,8 @@ export class MultiSelectDemo extends Component {
super();
this.state = {
cars1: [],
cars2: []
cars2: [],
cars3: []
};
this.carTemplate = this.carTemplate.bind(this);
this.selectedCarTemplate = this.selectedCarTemplate.bind(this);
Expand Down Expand Up @@ -477,7 +501,12 @@ export class MultiSelectDemo extends Component {

<h3>Templating</h3>
<MultiSelect value={this.state.cars2} options={cars} onChange={(e) => this.setState({cars2: e.value})}
style={{minWidth:'12em'}} filter={true} itemTemplate={this.carTemplate} selectedItemTemplate={this.selectedCarTemplate}/>
style={{minWidth:'12em'}} filter={true} itemTemplate={this.carTemplate} selectedItemTemplate={this.selectedCarTemplate}/>

<h3>Custom label</h3>
<MultiSelect value={this.state.cars3} options={cars} onChange={(e) => this.setState({cars3: e.value})}
label={this.state.cars3.length ? \`\${this.state.cars3.length} selected\` : 'Choose'}
style={{minWidth: '12em'}} filter={true} itemTemplate={this.carTemplate} selectedItemTemplate={this.selectedCarTemplate} />
</div>
</div>
);
Expand Down