Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
vassbence committed Sep 5, 2024
1 parent be53d85 commit bc6690b
Show file tree
Hide file tree
Showing 16 changed files with 717 additions and 675 deletions.
8 changes: 0 additions & 8 deletions src/components/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,3 @@ export const Default = {
</div>
),
}

export const FileUploadAsyncOnClick = () => {
const client = useClient()

console.log(client)

return <Button>Upload a file</Button>
}
34 changes: 12 additions & 22 deletions src/components/Form/Form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { Button } from '../Button/index.js'
import { format } from 'date-fns'

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

export const AllTypes = () => {
export const Default = () => {
const form = useForm({
onSubmit: (values) => {
console.log('onSubmit', values)
Expand All @@ -24,11 +24,9 @@ export const AllTypes = () => {
},
})

console.count('rerender')

return (
<>
<FormFieldGroup header="Group title">
<FormFieldGroup label="Group title">
<FormField label="TextInput">
<TextInput
value={form.values['text'] as string}
Expand Down Expand Up @@ -89,24 +87,17 @@ export const AllTypes = () => {
</Calendar>
</FormField>
</FormFieldGroup>
<pre>{JSON.stringify(form, null, 2)}</pre>
</>
)
}

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

React.useEffect(() => {
setTimeout(() => {
setApiResponse({
email: '[email protected]',
password: crypto.randomUUID(),
})
setInterval(() => {
setApiResponse((p) => ({ ...p, password: crypto.randomUUID() }))
}, 500)
}, 1500)
setInterval(() => {
setApiResponse((p) => ({ ...p, email: crypto.randomUUID() }))
}, 500)
}, [])

const form = useForm({
Expand Down Expand Up @@ -142,28 +133,27 @@ export const WIP = () => {

return (
<>
<FormFieldGroup header="Group title">
<FormField label="Email">
<FormFieldGroup label="Group title">
<FormField label="Email" error={form.errors['email']}>
<TextInput
type="email"
// TODO @vassbence fix "as string"
disabled={form.isSubmitting}
value={form.values['email'] as string}
onChange={(value) => {
form.setValue('email', value)
}}
error={form.errors['email']}
error={!!form.errors['email']}
/>
</FormField>
<FormField label="Password">
<FormField label="Password" error={form.errors['password']}>
<TextInput
type="password"
disabled={form.isSubmitting}
value={form.values['password'] as string}
onChange={(value) => {
form.setValue('password', value)
}}
error={form.errors['password']}
error={!!form.errors['password']}
/>
</FormField>
</FormFieldGroup>
Expand Down
29 changes: 29 additions & 0 deletions src/components/Form/FormField.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import { FormField } from './FormField.js'
import { TextInput } from '../TextInput/index.js'

export default {
title: 'FormField',
component: FormField,
}

export const Vertical = () => {
return (
<FormField label="Label" description="Description text" error="Error text">
<TextInput error onChange={() => {}} />
</FormField>
)
}

export const Horizontal = () => {
return (
<FormField
horizontal
label="Label"
description="Description text"
error="Error text"
>
<TextInput error onChange={() => {}} />
</FormField>
)
}
108 changes: 108 additions & 0 deletions src/components/Form/FormField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { ReactNode } from 'react'
import { Text } from '../Text/index.js'
import { colors } from '../../utils/colors.js'
import { Icon } from '../Icon/index.js'

type FormFieldProps = {
label?: string
description?: string
error?: string
children: ReactNode
horizontal?: boolean
}

function FormField({
children,
label,
description,
error,
horizontal = false,
}: FormFieldProps) {
if (horizontal) {
return (
<div
style={{
flex: 1,
width: '100%',
display: 'flex',
gap: 16,
minHeight: 36,
}}
>
<div style={{ width: 160, paddingTop: 9.5 }}>
<Text variant="display-regular" color="neutral60">
{label}
</Text>
</div>
<div
style={{ flex: 1, display: 'flex', flexDirection: 'column', gap: 8 }}
>
{children}
{error && (
<div
style={{
display: 'flex',
alignItems: 'center',
gap: 4,
color: colors.red100,
}}
>
<Icon variant="error-filled" />
<Text color="inherit" variant="display-medium">
{error}
</Text>
</div>
)}
{description && (
<Text variant="display-regular" color="neutral60">
{description}
</Text>
)}
</div>
</div>
)
}

return (
<div
style={{
flex: 1,
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'start',
gap: 8,
}}
>
{label && (
<Text variant="display-regular" color="neutral60">
{label}
</Text>
)}
{children}
{error && (
<div
style={{
display: 'flex',
alignItems: 'center',
gap: 4,
color: colors.red100,
}}
>
<Icon variant="error-filled" />
<Text color="inherit" variant="display-medium">
{error}
</Text>
</div>
)}
{description && (
<Text variant="display-regular" color="neutral60">
{description}
</Text>
)}
</div>
)
}

export { FormField }
export type { FormFieldProps }
56 changes: 56 additions & 0 deletions src/components/Form/FormFieldGroup.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react'
import { FormFieldGroup } from './FormFieldGroup.js'
import { FormField } from './FormField.js'
import { TextInput } from '../TextInput/index.js'

export default {
title: 'FormFieldGroup',
component: FormFieldGroup,
}

export const Vertical = () => {
return (
<FormFieldGroup label="Group title">
<FormField label="Label">
<TextInput onChange={() => {}} />
</FormField>
<FormField label="Label" description="Description text">
<TextInput onChange={() => {}} />
</FormField>
<FormField label="Label" error="Error text">
<TextInput error onChange={() => {}} />
</FormField>
<FormField
label="Label"
description="Description text"
error="Error text"
>
<TextInput error onChange={() => {}} />
</FormField>
</FormFieldGroup>
)
}

export const Horizontal = () => {
return (
<FormFieldGroup label="Group title">
<FormField horizontal label="Label">
<TextInput onChange={() => {}} />
</FormField>
<FormField horizontal label="Label" description="Description text">
<TextInput onChange={() => {}} />
</FormField>
<FormField horizontal label="Label" error="Error text">
<TextInput error onChange={() => {}} />
</FormField>
<FormField
horizontal
label="Label"
description="Description text"
error="Error text"
>
<TextInput error onChange={() => {}} />
</FormField>
</FormFieldGroup>
)
}
31 changes: 31 additions & 0 deletions src/components/Form/FormFieldGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ReactNode } from 'react'
import { Text } from '../Text/index.js'
import { colors } from '../../utils/colors.js'

type FormFieldGroupProps = {
label?: string
children: ReactNode
}

function FormFieldGroup({ label, children }: FormFieldGroupProps) {
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
{label && (
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<Text variant="display-bold" color="neutral80">
{label}
</Text>
<div
style={{ height: 1, width: '100%', background: colors.neutral10 }}
/>
</div>
)}
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
{children}
</div>
</div>
)
}

export { FormFieldGroup }
export type { FormFieldGroupProps }
Loading

0 comments on commit bc6690b

Please sign in to comment.