File tree Expand file tree Collapse file tree 2 files changed +42
-6
lines changed Expand file tree Collapse file tree 2 files changed +42
-6
lines changed Original file line number Diff line number Diff line change @@ -158,6 +158,30 @@ export default class Tags extends Component {
158158 ) ;
159159 }
160160
161+ renderOptGroups ( ) {
162+ return (
163+ < div >
164+ Option Groups< br />
165+ < Select2
166+ multiple
167+ defaultValue = { [ 1 , 4 ] }
168+ data = { [
169+ { text : 'Development' ,
170+ children : [
171+ { text : 'bug' , id : 1 } ,
172+ { text : 'feature' , id : 2 } ] } ,
173+ { text : 'documents' , id : 3 } ,
174+ { text : 'discussion' , id : 4 } ,
175+ ] }
176+ options = { {
177+ placeholder : 'search by tags' ,
178+ } }
179+ />
180+ </ div >
181+ ) ;
182+ }
183+
184+
161185 render ( ) {
162186 return (
163187 < div >
@@ -172,7 +196,9 @@ export default class Tags extends Component {
172196 { /* example 5 */ }
173197 { this . renderDefaultMultipleValue ( ) } < br />
174198 { /* example 6 */ }
175- { this . renderDynamicUpdateData ( ) }
199+ { this . renderDynamicUpdateData ( ) } < br />
200+ { /* example 7 */ }
201+ { this . renderOptGroups ( ) }
176202 </ div >
177203 ) ;
178204 }
Original file line number Diff line number Diff line change @@ -112,18 +112,28 @@ export default class Select2 extends Component {
112112 const type = typeof value ;
113113 return type === 'function' || ( value && type === 'object' ) || false ;
114114 }
115+ makeOption ( item , k ) {
116+ if ( this . isObject ( item ) ) {
117+ const { id, text, ...itemParams } = item ;
118+ return ( < option key = { `option-${ k } ` } value = { id } { ...itemParams } > { text } </ option > ) ;
119+ }
120+
121+ return ( < option key = { `option-${ k } ` } value = { item } > { item } </ option > ) ;
122+ }
123+
115124
116125 render ( ) {
117126 const { data, value, ...params } = this . props ;
118127 return (
119128 < select { ...params } >
120129 { data . map ( ( item , k ) => {
121- if ( this . isObject ( item ) ) {
122- const { id, text, ...itemParams } = item ;
123- return ( < option key = { `option-${ k } ` } value = { id } { ...itemParams } > { text } </ option > ) ;
130+ if ( this . isObject ( item ) && this . isObject ( item . children ) ) {
131+ const { children, text, ...itemParams } = item ;
132+ return ( < optgroup key = { `optgroup-${ k } ` } label = { text } { ...itemParams } >
133+ { children . map ( ( child , k2 ) => this . makeOption ( child , `${ k } -${ k2 } ` ) ) }
134+ </ optgroup > ) ;
124135 }
125-
126- return ( < option key = { `option-${ k } ` } value = { item } > { item } </ option > ) ;
136+ return this . makeOption ( item , k ) ;
127137 } ) }
128138 </ select >
129139 ) ;
You can’t perform that action at this time.
0 commit comments