Skip to content

Commit

Permalink
Introduce submit attribute to textbox
Browse files Browse the repository at this point in the history
The implementation conditionally calls the keyup handler based on the values of the submit attribute.
Fixes: #372
  • Loading branch information
VijithaEkanayake committed Dec 18, 2020
1 parent 6737d9f commit be42109
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
10 changes: 3 additions & 7 deletions py/examples/textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@app('/demo')
async def serve(q: Q):
if q.args.show_inputs:
if q.args.show_inputs or q.args.textbox_submit:
q.page['example'].items = [
ui.text(f'textbox={q.args.textbox}'),
ui.text(f'textbox_disabled={q.args.textbox_disabled}'),
Expand All @@ -20,11 +20,7 @@ async def serve(q: Q):
ui.text(f'textbox_placeholder={q.args.textbox_placeholder}'),
ui.text(f'textbox_disabled_placeholder={q.args.textbox_disabled_placeholder}'),
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.text(f'textbox_enter={q.args.textbox_enter}'),
ui.button(name='show_form', label='Back', primary=True),
]
else:
Expand All @@ -41,7 +37,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'),
ui.textbox(name='textbox_submit', label='Submits on enter pressed', icon='Search', submit=True),
ui.textbox(name='textbox_multiline', label='Multiline textarea', multiline=True),
ui.button(name='show_inputs', label='Submit', primary=True),
])
Expand Down
7 changes: 7 additions & 0 deletions py/h2o_wave/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ def __init__(
height: Optional[str] = None,
visible: Optional[bool] = None,
tooltip: Optional[str] = None,
submit: Optional[bool] = None,
):
self.name = name
"""An identifying name for this component."""
Expand Down Expand Up @@ -983,6 +984,8 @@ def __init__(
"""True if the component should be visible. Defaults to true."""
self.tooltip = tooltip
"""An optional tooltip message displayed when a user clicks the help icon to the right of the component."""
self.submit = submit
"""True if the form should be submitted when enter key pressed."""

def dump(self) -> Dict:
"""Returns the contents of this object as a dict."""
Expand All @@ -1007,6 +1010,7 @@ def dump(self) -> Dict:
height=self.height,
visible=self.visible,
tooltip=self.tooltip,
submit=self.submit,
)

@staticmethod
Expand All @@ -1032,6 +1036,7 @@ def load(__d: Dict) -> 'Textbox':
__d_height: Any = __d.get('height')
__d_visible: Any = __d.get('visible')
__d_tooltip: Any = __d.get('tooltip')
__d_submit: Any = __d.get('submit')
name: str = __d_name
label: Optional[str] = __d_label
placeholder: Optional[str] = __d_placeholder
Expand All @@ -1050,6 +1055,7 @@ def load(__d: Dict) -> 'Textbox':
height: Optional[str] = __d_height
visible: Optional[bool] = __d_visible
tooltip: Optional[str] = __d_tooltip
submit: Optional[bool] = __d_submit
return Textbox(
name,
label,
Expand All @@ -1069,6 +1075,7 @@ def load(__d: Dict) -> 'Textbox':
height,
visible,
tooltip,
submit,
)


Expand Down
3 changes: 3 additions & 0 deletions py/h2o_wave/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ def textbox(
height: Optional[str] = None,
visible: Optional[bool] = None,
tooltip: Optional[str] = None,
submit: Optional[bool] = None,
) -> Component:
"""Create a text box.
Expand All @@ -476,6 +477,7 @@ def textbox(
height: The height of the text box, e.g. '100px'. Applicable only if `multiline` is true.
visible: True if the component should be visible. Defaults to true.
tooltip: An optional tooltip message displayed when a user clicks the help icon to the right of the component.
submit: True if the form should be submitted when enter key pressed.
Returns:
A `h2o_wave.types.Textbox` instance.
"""
Expand All @@ -498,6 +500,7 @@ def textbox(
height,
visible,
tooltip,
submit,
))


Expand Down
22 changes: 16 additions & 6 deletions ui/src/textbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ describe('Textbox.tsx', () => {
expect(syncMock).not.toBeCalled()
})

it('Calls sync on enter pressed', () => {
const { getByTestId } = render(<XTextbox model={textboxProps} />)
it('Calls sync on enter pressed - submit specified', () => {
const { getByTestId } = render(<XTextbox model={{ ...textboxProps, submit: true}} />)
const syncMock = jest.fn()

T.qd.sync = syncMock
Expand All @@ -112,8 +112,8 @@ describe('Textbox.tsx', () => {
expect(syncMock).toBeCalled()
})

it('Does not call sync when key pressed is not enter', () => {
const { getByTestId } = render(<XTextbox model={textboxProps} />)
it('Does not call sync when key pressed is not enter - submit specified', () => {
const { getByTestId } = render(<XTextbox model={{ ...textboxProps, submit: true}} />)
const syncMock = jest.fn()

T.qd.sync = syncMock
Expand All @@ -122,8 +122,18 @@ describe('Textbox.tsx', () => {
expect(syncMock).not.toBeCalled()
})

it('Does not call sync on enter - multiline is true', () => {
const { getByTestId } = render(<XTextbox model={{ ...textboxProps, multiline: true}} />)
it('Does not call sync on enter pressed - submit not specified', () => {
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).not.toBeCalled()
})

it('Does not call sync on enter - multiline and submit both are true', () => {
const { getByTestId } = render(<XTextbox model={{ ...textboxProps, multiline: true, submit: true}} />)
const syncMock = jest.fn()

T.qd.sync = syncMock
Expand Down
6 changes: 4 additions & 2 deletions ui/src/textbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export interface Textbox {
visible?: B
/** An optional tooltip message displayed when a user clicks the help icon to the right of the component. */
tooltip?: S
/** True if the form should be submitted when enter key is pressed. */
submit?: B
}

const DEBOUNCE_TIMEOUT = 500
Expand Down Expand Up @@ -95,7 +97,7 @@ export const
disabled={m.disabled}
readOnly={m.readonly}
onChange={m.trigger ? debounce(DEBOUNCE_TIMEOUT, onChange) : onChange}
onKeyUp={onKeyUp}
onKeyUp={m.submit ? onKeyUp: undefined}
/>
)
: (
Expand All @@ -116,7 +118,7 @@ export const
multiline={m.multiline}
type={m.password ? 'password' : undefined}
onChange={m.trigger ? debounce(DEBOUNCE_TIMEOUT, onChange) : onChange}
onKeyUp={onKeyUp}
onKeyUp={m.submit ? onKeyUp: undefined}
/>
)

Expand Down

0 comments on commit be42109

Please sign in to comment.