Skip to content

Commit 06973f0

Browse files
author
Scott Watkins
committed
add simple-dropdown
1 parent 425ca58 commit 06973f0

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import { SimpleDropdown } from './simple-dropdown';
3+
import { StorybookAction, liveEdit, liveEditScope } from '../../../storybook-utils';
4+
5+
export default {
6+
title: 'Simple Dropdown',
7+
component: SimpleDropdown,
8+
decorators: [liveEditScope({ SimpleDropdown })]
9+
};
10+
11+
export const LiveEdit = (action: StorybookAction) => liveEdit(
12+
<div style={{ width: '100%' }}>
13+
<SimpleDropdown
14+
label="Fruits"
15+
options={["apple", "banana", "pear", "strawberries"]}
16+
onChange={event => action("Selected value is: " + event.target.value)(event)}
17+
/>
18+
</div>
19+
)(action);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@import "../../colors";
2+
3+
div.simple-dropdown {
4+
display: inline-block
5+
}
6+
7+
.simple-dropdown {
8+
background-color: $color-tw-blue-50;
9+
color: $color-tw-blue-700;
10+
font-size: 14px;
11+
font-family: Arial, Helvetica, sans-serif;
12+
13+
select {
14+
color: inherit;
15+
font: inherit;
16+
vertical-align: baseline;
17+
border: none;
18+
background-color: transparent;
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import { SimpleDropdown } from './simple-dropdown';
4+
5+
it('SimpleDropdown renders without crashing', () => {
6+
mount(
7+
(<SimpleDropdown
8+
label="Fruits"
9+
options={["apple", "banana", "strawberries"]}
10+
onChange={event => console.log("Selected value: `${event.target.value)`")}
11+
/>));
12+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import './simple-dropdown.styles.scss';
2+
import React, { useState, FC, ChangeEventHandler, ChangeEvent } from 'react';
3+
4+
5+
/**
6+
* SimpleDropdownProps
7+
*
8+
* @memberof SimpleDropdown
9+
*/
10+
interface SimpleDropdownProps {
11+
/**
12+
* Label displayed on the left of the drop down.
13+
*/
14+
label: string,
15+
/**
16+
* This is the list of values to be displayed on the Dropdown.
17+
*/
18+
options: string[],
19+
20+
/**
21+
* Event to fire when the value of the select element changes
22+
*/
23+
onChange?: ChangeEventHandler<HTMLSelectElement>
24+
};
25+
26+
/**
27+
* A Simple Dropdown Component
28+
*/
29+
export const SimpleDropdown: FC<SimpleDropdownProps> = (
30+
{
31+
onChange = event => { }, // default value is a no-op
32+
...props
33+
}
34+
) => {
35+
// Use State Hooks
36+
const [value, setValue] = useState(props.options.length > 0 ? props.options[0] : '');
37+
38+
// Handle Change Function
39+
const handleChange: ChangeEventHandler<HTMLSelectElement> = (event: ChangeEvent<HTMLSelectElement>) => {
40+
setValue(event.target.value);
41+
onChange(event);
42+
};
43+
44+
// Render
45+
return (
46+
<div className="simple-dropdown" >
47+
<label>
48+
{props.label}:
49+
<select value={value} onChange={handleChange}>
50+
{
51+
props.options.map(element =>
52+
(
53+
<option key={element} value={element}>{element}</option>
54+
)
55+
)
56+
}
57+
</select>
58+
</label >
59+
</div >
60+
)
61+
};

0 commit comments

Comments
 (0)