Skip to content

Commit

Permalink
Merge pull request #159 from korbinancell/listbox
Browse files Browse the repository at this point in the history
Add Listbox and Parameter Sentence component
  • Loading branch information
korbinancell committed May 14, 2019
2 parents 57fd15a + 8c88e9d commit 19e890a
Show file tree
Hide file tree
Showing 34 changed files with 1,176 additions and 148 deletions.
80 changes: 80 additions & 0 deletions catalog/index.js
Expand Up @@ -40,6 +40,13 @@ import {
MenuItem,
MenuSeparator,
MenuCheckbox,
Listbox,
ListboxToggle,
ListboxMenu,
ListItem,
ParameterSelect,
ParameterInputBox,
ParameterSentence,
} from '../components/main';
import { BaseButton } from '../components/button/base-button';
import { GroupSelector, LargeGroupSelector } from '../components/group-selector';
Expand All @@ -50,6 +57,7 @@ import { ProductDrawerWithResources } from './product-drawer';
import { DocgenTable } from './docgen-table';
import { MemberDirectory, VolunteerScheduling } from './grid';
import { textInputPages } from './text-input/pages';
import DownArrow from './svgs/arrow-down.svg';

// SVG icons embedded in SASS stylesheets do not work properly with catalog,
// so the stylesheets must be built by a separate webpack build.
Expand Down Expand Up @@ -724,6 +732,78 @@ const components = [
},
],
},
{
title: 'Listbox',
pages: [
{
path: '/Listbox/variations',
title: 'Listbox',
content: pageLoader(() => import('./listbox/variations.md')),
imports: {
Listbox,
ListboxToggle,
ListboxMenu,
ListItem,
ListboxDemo: styled.div`
display: flex;
align-items: baseline;
`,
Button,
browserList: ['Firefox', 'Chrome', 'Opera', 'Edge'],
Label: styled.span`
margin-right: 8px;
`,
DownArrow: styled.img.attrs({ src: DownArrow })``,
},
},
{
path: '/listbox/documentation',
title: 'Listbox Documentation',
content: pageLoader(() => import('./listbox/documentation.md')),
imports: { Listbox, ListboxToggle, ListItem, DocgenTable },
},
],
},
{
title: 'Parameter Sentence',
pages: [
{
path: '/parameter-sentence/variations',
title: 'Parameter Sentence',
content: pageLoader(() => import('./parameter-sentence/variations.md')),
imports: {
ParameterSelect,
ParameterSentenceDemo: styled.div`
display: flex;
align-items: baseline;
${({ addMargin }) => addMargin && '&& > * { margin-right: 16px; }'};
`,
scheduleOptions: {
weekly: 'weekly',
biweekly: 'biweekly',
twiceMonthly: 'twice-monthly',
monthly: 'monthly',
quarterly: 'quarterly',
annual: 'annual',
},
ParameterInputBox,
ParameterSentence,
},
},
{
path: '/parameter-sentence/command-sentence',
title: 'Command Sentence',
content: pageLoader(() => import('./parameter-sentence/command-sentence.md')),
imports: {},
},
{
path: '/parameter-sentence/documentation',
title: 'Parameter Sentence Documentation',
content: pageLoader(() => import('./parameter-sentence/documentation.md')),
imports: { ParameterSentence, ParameterSelect, ParameterInputBox, DocgenTable },
},
],
},
].sort((a, b) => {
if (a.title < b.title) {
return -1;
Expand Down
19 changes: 19 additions & 0 deletions catalog/listbox/documentation.md
@@ -0,0 +1,19 @@
This documentation is automatically generated from jsdoc comments.

```react
noSource: true
---
<DocgenTable component={Listbox} />
```

```react
noSource: true
---
<DocgenTable component={ListboxToggle} />
```

```react
noSource: true
---
<DocgenTable component={ListItem} />
```
25 changes: 25 additions & 0 deletions catalog/listbox/variations.md
@@ -0,0 +1,25 @@
## Listbox

A list box should be used in situations simmilar to a html select. Refer to the dropdown docs for more info on varriations.

```react
showSource: true
state: { isOpen: false, selected: 0 }
---
<ListboxDemo>
<Label id="listboxLabel">Pick your favorite browser:</Label>
<Listbox
isOpen={state.isOpen}
onItemSelect={id => setState({ selected: id })}
selectedId={state.selected}
onToggleMenu={() => setState({ isOpen: !state.isOpen })}
labelledBy="listboxLabel"
>
<ListboxToggle primary medium icon={<DownArrow />} styleOverrides={{width: '100px'}}>{browserList[state.selected]}</ListboxToggle>
<ListboxMenu>
{browserList.map((name, index) => <ListItem id={index}>{name}</ListItem>)}
<ListItem id="ie" disabled>Internet Explorer</ListItem>
</ListboxMenu>
</Listbox>
</ListboxDemo>
```
54 changes: 54 additions & 0 deletions catalog/parameter-sentence/command-sentence.md
@@ -0,0 +1,54 @@
## Parameter Sentence with CommandSentence

Parameter sentences are made to work with [@faithlife/command-sentence-control](https://git/Logos/command-sentence-control) (git enterprise link)

Below is an example usage

```code
lang: jsx
---
<ParameterSentenceDemo>
<ParameterSentence accessibilityFormLabel="Tithe Calculator">
<CommandSentence
template={'I want to give %PERCENTAGE% (%PREPOST% 19% taxes) of my %SCHEDULE% income of %INCOME%.'}
>
<CommandSentence.Field name="PERCENTAGE">
<ParameterInputBox
defaultValue="10"
value={state.percentage}
onChange={event => setState({ percentage: event.target.value })}
formatValue={val => `${val}%`}
width="35px"
accessibilityLabel={'Percent of income to tithe'}
/>
</CommandSentence.Field>
<CommandSentence.Field name="PREPOST">
<ParameterSelect
selectedId={state.prepost}
onItemSelect={item => setState({ prepost: item })}
options={prePostOptions}
accessibilityLabel={'Should give tithe before or after taxes'}
/>
</CommandSentence.Field>
<CommandSentence.Field name="SCHEDULE">
<ParameterSelect
selectedId={state.schedule}
onItemSelect={item => setState({ schedule: item })}
options={scheduleOptions}
accessibilityLabel={'Pay schedule of income'}
/>
</CommandSentence.Field>
<CommandSentence.Field name="INCOME">
<ParameterInputBox
defaultValue="55700"
value={state.income}
onChange={event => setState({ income: event.target.value })}
formatValue={val => `$${val}`}
width="50px"
accessibilityLabel={'Income per pay schedule period'}
/>
</CommandSentence.Field>
</CommandSentence>
</ParameterSentence>
</ParameterSentenceDemo>
```
19 changes: 19 additions & 0 deletions catalog/parameter-sentence/documentation.md
@@ -0,0 +1,19 @@
This documentation is automatically generated from jsdoc comments.

```react
noSource: true
---
<DocgenTable component={ParameterSentence} />
```

```react
noSource: true
---
<DocgenTable component={ParameterSelect} />
```

```react
noSource: true
---
<DocgenTable component={ParameterInputBox} />
```
98 changes: 98 additions & 0 deletions catalog/parameter-sentence/variations.md
@@ -0,0 +1,98 @@
## Desktop Parameter Sentence

Note to designers: under the hood a parameter sentence is seen as a form by screen readers. Including a small description of each parameter as if it was a form label will go a long way towards keeping it accessible.

```react
showSource: true
state: {
isOpen: false,
prepost: 'after',
schedule: 'annual',
percentage: 10,
income: 55700,
}
---
<ParameterSentenceDemo>
<ParameterSentence accessibilityFormLabel="Tithe Calculator">
{'I want to give '}
<ParameterInputBox
defaultValue="10"
value={state.percentage}
onChange={event => setState({ percentage: event.target.value })}
formatValue={val => `${val}%`}
width="35px"
accessibilityLabel={'Percent of income to tithe'}
type="number"
/>
{' of my '}
<ParameterSelect
selectedId={state.schedule}
onItemSelect={item => setState({ schedule: item })}
options={scheduleOptions}
accessibilityLabel={'Pay schedule of income'}
/>
{' income'}
</ParameterSentence>
</ParameterSentenceDemo>
```

## Mobile ParameterSelects

If this is a mobile or touch screen device include the `useNativeSelect` prop to trigger the native select picker.

```react
showSource: true
state: {
isOpen: false,
prepost: 'after',
schedule: 'annual',
percentage: 10,
income: 55700,
}
---
<ParameterSentenceDemo>
<ParameterSentence accessibilityFormLabel="Tithe Calculator">
<ParameterSelect
useNativeSelect
selectedId={state.schedule}
onItemSelect={item => setState({ schedule: item })}
options={scheduleOptions}
accessibilityLabel={'Pay schedule of income'}
/>
</ParameterSentence>
</ParameterSentenceDemo>
```

## Style variations

```react
showSource: true
state: {
isOpen: false,
prepost: 'after',
schedule: 'annual',
percentage: 10,
income: 55700,
}
---
<ParameterSentenceDemo addMargin>
<ParameterSelect
selectedId={state.schedule}
onItemSelect={item => setState({ schedule: item })}
options={scheduleOptions}
accessibilityLabel={'Pay schedule of income'}
theme={{ underlineColor: 'plum' }}
styleOverrides={{ fontSize: '18px' }}
/>
<ParameterInputBox
defaultValue="10"
value={state.percentage}
onChange={event => setState({ percentage: event.target.value })}
formatValue={val => `${val}%`}
width="35px"
accessibilityLabel={'Percent of income to tithe'}
theme={{ underlineColor: 'plum' }}
styleOverrides={{ fontSize: '18px' }}
/>
</ParameterSentenceDemo>
```
3 changes: 3 additions & 0 deletions catalog/svgs/arrow-down.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 10 additions & 45 deletions components/dropdown/component.jsx
@@ -1,52 +1,17 @@
import React, { useCallback, useState, useRef, useMemo } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import { useId } from '../shared-hooks';
import { PopoverManager } from '../popover';
import { DropdownContextProvider } from './dropdown-utils';
import { DropdownCore } from './dropdown-core';

export function Dropdown({ isOpen, onToggleMenu, theme, styleOverrides, children }) {
const [focusedMenuItem, setFocusedMenuItem] = useState(null);
const dropdownToggleRef = useRef();
const menuId = useId();

const handleCloseMenu = useCallback(
() => {
if (onToggleMenu && isOpen) {
onToggleMenu();
}
},
[onToggleMenu],
);

const context = useMemo(
() => ({
isOpen,
handleCloseMenu,
menuId,
focusedMenuItem,
setFocusedMenuItem,
dropdownToggleRef,
theme,
styleOverrides,
onToggleMenu,
}),
[
isOpen,
handleCloseMenu,
menuId,
focusedMenuItem,
setFocusedMenuItem,
dropdownToggleRef,
theme,
styleOverrides,
onToggleMenu,
],
);

return (
<DropdownContextProvider value={context}>
<PopoverManager onFocusAway={handleCloseMenu}>{children}</PopoverManager>
</DropdownContextProvider>
<DropdownCore
isOpen={isOpen}
onToggleMenu={onToggleMenu}
theme={theme}
styleOverrides={styleOverrides}
>
{children}
</DropdownCore>
);
}

Expand Down

0 comments on commit 19e890a

Please sign in to comment.