File size: 6,670 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { PureComponent } from 'react';
import SelectDropdown from '..';
import Gridicon from '../../gridicon';

class SelectDropdownExample extends PureComponent {
	static displayName = 'SelectDropdownExample';

	static defaultProps = {
		options: [
			{ value: 'status-options', label: 'Statuses', isLabel: true },
			{ value: 'published', label: 'Published', count: 12 },
			{ value: 'scheduled', label: 'Scheduled' },
			{ value: 'drafts', label: 'Drafts' },
			null,
			{ value: 'trashed', label: 'Trashed' },
		],
	};

	state = {
		childSelected: 'Published',
		selectedCount: 10,
		compactButtons: false,
		selectedIcon: <Gridicon icon="align-image-left" size={ 18 } />,
	};

	toggleButtons = () => {
		this.setState( { compactButtons: ! this.state.compactButtons } );
	};

	render() {
		const toggleButtonsText = this.state.compactButtons ? 'Normal Buttons' : 'Compact Buttons';

		return (
			<div className="docs__select-dropdown-container">
				<button className="docs__design-toggle button" onClick={ this.toggleButtons }>
					{ toggleButtonsText }
				</button>

				<h3>Items passed as options prop</h3>
				<SelectDropdown
					compact={ this.state.compactButtons }
					options={ this.props.options }
					onSelect={ this.onDropdownSelect }
				/>

				<h3 style={ { marginTop: 20 } }>Items passed as children</h3>
				<SelectDropdown
					compact={ this.state.compactButtons }
					onSelect={ this.onDropdownSelect }
					selectedText={ this.state.childSelected }
					selectedCount={ this.state.selectedCount }
				>
					<SelectDropdown.Label>
						<strong>Statuses</strong>
					</SelectDropdown.Label>

					<SelectDropdown.Item
						count={ 10 }
						selected={ this.state.childSelected === 'Published' }
						onClick={ this.getSelectItemHandler( 'Published', 10 ) }
					>
						Published
					</SelectDropdown.Item>

					<SelectDropdown.Item
						count={ 4 }
						selected={ this.state.childSelected === 'Scheduled' }
						onClick={ this.getSelectItemHandler( 'Scheduled', 4 ) }
					>
						Scheduled
					</SelectDropdown.Item>

					<SelectDropdown.Item
						count={ 3343 }
						selected={ this.state.childSelected === 'Drafts' }
						onClick={ this.getSelectItemHandler( 'Drafts', 3343 ) }
					>
						Drafts
					</SelectDropdown.Item>

					<SelectDropdown.Separator />

					<SelectDropdown.Item
						count={ 3 }
						selected={ this.state.childSelected === 'Trashed' }
						onClick={ this.getSelectItemHandler( 'Trashed', 3 ) }
					>
						Trashed
					</SelectDropdown.Item>
				</SelectDropdown>

				<h3 style={ { marginTop: 20 } }>With Icons in Items Passed as Options</h3>
				<SelectDropdown
					compact={ this.state.compactButtons }
					onSelect={ this.onDropdownSelect }
					options={ [
						{
							value: 'published',
							label: 'Published',
							icon: <Gridicon icon="align-image-left" size={ 18 } />,
						},
						{
							value: 'scheduled',
							label: 'Scheduled',
							icon: <Gridicon icon="calendar" size={ 18 } />,
						},
						{
							value: 'drafts',
							label: 'Drafts',
							icon: <Gridicon icon="create" size={ 18 } />,
						},
						{
							value: 'trashed',
							label: 'Trashed',
							icon: <Gridicon icon="trash" size={ 18 } />,
						},
					] }
				/>

				<h3 style={ { marginTop: 20 } }>With Icons in Items Passed as Children</h3>
				<SelectDropdown
					compact={ this.state.compactButtons }
					onSelect={ this.onDropdownSelect }
					selectedText={ this.state.childSelected }
					selectedIcon={ this.state.selectedIcon }
				>
					<SelectDropdown.Label>
						<strong>Statuses</strong>
					</SelectDropdown.Label>

					<SelectDropdown.Item
						selected={ this.state.childSelected === 'Published' }
						icon={ <Gridicon icon="align-image-left" size={ 18 } /> }
						onClick={ this.getSelectItemHandler(
							'Published',
							10,
							<Gridicon icon="align-image-left" size={ 18 } />
						) }
					>
						Published
					</SelectDropdown.Item>

					<SelectDropdown.Item
						selected={ this.state.childSelected === 'Scheduled' }
						icon={ <Gridicon icon="calendar" size={ 18 } /> }
						onClick={ this.getSelectItemHandler(
							'Scheduled',
							4,
							<Gridicon icon="calendar" size={ 18 } />
						) }
					>
						Scheduled
					</SelectDropdown.Item>

					<SelectDropdown.Item
						selected={ this.state.childSelected === 'Drafts' }
						icon={ <Gridicon icon="create" size={ 18 } /> }
						onClick={ this.getSelectItemHandler(
							'Drafts',
							3343,
							<Gridicon icon="create" size={ 18 } />
						) }
					>
						Drafts
					</SelectDropdown.Item>

					<SelectDropdown.Separator />

					<SelectDropdown.Item
						selected={ this.state.childSelected === 'Trashed' }
						icon={ <Gridicon icon="trash" size={ 18 } /> }
						onClick={ this.getSelectItemHandler(
							'Trashed',
							3,
							<Gridicon icon="trash" size={ 18 } />
						) }
					>
						Trashed
					</SelectDropdown.Item>
				</SelectDropdown>

				<h3 style={ { marginTop: 20 } }>max-width: 220px;</h3>
				<SelectDropdown
					className="select-dropdown-example__fixed-width"
					compact={ this.state.compactButtons }
					onSelect={ this.onDropdownSelect }
					selectedText="Published publish publish publish"
					selectedCount={ 10 }
				>
					<SelectDropdown.Label>
						<strong>Statuses</strong>
					</SelectDropdown.Label>
					<SelectDropdown.Item count={ 10 } selected>
						Published publish publish publish
					</SelectDropdown.Item>
					<SelectDropdown.Item count={ 4 }> Scheduled scheduled</SelectDropdown.Item>
					<SelectDropdown.Item count={ 3343 }>Drafts</SelectDropdown.Item>
					<SelectDropdown.Item disabled>Disabled Item</SelectDropdown.Item>
					<SelectDropdown.Separator />
					<SelectDropdown.Item count={ 3 }>Trashed</SelectDropdown.Item>
				</SelectDropdown>

				<h3 style={ { marginTop: 20 } }>Disabled State</h3>
				<SelectDropdown
					compact={ this.state.compactButtons }
					options={ this.props.options }
					onSelect={ this.onDropdownSelect }
					disabled
				/>
			</div>
		);
	}

	getSelectItemHandler = ( name, count, icon ) => {
		return ( event ) => {
			event.preventDefault();
			this.selectItem( name, count, icon );
		};
	};

	selectItem = ( childSelected, count, icon ) => {
		this.setState( {
			childSelected: childSelected,
			selectedCount: count,
			selectedIcon: icon,
		} );

		// eslint-disable-next-line no-console
		console.log( 'Select Dropdown Item (selected):', childSelected );
	};

	onDropdownSelect( option ) {
		// eslint-disable-next-line no-console
		console.log( 'Select Dropdown (selected):', option );
	}
}

export default SelectDropdownExample;