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

Commit d510f2f

Browse files
committed
feat: add default values for inputs
1 parent f195863 commit d510f2f

7 files changed

+28
-15
lines changed

Diff for: src/UniformComponent.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component } from "react"
22
import { dynamicOnChange } from "dynamic-on-change"
33

4-
export type UniformProps<D, P> = {
4+
export type UniformProps<D, P = {}> = {
55
onChange?: (newValue: D) => void
66
defaultValue: D
77
} & P

Diff for: src/UniformInput.test.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ describe("<UniformInput>", () => {
1717
const wrapper = mount(<UniformInput defaultValue="3" />)
1818
wrapper.find("input").simulate("change", { target: { value: "foo" } })
1919
})
20+
it("should allow ommitting property defaultValue", () => {
21+
const wrapper = mount(<UniformInput />)
22+
wrapper.find("input").simulate("change", { target: { value: "foo" } })
23+
})
2024
})

Diff for: src/UniformInput.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import * as React from "react"
22
import { UniformProps } from "./UniformComponent"
3-
import { Omit } from "./type-helpers"
3+
import { SafeJoin } from "./type-helpers"
44

55
export class UniformInput extends React.Component<
6-
UniformProps<string, Omit<JSX.IntrinsicElements["input"], "onChange">>
6+
SafeJoin<
7+
SafeJoin<JSX.IntrinsicElements["input"], UniformProps<string>>,
8+
{
9+
defaultValue?: string
10+
}
11+
>
712
> {
813
private _UniformInputOnChange = (ev: React.ChangeEvent<HTMLInputElement>) => {
914
if (this.props.onChange) {

Diff for: src/UniformInputNumber.test.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ describe("<UniformInputNumber>", () => {
2121
const wrapper = mount(<UniformInputNumber defaultValue={3} />)
2222
wrapper.find("input").simulate("change", { target: { value: "6" } })
2323
})
24+
it("should allow ommitting property defaultValue", () => {
25+
const wrapper = mount(<UniformInputNumber />)
26+
wrapper.find("input").simulate("change", { target: { value: "6" } })
27+
})
2428
})

Diff for: src/UniformInputNumber.tsx

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

55
export class UniformInputNumber extends React.Component<
6-
UniformProps<
7-
number,
8-
Omit<JSX.IntrinsicElements["input"], "onChange" | "defaultValue"> & {
9-
defaultValue: number
6+
SafeJoin<
7+
SafeJoin<JSX.IntrinsicElements["input"], UniformProps<number>>,
8+
{
9+
defaultValue?: number
1010
}
1111
>
1212
> {
@@ -16,12 +16,10 @@ export class UniformInputNumber extends React.Component<
1616
}
1717
}
1818
render() {
19+
const defaultValue =
20+
this.props.defaultValue === undefined ? "0" : this.props.defaultValue.toString()
1921
return (
20-
<input
21-
{...this.props}
22-
onChange={this._UniformInputOnChange}
23-
defaultValue={this.props.defaultValue.toString()}
24-
/>
22+
<input {...this.props} onChange={this._UniformInputOnChange} defaultValue={defaultValue} />
2523
)
2624
}
2725
}

Diff for: src/UniformSelect.tsx

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

5-
export type UniformOptionProps<T> = Omit<JSX.IntrinsicElements["option"], "value"> & { value: T }
5+
export type UniformOptionProps<T> = SafeJoin<JSX.IntrinsicElements["option"], { value: T }>
66

77
export class UniformSelect<T extends string> extends UniformComponent<
88
T,

Diff for: src/type-helpers.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
// From Typescript docs
22
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
3+
4+
export type SafeJoin<T, K> = Omit<T, keyof K> & K

0 commit comments

Comments
 (0)