Skip to content

Commit

Permalink
feat #65 - Update Typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Aug 26, 2020
1 parent 31660d4 commit 27d28f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
6 changes: 4 additions & 2 deletions src/components/Form/FieldContext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createContext } from 'react'
import { FieldValues } from 'react-hook-form/dist/types/form'
import { SubmitHandler } from 'react-hook-form'

export interface FieldContextProps {
initialValues: Record<string, any>
initialValues: FieldValues
loading: boolean
onSubmit: (data: any) => void
onSubmit: SubmitHandler<FieldValues>
}

const FieldContext = createContext<FieldContextProps>({} as FieldContextProps)
Expand Down
12 changes: 8 additions & 4 deletions src/components/Form/Form.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { action } from '@storybook/addon-actions'
import Form from './index'
import React from 'react'
import Form, { FormProps } from './index'
import { Meta, Story } from '@storybook/react/types-6-0'

export default {
Expand All @@ -14,8 +13,13 @@ export default {
title: 'Form'
} as Meta

const Template: Story = args => (
<Form onSubmit={action('onSubmit')} {...args}>
interface UserModel {
firstName: string
lastName?: string
}

const Template: Story<FormProps<UserModel>> = (args: FormProps<UserModel>) => (
<Form {...args}>
<Form.Input label='First Name' name='firstName' required />
<Form.Input label='Last Name' name='lastName' />
<Form.Button />
Expand Down
26 changes: 11 additions & 15 deletions src/components/Form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createUseStyles } from 'react-jss'
import FieldContext from './FieldContext'
import FormButton, { FormButtonProps } from './FormButton'
import FormInput, { FormInputProps } from './FormInput'
import { FormProvider, useForm } from 'react-hook-form'
import React, { FC, ReactNode } from 'react'
import { FieldValues } from 'react-hook-form/dist/types/form'
import FormButton from './FormButton'
import FormInput from './FormInput'
import { FormProvider, SubmitHandler, useForm } from 'react-hook-form'
import React, { ReactNode } from 'react'

const useStyles = createUseStyles({
container: {
Expand All @@ -13,24 +14,19 @@ const useStyles = createUseStyles({
}
})

export interface FormProps {
export interface FormProps<Model> {
children: ReactNode
initialValues?: Record<string, any>
initialValues?: Model
loading?: boolean
onSubmit: (data: any) => void
onSubmit: SubmitHandler<FieldValues>
}

interface FormSubComponents {
Button: FC<FormButtonProps>
Input: FC<FormInputProps>
}

const Form: FC<FormProps> & FormSubComponents = ({
function Form<Model>({
children,
initialValues = {},
initialValues = {} as Model,
loading = false,
onSubmit
}: FormProps) => {
}: FormProps<Model>) {
const classes = useStyles()
const methods = useForm()

Expand Down

0 comments on commit 27d28f9

Please sign in to comment.