Skip to content

Commit

Permalink
chore: update new property form (#18576)
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin authored Nov 14, 2023
1 parent 34a6bc4 commit 7a11387
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CopyToClipboardInline } from '../CopyToClipboard'
import { useValues } from 'kea'
import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { NewPropertyComponent } from 'scenes/persons/NewPropertyComponent'
import { NewProperty } from 'scenes/persons/NewProperty'
import { LemonCheckbox, LemonInput, Link } from '@posthog/lemon-ui'
import clsx from 'clsx'
import { PropertyDefinitionType } from '~/types'
Expand Down Expand Up @@ -351,7 +351,7 @@ export function PropertiesTable({
)}
</span>

{onEdit && <NewPropertyComponent editProperty={onEdit} />}
{onEdit && <NewProperty onSave={onEdit} />}
</div>
)}

Expand Down
129 changes: 129 additions & 0 deletions frontend/src/scenes/persons/NewProperty.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { useState } from 'react'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonInput, LemonLabel, LemonModal, LemonSegmentedButton } from '@posthog/lemon-ui'

interface NewPropertyInterface {
creating: boolean
propertyType: 'string' | 'boolean'
key?: string | null
value?: string | number
}

export interface NewPropertyProps {
onSave: (key: string, newValue?: string | number) => void
}

export function NewProperty({ onSave }: NewPropertyProps): JSX.Element {
const initialState = { creating: false, propertyType: 'string', value: '' } as NewPropertyInterface
const [state, setState] = useState(initialState)

const saveProperty = (): void => {
if (state.key && state.value !== undefined) {
onSave(state.key, state.value)
setState(initialState)
}
}

return (
<>
<LemonButton
data-attr="add-prop-button"
onClick={() => setState({ ...state, creating: true })}
type="primary"
>
New property
</LemonButton>
<LemonModal
isOpen={state.creating}
onClose={() => setState(initialState)}
title="Add new property"
footer={
<LemonButton
disabledReason={(!state.key || state.value === undefined) && 'Set a key and a value'}
type="secondary"
onClick={saveProperty}
>
Save
</LemonButton>
}
>
<div className="space-y-2">
<div>
<LemonLabel>Key</LemonLabel>
<LemonInput
id="propertyKey"
autoFocus
placeholder="try email, first_name, is_verified, membership_level, total_revenue"
onChange={(key) => setState({ ...state, key: key })}
autoComplete="off"
autoCapitalize="off"
/>
</div>
<div>
<LemonLabel>Type of Property</LemonLabel>
<LemonSegmentedButton
onChange={(value: 'string' | 'boolean') =>
setState({
...state,
propertyType: value,
value: value === 'string' ? '' : 'true',
})
}
value={state.propertyType}
options={[
{
value: 'string',
label: 'Text or Number',
},
{
value: 'boolean',
label: 'Boolean or Null',
},
]}
fullWidth
/>
</div>
<div>
<LemonLabel>Value</LemonLabel>
{state.propertyType === 'boolean' ? (
<LemonSegmentedButton
onChange={(value) =>
setState({
...state,
value: value,
})
}
fullWidth
value={state.value}
options={[
{
value: 'true',
label: 'True',
},
{
value: 'false',
label: 'False',
},
{
value: 'null',
label: 'Null',
},
]}
size="small"
/>
) : (
<LemonInput
id="propertyValue"
placeholder="try [email protected], gold, 1"
onChange={(value) => setState({ ...state, value: value })}
onKeyDown={(e) => e.key === 'Enter' && saveProperty()}
autoComplete="off"
autoCapitalize="off"
/>
)}
</div>
</div>
</LemonModal>
</>
)
}
126 changes: 0 additions & 126 deletions frontend/src/scenes/persons/NewPropertyComponent.tsx

This file was deleted.

0 comments on commit 7a11387

Please sign in to comment.