Skip to content

Commit

Permalink
fix: stop propagation in the toolbar (#17716)
Browse files Browse the repository at this point in the history
* fix: stop propagation in the toolbar edit action form when typing

* and step fields
  • Loading branch information
pauldambra authored Oct 2, 2023
1 parent 10d8a9b commit 38a73fa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
15 changes: 15 additions & 0 deletions frontend/src/lib/lemon-ui/LemonInput/LemonInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ interface LemonInputPropsBase
onPressEnter?: (event: React.KeyboardEvent<HTMLInputElement>) => void
'data-attr'?: string
'aria-label'?: string
/** Whether to stop propagation of events from the input */
stopPropagation?: boolean
}

export interface LemonInputPropsText extends LemonInputPropsBase {
Expand Down Expand Up @@ -80,6 +82,7 @@ export const LemonInput = React.forwardRef<HTMLInputElement, LemonInputProps>(fu
value,
transparentBackground = false,
size = 'medium',
stopPropagation = false,
...textProps
},
ref
Expand Down Expand Up @@ -160,6 +163,9 @@ export const LemonInput = React.forwardRef<HTMLInputElement, LemonInputProps>(fu
type={(type === 'password' && passwordVisible ? 'text' : type) || 'text'}
value={value}
onChange={(event) => {
if (stopPropagation) {
event.stopPropagation()
}
if (type === 'number') {
onChange?.(
!isNaN(event.currentTarget.valueAsNumber) ? event.currentTarget.valueAsNumber : undefined
Expand All @@ -169,14 +175,23 @@ export const LemonInput = React.forwardRef<HTMLInputElement, LemonInputProps>(fu
}
}}
onFocus={(event) => {
if (stopPropagation) {
event.stopPropagation()
}
setFocused(true)
onFocus?.(event)
}}
onBlur={(event) => {
if (stopPropagation) {
event.stopPropagation()
}
setFocused(false)
onBlur?.(event)
}}
onKeyDown={(event) => {
if (stopPropagation) {
event.stopPropagation()
}
if (onPressEnter && event.key === 'Enter') {
onPressEnter(event)
}
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/lib/lemon-ui/LemonTextArea/LemonTextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export interface LemonTextAreaProps
minRows?: number
maxRows?: number
rows?: number
/** Whether to stop propagation of events from the input */
stopPropagation?: boolean
}

/** A `textarea` component for multi-line text. */
export const LemonTextArea = React.forwardRef<HTMLTextAreaElement, LemonTextAreaProps>(function _LemonTextArea(
{ className, onChange, onPressCmdEnter: onPressEnter, minRows = 3, onKeyDown, ...textProps },
{ className, onChange, onPressCmdEnter: onPressEnter, minRows = 3, onKeyDown, stopPropagation, ...textProps },
ref
): JSX.Element {
const _ref = useRef<HTMLTextAreaElement | null>(null)
Expand All @@ -50,13 +52,21 @@ export const LemonTextArea = React.forwardRef<HTMLTextAreaElement, LemonTextArea
ref={textRef}
className={clsx('LemonTextArea', className)}
onKeyDown={(e) => {
if (stopPropagation) {
e.stopPropagation()
}
if (onPressEnter && e.key === 'Enter' && (e.metaKey || e.ctrlKey)) {
onPressEnter(textProps.value?.toString() ?? '')
}

onKeyDown?.(e)
}}
onChange={(event) => onChange?.(event.currentTarget.value ?? '')}
onChange={(event) => {
if (stopPropagation) {
event.stopPropagation()
}
return onChange?.(event.currentTarget.value ?? '')
}}
{...textProps}
/>
)
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/toolbar/actions/EditAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export function EditAction(): JSX.Element {
<div className="mb-4">
<p>What did your user do?</p>
<Field name="name">
<LemonInput placeholder="E.g: Clicked Sign Up" className="action-title-field" />
<LemonInput
placeholder="E.g: Clicked Sign Up"
className="action-title-field"
stopPropagation={true}
/>
</Field>
</div>

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/toolbar/actions/StepField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ export function StepField({ step, item, label, caption }: StepFieldProps): JSX.E
className={clsx(!selected && 'opacity-50')}
onChange={onChange}
value={value ?? ''}
stopPropagation={true}
/>
) : (
<LemonInput
className={clsx(!selected && 'opacity-50')}
onChange={onChange}
value={value ?? ''}
stopPropagation={true}
/>
)
}}
Expand Down

0 comments on commit 38a73fa

Please sign in to comment.