|
| 1 | +import * as Fluent from '@fluentui/react' |
| 2 | +import React from 'react' |
| 3 | +import {Button, Buttons, XButtons, XStandAloneButton} from './button' |
| 4 | +import {Label, XLabel} from './label' |
| 5 | +import {cards} from './layout' |
| 6 | +import {bond, Card, Dict, Packed, S, unpack, xid} from './qd' |
| 7 | +import {Textbox, XTextbox} from './textbox' |
| 8 | +import {getTheme} from './theme' |
| 9 | +import {XToolTip} from './tooltip' |
| 10 | + |
| 11 | + |
| 12 | +/** Create a SearchBar. */ |
| 13 | +export interface SearchBar { |
| 14 | + /** Label. */ |
| 15 | + label?: Label; |
| 16 | + /** Textbox. */ |
| 17 | + textbox?: Textbox; |
| 18 | + /** Button. */ |
| 19 | + button?: Button; |
| 20 | + /** Button set. */ |
| 21 | + buttons?: Buttons; |
| 22 | +} |
| 23 | + |
| 24 | +/** Create a searchbar. */ |
| 25 | +interface State { |
| 26 | + /** The components in this searchbar. */ |
| 27 | + items: Packed<SearchBar[]>; |
| 28 | + |
| 29 | + /** SearchBar direction. */ |
| 30 | + direction?: 'horizontal' | 'vertical' |
| 31 | + |
| 32 | + /** SearchBar strategy for main axis. */ |
| 33 | + justify?: 'start' | 'end' | 'center' | 'between' | 'around' |
| 34 | + |
| 35 | + /** SearchBar strategy for cross axis. */ |
| 36 | + align?: 'start' | 'end' | 'center' | 'baseline' | 'stretch' |
| 37 | + |
| 38 | + /** SearchBar wrapping strategy. */ |
| 39 | + wrap?: 'start' | 'end' | 'center' | 'between' | 'around' | 'stretch' |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +const |
| 44 | + theme = getTheme(), |
| 45 | + defaults: Partial<State> = {items: []}, |
| 46 | + directions: Dict<S> = { |
| 47 | + horizontal: 'row', |
| 48 | + vertical: 'column', |
| 49 | + }, |
| 50 | + justifications: Dict<S> = { |
| 51 | + start: 'flex-start', |
| 52 | + end: 'flex-end', |
| 53 | + center: 'center', |
| 54 | + between: 'space-between', |
| 55 | + around: 'space-around', |
| 56 | + }, |
| 57 | + alignments: Dict<S> = { |
| 58 | + start: 'flex-start', |
| 59 | + end: 'flex-end', |
| 60 | + center: 'center', |
| 61 | + baseline: 'baseline', |
| 62 | + stretch: 'stretch', |
| 63 | + }, |
| 64 | + wrappings: Dict<S> = { |
| 65 | + start: 'flex-start', |
| 66 | + end: 'flex-end', |
| 67 | + center: 'center', |
| 68 | + between: 'space-between', |
| 69 | + around: 'space-around', |
| 70 | + stretch: 'stretch', |
| 71 | + }, |
| 72 | + toFlexStyle = (state: State): React.CSSProperties => { |
| 73 | + const |
| 74 | + css: React.CSSProperties = { display: 'flex', flexGrow: 1 }, |
| 75 | + direction = directions[state.direction || ''], |
| 76 | + justify = justifications[state.justify || ''], |
| 77 | + align = alignments[state.align || ''], |
| 78 | + wrap = wrappings[state.wrap || ''] |
| 79 | + |
| 80 | + if (direction) css.flexDirection = direction as any |
| 81 | + if (justify) css.justifyContent = justify |
| 82 | + if (align) css.alignItems = align |
| 83 | + if (wrap) { |
| 84 | + css.flexWrap = 'wrap' |
| 85 | + css.alignContent = wrap |
| 86 | + } |
| 87 | + return css |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | +export const |
| 92 | + XSearchBar = ({ items }: { items: SearchBar[] }) => { |
| 93 | + const components = items.map(m => <XStandardSearchBar key={xid()} model={m} />) |
| 94 | + return <>{components}</> |
| 95 | + } |
| 96 | + |
| 97 | +const |
| 98 | + XStandardSearchBar = ({ model: m }: { model: SearchBar }) => { |
| 99 | + if (m.label) return <XToolTip content={m.label.tooltip} expand={false}><XLabel model={m.label} /></XToolTip> |
| 100 | + if (m.textbox) return <XToolTip content={m.textbox.tooltip}><XTextbox model={m.textbox} /></XToolTip> |
| 101 | + if (m.buttons) return <XButtons model={m.buttons} /> |
| 102 | + if (m.button) return <XToolTip content={m.button.tooltip} showIcon={false} expand={false}><XStandAloneButton model={m.button} /></XToolTip> |
| 103 | + return <Fluent.MessageBar messageBarType={Fluent.MessageBarType.severeWarning}>This component could not be rendered.</Fluent.MessageBar> |
| 104 | + } |
| 105 | + |
| 106 | +export const |
| 107 | + View = bond(({ name, state, changed }: Card<State>) => { |
| 108 | + |
| 109 | + const |
| 110 | + render = () => { |
| 111 | + const |
| 112 | + s = theme.merge(defaults, state), |
| 113 | + items = unpack<SearchBar[]>(s.items) |
| 114 | + return ( |
| 115 | + <div data-test={name} style={toFlexStyle(state)}> |
| 116 | + <XSearchBar items={items} /> |
| 117 | + </div> |
| 118 | + ) |
| 119 | + |
| 120 | + } |
| 121 | + return { render, changed } |
| 122 | + }) |
| 123 | + |
| 124 | +cards.register('searchbar', View) |
| 125 | + |
0 commit comments