Skip to content

Add enter handler for the textbox #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions py/examples/textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ async def serve(q: Q):
ui.text(f'textbox_multiline={q.args.textbox_multiline}'),
ui.button(name='show_form', label='Back', primary=True),
]
elif q.args.enter_key_handler:
q.page['example'].items = [
ui.text(f'textbox_enter_key_handler={q.args.enter_key_handler}'),
ui.button(name='show_form', label='Back', primary=True),
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess a better scenario would be to show every input filled (add the enter handler logic to q.args.show if branch instead), not just enter handler as the enter submits the whole form, not just a single item and might cause confusion for some people. @lo5 wdyt.

else:
q.page['example'] = ui.form_card(box='1 1 4 10', items=[
ui.textbox(name='textbox', label='Standard'),
Expand All @@ -36,6 +41,7 @@ async def serve(q: Q):
ui.textbox(name='textbox_placeholder', label='With placeholder', placeholder='I need some input'),
ui.textbox(name='textbox_disabled_placeholder', label='Disabled with placeholder', disabled=True,
placeholder='I am disabled'),
ui.textbox(name='enter_key_handler', label='Submits the textbox value on Enter key', icon='Search'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go with convention format: textbox_*** so textbox_enter or something for your case.

ui.textbox(name='textbox_multiline', label='Multiline textarea', multiline=True),
ui.button(name='show_inputs', label='Submit', primary=True),
])
Expand Down
33 changes: 32 additions & 1 deletion ui/src/textbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,35 @@ describe('Textbox.tsx', () => {

expect(syncMock).not.toBeCalled()
})
})

it('Calls sync on key up - When the key is Enter key', () => {
const { getByTestId } = render(<XTextbox model={textboxProps} />)
const syncMock = jest.fn()

T.qd.sync = syncMock
fireEvent.keyUp(getByTestId(name), { key: 'Enter', target: { value: 'text' } })

expect(syncMock).toBeCalled()
})

it('Does not call sync on key up - When the key is not Enter key', () => {
const { getByTestId } = render(<XTextbox model={textboxProps} />)
const syncMock = jest.fn()

T.qd.sync = syncMock
fireEvent.keyUp(getByTestId(name), { key: 'A', target: { value: 'text' } })

expect(syncMock).not.toBeCalled()
})

it('Does not call sync on key up - When multiline is true', () => {
const { getByTestId } = render(<XTextbox model={{ ...textboxProps, multiline: true}} />)
const syncMock = jest.fn()

T.qd.sync = syncMock
fireEvent.keyUp(getByTestId(name), { key: 'Enter', target: { value: 'text' } })

expect(syncMock).not.toBeCalled()
})

})
9 changes: 9 additions & 0 deletions ui/src/textbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export const
qd.args[m.name] = v ?? (m.value || '')
if (m.trigger) qd.sync()
},
onKeyUp = ( event: React.KeyboardEvent<HTMLInputElement>, v?: string) => {
if ((event).key == "Enter" && event.target instanceof HTMLInputElement) {
v = v || (event.target as HTMLInputElement).value
qd.args[m.name] = v ?? (m.value || '')
qd.sync()
}
},
render = () => m.mask
? (
<Fluent.MaskedTextField
Expand All @@ -88,6 +95,7 @@ export const
disabled={m.disabled}
readOnly={m.readonly}
onChange={m.trigger ? debounce(DEBOUNCE_TIMEOUT, onChange) : onChange}
onKeyUp={onKeyUp}
/>
)
: (
Expand All @@ -108,6 +116,7 @@ export const
multiline={m.multiline}
type={m.password ? 'password' : undefined}
onChange={m.trigger ? debounce(DEBOUNCE_TIMEOUT, onChange) : onChange}
onKeyUp={onKeyUp}
/>
)

Expand Down