Skip to content
This repository was archived by the owner on Feb 15, 2019. It is now read-only.

Commit 3223d2c

Browse files
committed
feat: add UniformSelect
1 parent 56bc41f commit 3223d2c

9 files changed

+89
-15
lines changed

Diff for: README.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
Components with the same simple interface to handle onChange events on react components
1111

12-
Also exports Input components using this pattern to help with implementation of forms and such
12+
Also exports Input and Select components using this pattern to help with implementation of forms and such
1313

14-
# Example
14+
## Example
1515

1616
```tsx
1717
import { UniformComponent, UniformInput, UniformInputNumber } from "uniform-react-components"
@@ -146,9 +146,9 @@ class SimpleUniform extends UniformComponent<ISimpleData> {
146146
}
147147
```
148148

149-
## Input helpers
149+
## Built-in helpers
150150

151-
uniform-react-components exports two input helpers ( _UniformInput_ and _UniformInputNumber_ )that have the same interface, it only changes the _onChange_ ( and _defaultValue_ which is the case of _UniformInputNumber_ ) properties, and accepts all the properties of a plain `input` element. This only saves you the trouble of implementing these components, but you could implement them if you want to, and even add some more.
151+
uniform-react-components exports two input helpers ( _UniformInput_ and _UniformInputNumber_ ) and a select helper that have the same interface, it only changes the _onChange_ ( and _defaultValue_ which is the case of _UniformInputNumber_ ) properties, and accepts all the properties of a plain `input` element. This only saves you the trouble of implementing these components, but you could implement them if you want to, and even add some more.
152152

153153
### UniformInput
154154

@@ -174,3 +174,25 @@ The same as the plain input element, but the onChange property returns an _numbe
174174
// you can add all the other properties such as className, style...
175175
/>
176176
```
177+
178+
### UniformSelect
179+
180+
```tsx
181+
import { UniformSelect, UniformOptionProps } from "./index"
182+
type MaleOrFemale = "male" | "female"
183+
const options: UniformOptionProps<MaleOrFemale>[] = [
184+
{
185+
value: "male",
186+
children: "Male",
187+
},
188+
{
189+
value: "female",
190+
children: "Female",
191+
},
192+
]
193+
let changed: MaleOrFemale | null = null
194+
const el = <UniformSelect options={options} onChange={(newValue) => console.log(newValue)} defaultValue={"male"} />, // newValue is MaleOrFemale
195+
/>
196+
```
197+
198+
The options is an array of Options, which has all the properties the option element takes, but value is restricted to the specified type, so it enforces type safety

Diff for: package-lock.json

-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"typescript": true,
2626
"react": true,
2727
"rules": {
28-
"no-console": "on"
28+
"no-console": "on",
29+
"jsx-no-multiline-js": "off"
2930
}
3031
},
3132
"jest": {
@@ -74,8 +75,7 @@
7475
"semantic-release": "^15.10.5",
7576
"travis-deploy-once": "^5.0.9",
7677
"ts-jest": "^23.1.4",
77-
"typescript": "^3.0.3",
78-
"utility-types": "^2.1.0"
78+
"typescript": "^3.0.3"
7979
},
8080
"peerDependencies": {
8181
"react": "^16.5.2",

Diff for: src/UniformInput.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react"
22
import { UniformProps } from "./UniformComponent"
3-
import { Omit } from "utility-types"
3+
import { Omit } from "./type-helpers"
44

55
export class UniformInput extends React.Component<
66
UniformProps<string, Omit<JSX.IntrinsicElements["input"], "onChange">>

Diff for: src/UniformInputNumber.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react"
22
import { UniformProps } from "./UniformComponent"
3-
import { Omit } from "utility-types"
3+
import { Omit } from "./type-helpers"
44

55
export class UniformInputNumber extends React.Component<
66
UniformProps<

Diff for: src/UniformSelect.test.tsx

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// tslint:disable:jsx-no-lambda
2+
import * as React from "react"
3+
import { mount } from "enzyme"
4+
import { UniformSelect, UniformOptionProps } from "./index"
5+
6+
describe("<UniformSelect>", () => {
7+
it("should dispatch change event", () => {
8+
type MaleOrFemale = "male" | "female"
9+
const options: UniformOptionProps<MaleOrFemale>[] = [
10+
{
11+
value: "male",
12+
children: "Male",
13+
},
14+
{
15+
value: "female",
16+
children: "Female",
17+
},
18+
]
19+
let changed: MaleOrFemale | null = null
20+
const wrapper = mount(
21+
<UniformSelect
22+
options={options}
23+
onChange={newValue => (changed = newValue)}
24+
defaultValue={"male"}
25+
/>,
26+
)
27+
const input = wrapper.find("select")
28+
input.simulate("change", { target: { value: "female" } })
29+
expect(changed).toBe("female")
30+
})
31+
})

Diff for: src/UniformSelect.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from "react"
2+
import { UniformComponent } from "./UniformComponent"
3+
import { Omit } from "./type-helpers"
4+
5+
export type UniformOptionProps<T> = Omit<JSX.IntrinsicElements["option"], "value"> & { value: T }
6+
7+
export class UniformSelect<T extends string> extends UniformComponent<
8+
T,
9+
Omit<JSX.IntrinsicElements["select"], "onChange"> & { options?: UniformOptionProps<T>[] }
10+
> {
11+
_UniformSelectOnChange = (ev: React.ChangeEvent<HTMLSelectElement>) => {
12+
if (this.props.onChange) {
13+
this.props.onChange(ev.target.value as T)
14+
}
15+
}
16+
render() {
17+
return (
18+
<select onChange={this._UniformSelectOnChange}>
19+
{this.props.options &&
20+
this.props.options.map(prop => <option {...prop} key={prop.value} />)}
21+
</select>
22+
)
23+
}
24+
}

Diff for: src/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./UniformComponent"
22
export * from "./UniformInput"
33
export * from "./UniformInputNumber"
4+
export * from "./UniformSelect"

Diff for: src/type-helpers.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// From Typescript docs
2+
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>

0 commit comments

Comments
 (0)