Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
vassbence committed Sep 4, 2024
1 parent 2debe34 commit be53d85
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StorybookConfig } from '@storybook/react-vite'

const config: StorybookConfig = {
stories: ['../src/**/*.stories.@(ts|tsx)'],
stories: ['../**/*.stories.@(ts|tsx)'],
addons: ['@storybook/addon-essentials'],
framework: {
name: '@storybook/react-vite',
Expand Down
2 changes: 1 addition & 1 deletion src/components/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ function Dialog({
lockScroll
style={{
background: colors.black60,
outline: 'none',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
Expand All @@ -130,6 +129,7 @@ function Dialog({
justifyContent: 'center',
flexDirection: 'column',
gap: 16,
outline: 'none',
...styles,
}}
>
Expand Down
7 changes: 3 additions & 4 deletions src/components/Form/Form.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React from 'react'
import { FormErrors, FormField, FormFieldGroup, useForm } from './index.js'
import { TextInput } from '../TextInput/index.js'
import { TextAreaInput } from '../TextAreaInput/index.js'
Expand All @@ -16,7 +16,6 @@ export default {

export const AllTypes = () => {
const form = useForm({
initialValues: {},
onSubmit: (values) => {
console.log('onSubmit', values)
},
Expand Down Expand Up @@ -96,7 +95,7 @@ export const AllTypes = () => {
}

export const WIP = () => {
const [apiResponse, setApiResponse] = React.useState<any>(null)
const [apiResponse, setApiResponse] = React.useState<any>()

React.useEffect(() => {
setTimeout(() => {
Expand All @@ -111,7 +110,7 @@ export const WIP = () => {
}, [])

const form = useForm({
initialValues: apiResponse ?? {},
initialValues: apiResponse,
validate: async (values) => {
const errors: FormErrors = {}

Expand Down
20 changes: 14 additions & 6 deletions src/components/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ type FormState = {
numberOfInFlightValidations: number
}
type UseFormProps = {
initialValues: FormValues
initialValues?: FormValues
onSubmit: (values: FormValues) => void | Promise<void>
validate?: (values: FormValues) => FormErrors | Promise<FormErrors>
}

function useForm({ initialValues, onSubmit, validate }: UseFormProps) {
function useForm({ initialValues = {}, onSubmit, validate }: UseFormProps) {
const rerender = useRerender()
const state = useRef<FormState>({
values: initialValues,
Expand Down Expand Up @@ -107,12 +107,20 @@ function useForm({ initialValues, onSubmit, validate }: UseFormProps) {
rerender()

await validateForm()
if (Object.keys(state.current.errors).length === 0) {
await onSubmit(state.current.mergedValues)
if (Object.keys(state.current.errors).length !== 0) {
state.current.isSubmitting = false
rerender()
return
}

state.current.isSubmitting = false
rerender()
try {
await onSubmit(state.current.mergedValues)
} catch {
// no-op
} finally {
state.current.isSubmitting = false
rerender()
}
}, [validateForm, onSubmit])

const resetForm = useCallback(() => {
Expand Down
12 changes: 6 additions & 6 deletions src/components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ function Modal({ children, open, onOpenChange }: ModalRootProps) {
justifyContent: 'center',
alignItems: 'center',
zIndex: 2,
outline: 'none',
...overlayStyles,
}}
>
Expand All @@ -87,6 +86,7 @@ function Modal({ children, open, onOpenChange }: ModalRootProps) {
display: 'flex',
justifyContent: 'center',
flexDirection: 'column',
outline: 'none',
...styles,
}}
>
Expand Down Expand Up @@ -152,14 +152,14 @@ function ModalBody({ children }: ModalBodyProps) {
paddingLeft: 24,
paddingRight: 24,
maxHeight: 480,
overflowY: 'auto',
scrollbarWidth: 'none',
'&::-webkit-scrollbar': {
display: 'none',
},
...(contentHeight > 480 && {
borderTop: `1px solid ${colors.neutral10}`,
borderBottom: `1px solid ${colors.neutral10}`,
overflowY: 'scroll',
scrollbarWidth: 'none',
'&::-webkit-scrollbar': {
display: 'none',
},
}),
}}
>
Expand Down
108 changes: 108 additions & 0 deletions src/components/Patterns.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import React, { useState } from 'react'
import {
useForm,
Button,
Modal,
TextInput,
CheckboxInput,
TextAreaInput,
} from '../index.js'

export default {
title: 'Patterns',
component: () => {},
}

// TODO actually use based for the data, form submits etc.

export function FormInModalWithDataFromAPI() {
const [open, setOpen] = useState(false)
const form = useForm({
validate: async (values) => {
const errors = {}

await new Promise((resolve) => {
setTimeout(() => {
resolve('foo')
}, 2000)
})

if (!values['title']) {
errors['title'] = 'title is required'
}

return errors
},
onSubmit: async (values) => {
await new Promise((resolve) => {
setTimeout(() => {
resolve('foo')
}, 2000)
})

console.log('form submitted with', JSON.parse(JSON.stringify(values)))

setOpen(false)
},
})

return (
<>
<Button
leadIcon="add"
onClick={() => {
form.resetForm()
setOpen(true)
}}
>
Add article
</Button>

<Modal open={open} onOpenChange={setOpen}>
<Modal.Title>Add todo</Modal.Title>
<Modal.Description>
Add a beautiful little todo here to make sure you remember
</Modal.Description>
<Modal.Body>
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<TextInput
disabled={form.isSubmitting}
label="Title"
value={form.values['title'] as string}
onChange={(v) => {
form.setValue('title', v)
}}
error={form.errors['title']}
/>
<TextAreaInput
disabled={form.isSubmitting}
label="Description"
value={form.values['description'] as string}
onChange={(v) => {
form.setValue('description', v)
}}
error={form.errors['description']}
/>
</div>
</Modal.Body>
<Modal.Actions>
{({ close }) => (
<>
<Button
disabled={form.isSubmitting}
onClick={close}
variant="border"
keyHint="Esc"
>
Close
</Button>
<Button onClick={form.submitForm} keyHint="Enter">
Create Todo
</Button>
</>
)}
</Modal.Actions>
</Modal>
</>
)
}

0 comments on commit be53d85

Please sign in to comment.