-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Date Input on Mobile Devices (#196)
* Fix Date Input on Mobile Devices * functions are pure * Allow user to input date * Fix SSR Issues * Pure comment * Add documentation
- Loading branch information
Showing
12 changed files
with
346 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as React from "react"; | ||
import { format, parse } from "date-fns"; | ||
import { DateInputProps } from "./typings/DateInput"; | ||
import Input from "./Input"; | ||
|
||
export default class NativeDateInput extends React.PureComponent< | ||
DateInputProps | ||
> { | ||
private onDateInputChange = (value: string) => { | ||
const date = parse(value); | ||
this.props.onChange(date && date.getTime()); | ||
}; | ||
|
||
render() { | ||
const { inputProps, placeholder, value } = this.props; | ||
|
||
return ( | ||
<Input | ||
onChange={this.onDateInputChange} | ||
type="date" | ||
// This format does not define the presentation format. | ||
// value will always be in YYYY-MM-DD format. | ||
// https://developers.google.com/web/updates/2012/08/Quick-FAQs-on-input-type-date-in-Google-Chrome | ||
value={value && format(value, "YYYY-MM-DD")} | ||
placeholder={placeholder} | ||
fixLabelAtTop | ||
{...inputProps} | ||
/> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
import { SimpleInputProps } from "./Input"; | ||
import { Omit } from "utility-types"; | ||
import { DateSingle } from "./Calendar"; | ||
|
||
export interface DateInputProps { | ||
onChange: (date?: number) => void; | ||
value?: number | Date; | ||
placeholder: string; | ||
inputProps?: Omit<SimpleInputProps, "value" | "onChange" | "placeholder">; | ||
calendarProps?: DateSingle; | ||
} | ||
|
||
export interface DateInputState { | ||
stringInput: string; | ||
propsValue?: number | Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# UserAgentInfoContext Documentation | ||
|
||
A React Context Provider specifically meant for userAgent and its derived attributes. | ||
Set userAgent from top level component so that it is accessible within components on server and client. | ||
Similar to [@quentin-sommer/react-useragent](https://github.com/quentin-sommer/react-useragent) but with type definitions and reduced scope. | ||
|
||
```tsx | ||
import { utils } from "@anarock/pebble"; | ||
const { UserAgentInfoContext } = utils; | ||
|
||
class Component extends React.PureComponent { | ||
static context = UserAgentInfoContext; | ||
render () { | ||
return ( | ||
<UserAgentInfoContext.Consumer> | ||
{({ userAgent, isMobile }) => ( | ||
// Can use userAgent & isMobile here. | ||
)} | ||
</UserAgentInfoContext.Consumer> | ||
) | ||
} | ||
} | ||
``` | ||
|
||
## Usage with next.js | ||
|
||
```tsx | ||
import App, { Container, NextAppContext } from "next/app"; | ||
import * as React from "react"; | ||
import { utils } from "@anarock/pebble"; | ||
|
||
interface RootProps { | ||
Component: React.ComponentClass; | ||
userAgent: string; | ||
// tslint:disable-next-line no-any | ||
pageProps: any; | ||
} | ||
|
||
const { UserAgentInfoProvider } = utils; | ||
|
||
export default class MyApp extends App<RootProps> { | ||
static async getInitialProps({ Component, ctx }: NextAppContext) { | ||
let pageProps = {}; | ||
if (Component.getInitialProps) { | ||
pageProps = await Component.getInitialProps(ctx); | ||
} | ||
|
||
const userAgent = | ||
(ctx.req | ||
? ctx.req.headers["user-agent"] | ||
: typeof navigator !== "undefined" && navigator.userAgent) || ""; | ||
|
||
return { | ||
userAgent, | ||
pageProps | ||
}; | ||
} | ||
|
||
render() { | ||
const { Component, pageProps, userAgent } = this.props; | ||
return ( | ||
<Container> | ||
<UserAgentInfoProvider userAgent={userAgent}> | ||
<Component {...pageProps} /> | ||
</UserAgentInfoProvider> | ||
</Container> | ||
); | ||
} | ||
} | ||
``` |
Oops, something went wrong.