Skip to content

Commit fc3f12e

Browse files
committed
Add date and time support client-side
1 parent 100b855 commit fc3f12e

File tree

10 files changed

+58
-16
lines changed

10 files changed

+58
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ You can check a more complete example in the [example](https://github.com/franci
8989
- Right now there is no observability on errors when submitting a form. See this [comment on the code](https://github.com/francisconeves97/react-google-forms-hooks/blob/ca5018e578cfb0e230f9be58dfeee4117db28160/src/hooks/useGoogleForm.ts#L61-L65).
9090
- You can use the `submitToGoogleForm` export to create a server to handle form submissions. This way you can mitigate the CORS problem.
9191
- No support for multi page, sections, images and other Google Forms functionalities. However you can build your React form with multiple pages, by saving the `data` from `handleSubmit` and only `submitToGoogleForms` on the last page.
92-
- The list of supported inputs doesn't feature every input from Google Forms. Supported inputs: Short Answer, Long Answer, Checkbox, Radio, Dropdown, Linear, Radio Grid, Checkbox Grid
92+
- The list of supported inputs doesn't feature every input from Google Forms. Supported inputs: Short Answer, Long Answer, Date, Time, Checkbox, Radio, Dropdown, Linear, Radio Grid, Checkbox Grid
9393
- Because of CORS you have to run the `googleFormsToJson` script in build time.
9494

9595
## Contributing
@@ -126,4 +126,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
126126

127127
<!-- ALL-CONTRIBUTORS-LIST:END -->
128128

129-
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
129+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

example/src/App.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import CheckboxInput from './components/CheckboxInput'
99
import RadioInput from './components/RadioInput'
1010
import ShortAnswerInput from './components/ShortAnswerInput'
1111
import LongAnswerInput from './components/LongAnswerInput'
12+
import DateInput from './components/DateInput'
13+
import TimeInput from "./components/TimeInput"
1214
import RadioGridInput from './components/RadioGridInput'
1315
import CheckboxGridInput from './components/CheckboxGridInput'
1416
import DropdownInput from './components/DropdownInput'
@@ -54,6 +56,12 @@ const Questions = () => {
5456
case 'LONG_ANSWER':
5557
questionInput = <LongAnswerInput id={id} />
5658
break
59+
case 'DATE':
60+
questionInput = <DateInput id={id} />
61+
break
62+
case 'TIMe':
63+
questionInput = <TimeInput id={id} />
64+
break
5765
case 'RADIO_GRID':
5866
questionInput = <RadioGridInput id={id} />
5967
break
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
3+
import { useDateInput } from 'react-google-forms-hooks'
4+
5+
export default function DateInput({ id }) {
6+
const { register } = useDateInput(id)
7+
8+
return (
9+
<div>
10+
<input type='date' {...register()} />
11+
</div>
12+
)
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
3+
import { useTimeInput } from 'react-google-forms-hooks'
4+
5+
export default function TimeInput({ id }) {
6+
const { register } = useTimeInput(id)
7+
8+
return (
9+
<div>
10+
<input type='time' {...register()} />
11+
</div>
12+
)
13+
}

src/hooks/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export * from './useCheckboxGridInput'
88
export * from './useRadioGridInput'
99
export * from './useDropdownInput'
1010
export * from './useLinearInput'
11+
export * from './useDateInput'
12+
export * from './useTimeInput'

src/hooks/useDateInput.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { UseTextFieldReturn } from '../types'
2+
import useTextInput from './utils/useTextInput'
3+
4+
export const useDateInput = (id: string): UseTextFieldReturn => {
5+
return useTextInput(id, 'DATE')
6+
}

src/hooks/useTimeInput.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { UseTextFieldReturn } from '../types'
2+
import useTextInput from './utils/useTextInput'
3+
4+
export const useTimeInput = (id: string): UseTextFieldReturn => {
5+
return useTextInput(id, 'TIME')
6+
}

src/hooks/utils/useTextInput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import getFieldFromContext from './getFieldFromContext'
66

77
export default (
88
id: string,
9-
fieldType: 'LONG_ANSWER' | 'SHORT_ANSWER'
9+
fieldType: 'LONG_ANSWER' | 'SHORT_ANSWER' | 'DATE' | 'TIME',
1010
): UseTextFieldReturn => {
1111
const context = useGoogleFormContext()
1212

src/scripts/googleFormsToJson.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ const parseFieldType = (rawField: Array<object>, fieldId: number) => {
100100
if (fieldId === 9) {
101101
return 'DATE'
102102
}
103+
if (fieldId === 10) {
104+
return 'TIME'
105+
}
103106

104107
return fieldTypes[fieldId]
105108
}
@@ -141,7 +144,9 @@ const parseField = (rawField: Array<any>): Field => {
141144

142145
switch (field.type) {
143146
case 'SHORT_ANSWER':
144-
case 'LONG_ANSWER': {
147+
case 'LONG_ANSWER':
148+
case 'DATE':
149+
case 'TIME': {
145150
const fieldInfo = rawField[4][0]
146151
field.id = toString(fieldInfo[0])
147152
field.required = toBool(fieldInfo[2])
@@ -179,12 +184,6 @@ const parseField = (rawField: Array<any>): Field => {
179184
field.required = toBool(rawField[4][0][2])
180185
break
181186
}
182-
case 'DATE': {
183-
const fieldInfo = rawField[4][0]
184-
field.id = toString(fieldInfo[0])
185-
field.required = toBool(rawField[4][0][2])
186-
break
187-
}
188187
}
189188

190189
return field

src/types/form.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ export interface BaseField {
1414
}
1515

1616
export interface TextField extends BaseField {
17-
type: 'SHORT_ANSWER' | 'LONG_ANSWER'
18-
}
19-
20-
export interface DateField extends BaseField {
21-
type: 'DATE'
17+
type: 'SHORT_ANSWER' | 'LONG_ANSWER' | 'DATE' | 'TIME'
2218
}
2319

2420
export interface CustomOptionField extends BaseField {
@@ -59,7 +55,6 @@ export interface GridField extends BaseField {
5955

6056
export type Field =
6157
| TextField
62-
| DateField
6358
| CustomOptionField
6459
| DropdownField
6560
| GridField

0 commit comments

Comments
 (0)