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

Commit

Permalink
feat: add default values for inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpza committed Nov 6, 2018
1 parent f195863 commit d510f2f
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/UniformComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component } from "react"
import { dynamicOnChange } from "dynamic-on-change"

export type UniformProps<D, P> = {
export type UniformProps<D, P = {}> = {
onChange?: (newValue: D) => void
defaultValue: D
} & P
Expand Down
4 changes: 4 additions & 0 deletions src/UniformInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ describe("<UniformInput>", () => {
const wrapper = mount(<UniformInput defaultValue="3" />)
wrapper.find("input").simulate("change", { target: { value: "foo" } })
})
it("should allow ommitting property defaultValue", () => {
const wrapper = mount(<UniformInput />)
wrapper.find("input").simulate("change", { target: { value: "foo" } })
})
})
9 changes: 7 additions & 2 deletions src/UniformInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as React from "react"
import { UniformProps } from "./UniformComponent"
import { Omit } from "./type-helpers"
import { SafeJoin } from "./type-helpers"

export class UniformInput extends React.Component<
UniformProps<string, Omit<JSX.IntrinsicElements["input"], "onChange">>
SafeJoin<
SafeJoin<JSX.IntrinsicElements["input"], UniformProps<string>>,
{
defaultValue?: string
}
>
> {
private _UniformInputOnChange = (ev: React.ChangeEvent<HTMLInputElement>) => {
if (this.props.onChange) {
Expand Down
4 changes: 4 additions & 0 deletions src/UniformInputNumber.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ describe("<UniformInputNumber>", () => {
const wrapper = mount(<UniformInputNumber defaultValue={3} />)
wrapper.find("input").simulate("change", { target: { value: "6" } })
})
it("should allow ommitting property defaultValue", () => {
const wrapper = mount(<UniformInputNumber />)
wrapper.find("input").simulate("change", { target: { value: "6" } })
})
})
18 changes: 8 additions & 10 deletions src/UniformInputNumber.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as React from "react"
import { UniformProps } from "./UniformComponent"
import { Omit } from "./type-helpers"
import { SafeJoin } from "./type-helpers"

export class UniformInputNumber extends React.Component<
UniformProps<
number,
Omit<JSX.IntrinsicElements["input"], "onChange" | "defaultValue"> & {
defaultValue: number
SafeJoin<
SafeJoin<JSX.IntrinsicElements["input"], UniformProps<number>>,
{
defaultValue?: number
}
>
> {
Expand All @@ -16,12 +16,10 @@ export class UniformInputNumber extends React.Component<
}
}
render() {
const defaultValue =
this.props.defaultValue === undefined ? "0" : this.props.defaultValue.toString()
return (
<input
{...this.props}
onChange={this._UniformInputOnChange}
defaultValue={this.props.defaultValue.toString()}
/>
<input {...this.props} onChange={this._UniformInputOnChange} defaultValue={defaultValue} />
)
}
}
4 changes: 2 additions & 2 deletions src/UniformSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from "react"
import { UniformComponent } from "./UniformComponent"
import { Omit } from "./type-helpers"
import { SafeJoin, Omit } from "./type-helpers"

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

export class UniformSelect<T extends string> extends UniformComponent<
T,
Expand Down
2 changes: 2 additions & 0 deletions src/type-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// From Typescript docs
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>

export type SafeJoin<T, K> = Omit<T, keyof K> & K

0 comments on commit d510f2f

Please sign in to comment.