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

Commit e5ffe16

Browse files
committed
feat: handle value property too
1 parent d510f2f commit e5ffe16

8 files changed

+24
-35
lines changed

Diff for: src/UniformComponent.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ describe("<UniformComponent>", () => {
1313
const defaultValue = { bar: "hello" }
1414
let changed: null | { bar: string } = null
1515
const wrapper = mount(
16-
<TestUniformComponent onChange={value => (changed = value)} defaultValue={defaultValue} />,
16+
<TestUniformComponent onChange={value => (changed = value)} value={defaultValue} />,
1717
)
1818
const input = wrapper.find("input")
1919
input.simulate("change", { target: { value: "foo" } })
2020
expect(changed).toMatchObject({ bar: "foo" })
2121
})
2222
it("should allow ommiting onChange property", () => {
2323
const defaultValue = { bar: "hello" }
24-
const wrapper = mount(<TestUniformComponent defaultValue={defaultValue} />)
24+
const wrapper = mount(<TestUniformComponent value={defaultValue} />)
2525
const input = wrapper.find("input")
2626
input.simulate("change", { target: { value: "foo" } })
2727
})

Diff for: src/UniformComponent.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { dynamicOnChange } from "dynamic-on-change"
33

44
export type UniformProps<D, P = {}> = {
55
onChange?: (newValue: D) => void
6-
defaultValue: D
6+
value: D
7+
defaultValue?: D // DEPRECATED
78
} & P
89

910
export class UniformComponent<D, P = {}, S = {}, SS = any> extends Component<
1011
UniformProps<D, P>,
1112
S,
1213
SS
1314
> {
14-
private _UniformData: D = this.props.defaultValue
15+
private _UniformData: D = this.props.value
1516
private _UniformOnChange = (key: keyof D, value: D[keyof D]) => {
1617
this._UniformData[key] = value
1718
if (this.props.onChange) {

Diff for: src/UniformInput.test.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import { UniformInput } from "./index"
66
describe("<UniformInput>", () => {
77
it("should dispatch change event", () => {
88
let changed: null | string = null
9-
const wrapper = mount(
10-
<UniformInput onChange={newValue => (changed = newValue)} defaultValue="3" />,
11-
)
9+
const wrapper = mount(<UniformInput onChange={newValue => (changed = newValue)} value="3" />)
1210
const input = wrapper.find("input")
1311
input.simulate("change", { target: { value: "foo" } })
1412
expect(changed).toBe("foo")
1513
})
1614
it("should allow ommitting property onChange", () => {
17-
const wrapper = mount(<UniformInput defaultValue="3" />)
15+
const wrapper = mount(<UniformInput value="3" />)
1816
wrapper.find("input").simulate("change", { target: { value: "foo" } })
1917
})
2018
it("should allow ommitting property defaultValue", () => {

Diff for: src/UniformInput.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class UniformInput extends React.Component<
66
SafeJoin<
77
SafeJoin<JSX.IntrinsicElements["input"], UniformProps<string>>,
88
{
9+
value?: string
910
defaultValue?: string
1011
}
1112
>

Diff for: src/UniformInputNumber.test.tsx

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ describe("<UniformInputNumber>", () => {
77
it("should dispatch change event", () => {
88
let changed: null | number = null
99
const wrapper = mount(
10-
<UniformInputNumber
11-
type="number"
12-
onChange={newValue => (changed = newValue)}
13-
defaultValue={3}
14-
/>,
10+
<UniformInputNumber type="number" onChange={newValue => (changed = newValue)} value={3} />,
1511
)
1612
const input = wrapper.find("input")
1713
input.simulate("change", { target: { value: "6" } })
1814
expect(changed).toBe(6)
1915
})
2016
it("should allow ommitting property onChange", () => {
21-
const wrapper = mount(<UniformInputNumber defaultValue={3} />)
17+
const wrapper = mount(<UniformInputNumber value={3} />)
2218
wrapper.find("input").simulate("change", { target: { value: "6" } })
2319
})
2420
it("should allow ommitting property defaultValue", () => {

Diff for: src/UniformInputNumber.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class UniformInputNumber extends React.Component<
66
SafeJoin<
77
SafeJoin<JSX.IntrinsicElements["input"], UniformProps<number>>,
88
{
9+
value?: number
910
defaultValue?: number
1011
}
1112
>
@@ -16,10 +17,13 @@ export class UniformInputNumber extends React.Component<
1617
}
1718
}
1819
render() {
19-
const defaultValue =
20-
this.props.defaultValue === undefined ? "0" : this.props.defaultValue.toString()
21-
return (
22-
<input {...this.props} onChange={this._UniformInputOnChange} defaultValue={defaultValue} />
23-
)
20+
const props = {
21+
...this.props,
22+
...(this.props.value === undefined ? {} : { value: this.props.value.toString() }),
23+
...(this.props.defaultValue === undefined
24+
? {}
25+
: { defaultValue: this.props.defaultValue.toString() }),
26+
} as any
27+
return <input {...props} onChange={this._UniformInputOnChange} />
2428
}
2529
}

Diff for: src/UniformSelect.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("<UniformSelect>", () => {
2121
<UniformSelect
2222
options={options}
2323
onChange={newValue => (changed = newValue)}
24-
defaultValue={"male"}
24+
value={"male"}
2525
/>,
2626
)
2727
const input = wrapper.find("select")

Diff for: src/index.test.tsx

+4-15
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@ it("should work together", () => {
1616
render() {
1717
return (
1818
<form>
19-
<UniformInput
20-
onChange={this.onChange.firstName}
21-
defaultValue={this.props.defaultValue.firstName}
22-
/>
23-
<UniformInput
24-
onChange={this.onChange.lastName}
25-
defaultValue={this.props.defaultValue.lastName}
26-
/>
27-
<UniformInputNumber
28-
onChange={this.onChange.age}
29-
defaultValue={this.props.defaultValue.age}
30-
/>
19+
<UniformInput onChange={this.onChange.firstName} value={this.props.value.firstName} />
20+
<UniformInput onChange={this.onChange.lastName} value={this.props.value.lastName} />
21+
<UniformInputNumber onChange={this.onChange.age} value={this.props.value.age} />
3122
</form>
3223
)
3324
}
@@ -37,9 +28,7 @@ it("should work together", () => {
3728
firstName: "foo",
3829
lastName: "bar",
3930
}
40-
const wrapper = mount(
41-
<SimpleUniform onChange={value => (update = value)} defaultValue={update} />,
42-
)
31+
const wrapper = mount(<SimpleUniform onChange={value => (update = value)} value={update} />)
4332
const inputs = wrapper.find("input")
4433
inputs.at(0).simulate("change", { target: { value: "my-firstname" } })
4534
expect(update).toMatchObject({

0 commit comments

Comments
 (0)