:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import type { Meta, StoryObj } from '@storybook/react'
import { MakeLogicType, kea, path, useAllValues } from 'kea'
import { Form, forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { LemonButton, LemonCheckbox, LemonInput, LemonSelect, LemonTextArea } from '@posthog/lemon-ui'
import { LemonField, LemonFieldProps } from './LemonField'
type Story = StoryObj<LemonFieldProps>
const meta: Meta<LemonFieldProps> = {
title: 'Lemon UI/Lemon Field',
component: LemonField,
parameters: {
docs: {
description: {
component: `
[Related Figma area](https://www.figma.com/file/Y9G24U4r04nEjIDGIEGuKI/PostHog-Design-System-One?node-id=3139%3A1388)
Fields are a wrapping component that take care of rendering a label, input and error messages in a standard format.
They can be used in a kea-forms controlled way via \`Field\` or a pure way via \`PureField\`.
`,
},
},
},
tags: ['autodocs'],
}
export default meta
export const _PureFields: Story = {
render: () => {
return (
<div className="deprecated-space-y-4">
<LemonField.Pure
label="Text input label"
showOptional
help={
<>
Optional descriptive or supportive text for the preceeding form element. This content can
wrap to multiple lines.
</>
}
>
<LemonInput placeholder="Optional descriptive placeholder text" />
</LemonField.Pure>
<LemonField.Pure label="Select label" info={<>With info!</>}>
<LemonSelect options={[{ value: 'foo', label: 'bar' }]} fullWidth />
</LemonField.Pure>
<LemonField.Pure label="Textarea label" error="This field has an error">
<LemonTextArea />
</LemonField.Pure>
<LemonField.Pure>
<LemonCheckbox bordered label="Checkbox labels are set differently" fullWidth />
</LemonField.Pure>
<div className="flex justify-end gap-2 border-t mt-4 pt-4">
<LemonButton type="secondary">Cancel</LemonButton>
<LemonButton htmlType="submit" type="primary">
Submit
</LemonButton>
</div>
</div>
)
},
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface formLogicValues {
isMyFormSubmitting: boolean
isMyFormValid: boolean
isSimpleFormSubmitting: boolean
isSimpleFormValid: boolean
myForm: {
email: string
name: string
pineappleOnPizza: false
}
myFormAllErrors: Record<string, any>
myFormChanged: boolean
myFormErrors: DeepPartialMap<
{
email: string
name: string
pineappleOnPizza: false
},
ValidationErrorType
>
myFormHasErrors: boolean
myFormManualErrors: Record<string, any>
myFormTouched: boolean
myFormTouches: Record<string, boolean>
myFormValidationErrors: DeepPartialMap<
{
email: string
name: string
pineappleOnPizza: false
},
ValidationErrorType
>
showMyFormErrors: boolean
showSimpleFormErrors: boolean
simpleForm: {
name: string
}
simpleFormAllErrors: Record<string, any>
simpleFormChanged: boolean
simpleFormErrors: DeepPartialMap<
{
name: string
},
ValidationErrorType
>
simpleFormHasErrors: boolean
simpleFormManualErrors: Record<string, any>
simpleFormTouched: boolean
simpleFormTouches: Record<string, boolean>
simpleFormValidationErrors: DeepPartialMap<
{
name: string
},
ValidationErrorType
>
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface formLogicActions {
resetMyForm: (values?: { email: string; name: string; pineappleOnPizza: false }) => {
values?: {
email: string
name: string
pineappleOnPizza: false
}
}
resetSimpleForm: (values?: { name: string }) => {
values?: {
name: string
}
}
setMyFormManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setMyFormValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setMyFormValues: (
values: DeepPartial<{
email: string
name: string
pineappleOnPizza: false
}>
) => {
values: DeepPartial<{
email: string
name: string
pineappleOnPizza: false
}>
}
setSimpleFormManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setSimpleFormValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setSimpleFormValues: (
values: DeepPartial<{
name: string
}>
) => {
values: DeepPartial<{
name: string
}>
}
submitMyForm: () => {
value: boolean
}
submitMyFormFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitMyFormRequest: (myForm: { email: string; name: string; pineappleOnPizza: false }) => {
myForm: {
email: string
name: string
pineappleOnPizza: false
}
}
submitMyFormSuccess: (myForm: { email: string; name: string; pineappleOnPizza: false }) => {
myForm: {
email: string
name: string
pineappleOnPizza: false
}
}
submitSimpleForm: () => {
value: boolean
}
submitSimpleFormFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitSimpleFormRequest: (simpleForm: { name: string }) => {
simpleForm: {
name: string
}
}
submitSimpleFormSuccess: (simpleForm: { name: string }) => {
simpleForm: {
name: string
}
}
touchMyFormField: (key: string) => {
key: string
}
touchSimpleFormField: (key: string) => {
key: string
}
}
export type formLogicType = MakeLogicType<formLogicValues, formLogicActions>
const formLogic = kea<formLogicType>([
path(['lib', 'forms', 'Field', 'stories']),
forms(({ actions }) => ({
myForm: {
defaults: {
name: '',
email: '',
pineappleOnPizza: false,
},
errors: ({ name, email, pineappleOnPizza }) => ({
name: !name ? 'Please enter your name' : null,
email: !email ? 'Please enter your email' : !email.includes('@') ? 'not a valid email' : null,
pineappleOnPizza: pineappleOnPizza ? 'I think you meant to leave this unchecked...' : null,
}),
submit: async (_, breakpoint) => {
await breakpoint(3000)
actions.resetMyForm()
},
},
simpleForm: {
defaults: {
name: '',
},
errors: ({ name }) => ({
name: !name ? 'Please enter your name' : undefined,
}),
submit: async (_, breakpoint) => {
await breakpoint(3000)
actions.resetSimpleForm()
},
},
})),
])
function useSpecificFormValues(formKey: string): Record<string, any> {
const allValues = useAllValues(formLogic)
return Object.fromEntries(
Object.entries(allValues).filter(([key]) => key.toLowerCase().includes(formKey.toLowerCase()))
)
}
export const _FieldsWithKeaForm: Story = {
render: () => {
const formKey = 'myForm'
const formValues = useSpecificFormValues(formKey)
return (
<Form logic={formLogic} formKey={formKey} enableFormOnSubmit>
<div className="deprecated-space-y-4">
<LemonField
name="name"
label={
<>
Name <span>(What should we call you?)</span>
</>
}
help={
<>
Optional descriptive or supportive text for the preceeding form element. This content
can wrap to multiple lines.
</>
}
>
<LemonInput placeholder="Jon Snow" />
</LemonField>
<LemonField name="select" label="Select label" info={<>With info!</>}>
<LemonSelect options={[{ value: 'foo', label: 'bar' }]} fullWidth />
</LemonField>
<LemonField name="email" label="Email address">
<LemonInput type="email" />
</LemonField>
<LemonField name="pineappleOnPizza">
<LemonCheckbox bordered label="Pineapple on your pizza?" fullWidth />
</LemonField>
<div className="flex justify-end gap-2 border-t mt-4 pt-4">
<LemonButton type="secondary">Cancel</LemonButton>
<LemonButton htmlType="submit" type="primary">
Submit
</LemonButton>
</div>
<pre className="rounded-lg text-bg-light bg-default p-2 m-2">
formLogic.values = {JSON.stringify(formValues, null, 2)}
</pre>
</div>
</Form>
)
},
}