-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
searchbar_card can be used to horizontally align textbox and button on the requirement. Fixes: #372
- Loading branch information
1 parent
80def55
commit f51ee5a
Showing
7 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# SearchBar | ||
# SearchBar is used to align textbox and button components horizontally. | ||
# searchbar_card can be used to horizontally align textbox and button on the requirement. | ||
# --- | ||
from h2o_wave import site, ui | ||
|
||
page = site['/demo'] | ||
|
||
|
||
page["search_bar"] = ui.searchbar_card( | ||
box="1 1 -1 1", items=[ | ||
ui.textbox(name='text', label='', placeholder='#wave', multiline=False, trigger=False), | ||
ui.button(name="search", label="search", primary=True), | ||
], direction=ui.SearchBarDirection.ROW, justify=ui.SearchBarJustify.CENTER) | ||
|
||
page.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { View } from './form' | ||
import * as T from './qd' | ||
|
||
const | ||
name = 'searchbar', | ||
formProps: T.Card<any> = { | ||
name, | ||
state: {}, | ||
changed: T.box(false) | ||
} | ||
|
||
describe('Searchbar.tsx', () => { | ||
|
||
it('Renders data-test attr', () => { | ||
const { queryByTestId } = render(<View {...formProps} />) | ||
expect(queryByTestId(name)).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import * as Fluent from '@fluentui/react' | ||
import React from 'react' | ||
import {Button, Buttons, XButtons, XStandAloneButton} from './button' | ||
import {Label, XLabel} from './label' | ||
import {cards} from './layout' | ||
import {bond, Card, Dict, Packed, S, unpack, xid} from './qd' | ||
import {Textbox, XTextbox} from './textbox' | ||
import {getTheme} from './theme' | ||
import {XToolTip} from './tooltip' | ||
|
||
|
||
/** Create a SearchBar. */ | ||
export interface SearchBar { | ||
/** Label. */ | ||
label?: Label; | ||
/** Textbox. */ | ||
textbox?: Textbox; | ||
/** Button. */ | ||
button?: Button; | ||
/** Button set. */ | ||
buttons?: Buttons; | ||
} | ||
|
||
/** Create a searchbar. */ | ||
interface State { | ||
/** The components in this searchbar. */ | ||
items: Packed<SearchBar[]>; | ||
|
||
/** SearchBar direction. */ | ||
direction?: 'horizontal' | 'vertical' | ||
|
||
/** SearchBar strategy for main axis. */ | ||
justify?: 'start' | 'end' | 'center' | 'between' | 'around' | ||
|
||
/** SearchBar strategy for cross axis. */ | ||
align?: 'start' | 'end' | 'center' | 'baseline' | 'stretch' | ||
|
||
/** SearchBar wrapping strategy. */ | ||
wrap?: 'start' | 'end' | 'center' | 'between' | 'around' | 'stretch' | ||
} | ||
|
||
|
||
const | ||
theme = getTheme(), | ||
defaults: Partial<State> = {items: []}, | ||
directions: Dict<S> = { | ||
horizontal: 'row', | ||
vertical: 'column', | ||
}, | ||
justifications: Dict<S> = { | ||
start: 'flex-start', | ||
end: 'flex-end', | ||
center: 'center', | ||
between: 'space-between', | ||
around: 'space-around', | ||
}, | ||
alignments: Dict<S> = { | ||
start: 'flex-start', | ||
end: 'flex-end', | ||
center: 'center', | ||
baseline: 'baseline', | ||
stretch: 'stretch', | ||
}, | ||
wrappings: Dict<S> = { | ||
start: 'flex-start', | ||
end: 'flex-end', | ||
center: 'center', | ||
between: 'space-between', | ||
around: 'space-around', | ||
stretch: 'stretch', | ||
}, | ||
toFlexStyle = (state: State): React.CSSProperties => { | ||
const | ||
css: React.CSSProperties = { display: 'flex', flexGrow: 1 }, | ||
direction = directions[state.direction || ''], | ||
justify = justifications[state.justify || ''], | ||
align = alignments[state.align || ''], | ||
wrap = wrappings[state.wrap || ''] | ||
|
||
if (direction) css.flexDirection = direction as any | ||
if (justify) css.justifyContent = justify | ||
if (align) css.alignItems = align | ||
if (wrap) { | ||
css.flexWrap = 'wrap' | ||
css.alignContent = wrap | ||
} | ||
return css | ||
} | ||
|
||
|
||
export const | ||
XSearchBar = ({ items }: { items: SearchBar[] }) => { | ||
const components = items.map(m => <XStandardSearchBar key={xid()} model={m} />) | ||
return <>{components}</> | ||
} | ||
|
||
const | ||
XStandardSearchBar = ({ model: m }: { model: SearchBar }) => { | ||
if (m.label) return <XToolTip content={m.label.tooltip} expand={false}><XLabel model={m.label} /></XToolTip> | ||
if (m.textbox) return <XToolTip content={m.textbox.tooltip}><XTextbox model={m.textbox} /></XToolTip> | ||
if (m.buttons) return <XButtons model={m.buttons} /> | ||
if (m.button) return <XToolTip content={m.button.tooltip} showIcon={false} expand={false}><XStandAloneButton model={m.button} /></XToolTip> | ||
return <Fluent.MessageBar messageBarType={Fluent.MessageBarType.severeWarning}>This component could not be rendered.</Fluent.MessageBar> | ||
} | ||
|
||
export const | ||
View = bond(({ name, state, changed }: Card<State>) => { | ||
|
||
const | ||
render = () => { | ||
const | ||
s = theme.merge(defaults, state), | ||
items = unpack<SearchBar[]>(s.items) | ||
return ( | ||
<div data-test={name} style={toFlexStyle(state)}> | ||
<XSearchBar items={items} /> | ||
</div> | ||
) | ||
|
||
} | ||
return { render, changed } | ||
}) | ||
|
||
cards.register('searchbar', View) | ||
|