Skip to content

Add date and time support client-side #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ You can check a more complete example in the [example](https://github.com/franci
- 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).
- You can use the `submitToGoogleForm` export to create a server to handle form submissions. This way you can mitigate the CORS problem.
- 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.
- 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
- 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
- Because of CORS you have to run the `googleFormsToJson` script in build time.

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

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

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
8 changes: 8 additions & 0 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import CheckboxInput from './components/CheckboxInput'
import RadioInput from './components/RadioInput'
import ShortAnswerInput from './components/ShortAnswerInput'
import LongAnswerInput from './components/LongAnswerInput'
import DateInput from './components/DateInput'
import TimeInput from "./components/TimeInput"
import RadioGridInput from './components/RadioGridInput'
import CheckboxGridInput from './components/CheckboxGridInput'
import DropdownInput from './components/DropdownInput'
Expand Down Expand Up @@ -54,6 +56,12 @@ const Questions = () => {
case 'LONG_ANSWER':
questionInput = <LongAnswerInput id={id} />
break
case 'DATE':
questionInput = <DateInput id={id} />
break
case 'TIMe':
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a typo?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, will fix

questionInput = <TimeInput id={id} />
break
case 'RADIO_GRID':
questionInput = <RadioGridInput id={id} />
break
Expand Down
13 changes: 13 additions & 0 deletions example/src/components/DateInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'

import { useDateInput } from 'react-google-forms-hooks'

export default function DateInput({ id }) {
const { register } = useDateInput(id)

return (
<div>
<input type='date' {...register()} />
</div>
)
}
13 changes: 13 additions & 0 deletions example/src/components/TimeInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'

import { useTimeInput } from 'react-google-forms-hooks'

export default function TimeInput({ id }) {
const { register } = useTimeInput(id)

return (
<div>
<input type='time' {...register()} />
</div>
)
}
2 changes: 2 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export * from './useCheckboxGridInput'
export * from './useRadioGridInput'
export * from './useDropdownInput'
export * from './useLinearInput'
export * from './useDateInput'
export * from './useTimeInput'
6 changes: 6 additions & 0 deletions src/hooks/useDateInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { UseTextFieldReturn } from '../types'
import useTextInput from './utils/useTextInput'

export const useDateInput = (id: string): UseTextFieldReturn => {
return useTextInput(id, 'DATE')
}
6 changes: 6 additions & 0 deletions src/hooks/useTimeInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { UseTextFieldReturn } from '../types'
import useTextInput from './utils/useTextInput'

export const useTimeInput = (id: string): UseTextFieldReturn => {
return useTextInput(id, 'TIME')
}
2 changes: 1 addition & 1 deletion src/hooks/utils/useTextInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import getFieldFromContext from './getFieldFromContext'

export default (
id: string,
fieldType: 'LONG_ANSWER' | 'SHORT_ANSWER'
fieldType: 'LONG_ANSWER' | 'SHORT_ANSWER' | 'DATE' | 'TIME',
): UseTextFieldReturn => {
const context = useGoogleFormContext()

Expand Down
13 changes: 6 additions & 7 deletions src/scripts/googleFormsToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ const parseFieldType = (rawField: Array<object>, fieldId: number) => {
if (fieldId === 9) {
return 'DATE'
}
if (fieldId === 10) {
return 'TIME'
}

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

switch (field.type) {
case 'SHORT_ANSWER':
case 'LONG_ANSWER': {
case 'LONG_ANSWER':
case 'DATE':
case 'TIME': {
const fieldInfo = rawField[4][0]
field.id = toString(fieldInfo[0])
field.required = toBool(fieldInfo[2])
Expand Down Expand Up @@ -179,12 +184,6 @@ const parseField = (rawField: Array<any>): Field => {
field.required = toBool(rawField[4][0][2])
break
}
case 'DATE': {
const fieldInfo = rawField[4][0]
field.id = toString(fieldInfo[0])
field.required = toBool(rawField[4][0][2])
break
}
}

return field
Expand Down
7 changes: 1 addition & 6 deletions src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ export interface BaseField {
}

export interface TextField extends BaseField {
type: 'SHORT_ANSWER' | 'LONG_ANSWER'
}

export interface DateField extends BaseField {
type: 'DATE'
Copy link
Author

@DanielOrtel DanielOrtel May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DATE and TIME fields act exactly like SHORT_ANSWER and LONG_ANSWER from what I could see, since they're basically just strings, so it makes more sense to group them together

Copy link
Owner

@francisconeves97 francisconeves97 May 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are their format the same? Would it make sense to add some validation regarding the date format? I will have to take a deeper look into how google forms handles these type of inputs

type: 'SHORT_ANSWER' | 'LONG_ANSWER' | 'DATE' | 'TIME'
}

export interface CustomOptionField extends BaseField {
Expand Down Expand Up @@ -59,7 +55,6 @@ export interface GridField extends BaseField {

export type Field =
| TextField
| DateField
| CustomOptionField
| DropdownField
| GridField
Expand Down