Skip to content

Commit

Permalink
feat(list): add the ordered variant
Browse files Browse the repository at this point in the history
Signed-off-by: Boaz Shuster <boaz.shuster.github@gmail.com>
  • Loading branch information
boaz0 committed Dec 3, 2019
1 parent b110a3f commit 77f902d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
37 changes: 35 additions & 2 deletions packages/patternfly-4/react-core/src/components/List/List.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { mount } from 'enzyme';
import { List } from './List';
import { List, ListVariant, OrderType } from './List';
import { ListItem } from './ListItem';

const ListItems = () => (
Expand All @@ -25,10 +25,43 @@ describe('list', () => {

test('inline list', () => {
const view = mount(
<List variant="inline">
<List variant={ListVariant.inline}>
<ListItems />
</List>
);
expect(view).toMatchSnapshot();
});

test('ordered list', () => {
const view = mount(
<List component={ListVariant.ordered}>
<ListItem>Apple</ListItem>
<ListItem>Banana</ListItem>
<ListItem>Orange</ListItem>
</List>
);
expect(view.find('ol').length).toBe(1);
});

test('ordered list starts with 2nd item', () => {
const view = mount(
<List component={ListVariant.ordered} start={2}>
<ListItem>Banana</ListItem>
<ListItem>Orange</ListItem>
</List>
);
console.log(view.debug())
expect(view.find('ol').prop('start')).toBe(2);
});

test('ordered list items will be numbered with uppercase letters', () => {
const view = mount(
<List component={ListVariant.ordered} type={OrderType.uppercaseLetter}>
<ListItem>Banana</ListItem>
<ListItem>Orange</ListItem>
</List>
);
console.log(view.debug())
expect(view.find('ol').prop('type')).toBe('A');
});
});
41 changes: 32 additions & 9 deletions packages/patternfly-4/react-core/src/components/List/List.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
import * as React from 'react';
import styles from '@patternfly/react-styles/css/components/List/list';
import { css, getModifier } from '@patternfly/react-styles';
import { Omit } from '../../helpers/typeUtils'

export enum OrderType {
number = '1',
lowercaseLetter = 'a',
uppercaseLetter = 'A',
lowercaseRomanNumber = 'i',
uppercaseRomanNumber = 'I',
}

export enum ListVariant {
inline = 'inline'
inline = 'inline',
ordered = 'ol',
unordered = 'ul',
}

export interface ListProps extends React.HTMLProps<HTMLUListElement> {
export interface ListProps extends Omit<React.HTMLProps<HTMLUListElement | HTMLOListElement>, 'type'> {
/** Anything that can be rendered inside of the list */
children?: React.ReactNode;
/** Additional classes added to the list. */
/** Additional classes added to the list */
className?: string;
/** Adds list variant styles */
variant?: 'inline';
variant?: ListVariant.inline;
/** Sets the way items are numbered if variant is set to ordered */
type?: OrderType;
component?: ListVariant.ordered | ListVariant.unordered;
}

export const List: React.FunctionComponent<ListProps> = ({
className = '',
children = null,
variant = null,
type = OrderType.number,
ref = null,
component = ListVariant.unordered,
...props
}) => (
<ul {...props} className={css(styles.list, variant && getModifier(styles.modifiers, variant), className)}>
{children}
</ul>
);
}) => {
return component === ListVariant.ordered ? (
<ol ref={ref as React.LegacyRef<HTMLOListElement>} type={type} {...props} className={css(styles.list, className)}>
{children}
</ol>
) : (
<ul ref={ref as React.LegacyRef<HTMLUListElement>} {...props} className={css(styles.list, variant && getModifier(styles.modifiers, variant), className)}>
{children}
</ul>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ typescript: true
propComponents: ['List', 'ListItem']
---

import { List, ListItem } from '@patternfly/react-core';
import { List, ListItem, ListVariant, OrderType } from '@patternfly/react-core';

## Examples
```js title=Basic
Expand All @@ -24,10 +24,23 @@ SimpleList = (

```js title=Inline
import React from 'react';
import { List, ListItem } from '@patternfly/react-core';
import { List, ListItem, ListVariant } from '@patternfly/react-core';

InlineList = (
<List variant="inline">
<List variant={ListVariant.inline}>
<ListItem>First</ListItem>
<ListItem>Second</ListItem>
<ListItem>Third</ListItem>
</List>
);
```

```js title=Ordered
import React from 'react';
import { List, ListItem, ListVariant, OrderType } from '@patternfly/react-core';

OrderedList = (
<List component={ListVariant.ordered} type={OrderType.number}>
<ListItem>First</ListItem>
<ListItem>Second</ListItem>
<ListItem>Third</ListItem>
Expand Down

0 comments on commit 77f902d

Please sign in to comment.