Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit a468d8f

Browse files
author
Alexandru Buliga
authored
feat(dropdown): inline prop (#863)
* feat(dropdown): inline prop * changelog * restored wrong variables change * addressed comments * fixed list item width on all browsers using @layershifter suggestion * fix propTypes for DropdownSearchInput * remove unused props param * fixed list item styles and removed redundancy
1 parent 404b0e3 commit a468d8f

File tree

8 files changed

+116
-28
lines changed

8 files changed

+116
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2525
- Add multiple selection flavor for `Dropdown` component @Bugaa92 ([#845](https://github.com/stardust-ui/react/pull/845))
2626
- Add `black` and `white` options for the `color` prop of the `Label` component @mnajdova ([#855](https://github.com/stardust-ui/react/pull/855))
2727
- Add `Flex` component @kuzhelov ([#802](https://github.com/stardust-ui/react/pull/802))
28+
- Add `inline` prop for `Dropdown` component @Bugaa92 ([#863](https://github.com/stardust-ui/react/pull/863))
2829

2930
### Fixes
3031
- Focus the last focused element which triggered `Popup` on ESC @sophieH29 ([#861](https://github.com/stardust-ui/react/pull/861))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from 'react'
2+
import { Dropdown, Header } from '@stardust-ui/react'
3+
4+
const inputItems = [
5+
'Bruce Wayne',
6+
'Natasha Romanoff',
7+
'Steven Strange',
8+
'Alfred Pennyworth',
9+
`Scarlett O'Hara`,
10+
'Imperator Furiosa',
11+
'Bruce Banner',
12+
'Peter Parker',
13+
'Selina Kyle',
14+
]
15+
16+
const DropdownExampleInline = () => (
17+
<>
18+
<Header as="h3">Inline:</Header>
19+
<div>
20+
Some text inline with the{' '}
21+
<Dropdown inline items={inputItems} placeholder="Select your hero" /> and more text.
22+
</div>
23+
<Header as="h3">Inline Search:</Header>
24+
<span>
25+
Some other text inline with the{' '}
26+
<Dropdown
27+
inline
28+
search
29+
items={inputItems}
30+
noResultsMessage="We couldn't find any matches."
31+
placeholder="Start typing a name"
32+
/>{' '}
33+
and more text.
34+
</span>
35+
</>
36+
)
37+
38+
export default DropdownExampleInline

docs/src/examples/components/Dropdown/Types/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const Types = () => (
1919
description="A dropdown can be searchable."
2020
examplePath="components/Dropdown/Types/DropdownExampleSearch"
2121
/>
22+
<ComponentExample
23+
title="Inline"
24+
description="A dropdown can be used inline with text."
25+
examplePath="components/Dropdown/Types/DropdownExampleInline"
26+
/>
2227
</ExampleSection>
2328
)
2429

packages/react/src/components/Dropdown/Dropdown.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export interface DropdownProps extends UIComponentProps<DropdownProps, DropdownS
7676
*/
7777
getA11yStatusMessage?: (options: DownshiftA11yStatusMessageOptions<ShorthandValue>) => string
7878

79+
/** A dropdown can be formatted to appear inline in the content of other components. */
80+
inline?: boolean
81+
7982
/** Array of props for generating list options (Dropdown.Item[]) and selected item labels(Dropdown.SelectedItem[]), if it's a multiple selection. */
8083
items?: ShorthandValue[]
8184

@@ -190,6 +193,7 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo
190193
fluid: PropTypes.bool,
191194
getA11ySelectionMessage: PropTypes.object,
192195
getA11yStatusMessage: PropTypes.func,
196+
inline: PropTypes.bool,
193197
items: customPropTypes.collectionShorthand,
194198
itemToString: PropTypes.func,
195199
loading: PropTypes.bool,
@@ -333,11 +337,12 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo
333337
styles: ComponentSlotStylesInput,
334338
getToggleButtonProps: (options?: GetToggleButtonPropsOptions) => any,
335339
): JSX.Element {
340+
const { triggerButton } = this.props
336341
const content = this.getSelectedItemAsString(this.state.value)
337342

338343
return (
339344
<Ref innerRef={this.buttonRef}>
340-
{Button.create(this.props.triggerButton, {
345+
{Button.create(triggerButton, {
341346
defaultProps: {
342347
className: Dropdown.slotClassNames.triggerButton,
343348
content,
@@ -369,7 +374,7 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo
369374
) => void,
370375
variables,
371376
): JSX.Element {
372-
const { searchInput, multiple, placeholder } = this.props
377+
const { inline, searchInput, multiple, placeholder } = this.props
373378
const { searchQuery, value } = this.state
374379

375380
const noPlaceholder =
@@ -378,6 +383,7 @@ class Dropdown extends AutoControlledComponent<Extendable<DropdownProps>, Dropdo
378383
return DropdownSearchInput.create(searchInput || {}, {
379384
defaultProps: {
380385
placeholder: noPlaceholder ? '' : placeholder,
386+
inline,
381387
variables,
382388
inputRef: this.inputRef,
383389
},

packages/react/src/components/Dropdown/DropdownSearchInput.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { UIComponentProps } from '../../lib/commonPropInterfaces'
1414
import Input from '../Input/Input'
1515

1616
export interface DropdownSearchInputProps extends UIComponentProps<DropdownSearchInputProps> {
17+
/** A dropdown search input can be formatted to appear inline in the context of a Dropdown. */
18+
inline?: boolean
19+
1720
/** Ref for input DOM node. */
1821
inputRef?: React.Ref<HTMLElement>
1922

@@ -70,7 +73,7 @@ class DropdownSearchInput extends UIComponent<ReactProps<DropdownSearchInputProp
7073
}),
7174
accessibilityInputProps: PropTypes.object,
7275
accessibilityComboboxProps: PropTypes.object,
73-
hasToggleButton: PropTypes.bool,
76+
inline: PropTypes.bool,
7477
inputRef: customPropTypes.ref,
7578
onFocus: PropTypes.func,
7679
onInputBlur: PropTypes.func,

packages/react/src/themes/teams/components/Dropdown/dropdownItemStyles.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
22
import { DropdownVariables } from './dropdownVariables'
33
import { DropdownItemProps } from '../../../../components/Dropdown/DropdownItem'
4-
import ListItem from '../../../../components/List/ListItem'
54

65
const dropdownItemStyles: ComponentSlotStylesInput<DropdownItemProps, DropdownVariables> = {
7-
root: ({ variables: v, props: { active } }): ICSSInJSStyle => ({
8-
[`&.${ListItem.className}`]: { backgroundColor: v.listItemBackgroundColor },
9-
10-
...(active && {
11-
[`&.${ListItem.className}`]: {
12-
backgroundColor: v.listItemBackgroundColorActive,
13-
color: v.listItemColorActive,
14-
},
6+
root: ({ props: p, variables: v }): ICSSInJSStyle => ({
7+
whiteSpace: 'nowrap',
8+
backgroundColor: v.listItemBackgroundColor,
9+
...(p.active && {
10+
color: v.listItemColorActive,
11+
backgroundColor: v.listItemBackgroundColorActive,
1512
}),
1613
}),
1714
}

packages/react/src/themes/teams/components/Dropdown/dropdownSearchInputStyles.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ const dropdownSearchInputStyles: ComponentSlotStylesInput<
1111
flexGrow: 1,
1212
}),
1313

14-
input: ({ variables: v }): ICSSInJSStyle => ({
14+
input: ({ props: p }): ICSSInJSStyle => ({
1515
width: '100%',
16-
backgroundColor: v.backgroundColor,
16+
backgroundColor: 'transparent',
1717
borderWidth: 0,
18+
...(p.inline && {
19+
paddingLeft: 0,
20+
paddingRight: 0,
21+
}),
1822
}),
1923
}
2024

packages/react/src/themes/teams/components/Dropdown/dropdownStyles.ts

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,58 @@ import { DropdownProps, DropdownState } from '../../../../components/Dropdown/Dr
33
import { DropdownVariables } from './dropdownVariables'
44
import { pxToRem } from '../../../../lib'
55

6-
const dropdownStyles: ComponentSlotStylesInput<DropdownProps & DropdownState, DropdownVariables> = {
7-
root: (): ICSSInJSStyle => ({}),
6+
type DropdownPropsAndState = DropdownProps & DropdownState
7+
8+
const transparentColorStyle: ICSSInJSStyle = {
9+
backgroundColor: 'transparent',
10+
borderColor: 'transparent',
11+
borderBottomColor: 'transparent',
12+
}
13+
14+
const transparentColorStyleObj: ICSSInJSStyle = {
15+
...transparentColorStyle,
16+
':hover': transparentColorStyle,
17+
':active': transparentColorStyle,
18+
':focus': {
19+
...transparentColorStyle,
20+
':active': transparentColorStyle,
21+
},
22+
}
23+
24+
const getWidth = (p: DropdownPropsAndState, v: DropdownVariables): string => {
25+
if (p.fluid) {
26+
return '100%'
27+
}
28+
29+
if (p.inline) {
30+
return 'initial'
31+
}
32+
33+
return v.width
34+
}
35+
36+
const dropdownStyles: ComponentSlotStylesInput<DropdownPropsAndState, DropdownVariables> = {
37+
root: ({ props: p }): ICSSInJSStyle => ({
38+
...(p.inline && {
39+
display: 'inline-flex',
40+
}),
41+
}),
842

943
container: ({ props: p, variables: v }): ICSSInJSStyle => ({
1044
display: 'flex',
1145
flexWrap: 'wrap',
12-
outline: 0,
13-
backgroundColor: v.backgroundColor,
46+
position: 'relative',
1447
boxSizing: 'border-box',
1548
borderStyle: 'solid',
1649
borderColor: 'transparent',
50+
outline: 0,
51+
width: getWidth(p, v),
1752
borderWidth: v.borderWidth,
1853
borderRadius: v.borderRadius,
1954
color: v.color,
20-
width: p.fluid ? '100%' : v.width,
21-
position: 'relative',
55+
backgroundColor: v.backgroundColor,
2256
...(p.focused && { borderBottomColor: v.borderColorFocus }),
57+
...(p.inline && transparentColorStyleObj),
2358
}),
2459

2560
selectedItems: ({ props: p, variables: v }): ICSSInJSStyle => ({
@@ -32,19 +67,14 @@ const dropdownStyles: ComponentSlotStylesInput<DropdownProps & DropdownState, Dr
3267
}),
3368

3469
triggerButton: ({ props: p, variables: v }): ICSSInJSStyle => {
35-
const transparentColorStyle = {
36-
backgroundColor: 'transparent',
37-
borderColor: 'transparent',
38-
}
3970
return {
4071
boxShadow: 'none',
4172
margin: '0',
4273
justifyContent: 'left',
4374
padding: v.comboboxPaddingButton,
4475
height: pxToRem(30),
45-
...transparentColorStyle,
4676
...(p.multiple && { minWidth: 0, flex: 1 }),
47-
':hover': transparentColorStyle,
77+
...transparentColorStyleObj,
4878
':focus': {
4979
...transparentColorStyle,
5080
':after': {
@@ -54,7 +84,11 @@ const dropdownStyles: ComponentSlotStylesInput<DropdownProps & DropdownState, Dr
5484
},
5585
':active': transparentColorStyle,
5686
},
57-
':active': transparentColorStyle,
87+
...(p.inline && {
88+
paddingLeft: 0,
89+
paddingRight: 0,
90+
width: 'initial',
91+
}),
5892
}
5993
},
6094

@@ -65,7 +99,7 @@ const dropdownStyles: ComponentSlotStylesInput<DropdownProps & DropdownState, Dr
6599
zIndex: 1000,
66100
maxHeight: v.listMaxHeight,
67101
overflowY: 'auto',
68-
width: p.fluid ? '100%' : v.width,
102+
width: getWidth(p, v),
69103
top: 'calc(100% + 2px)', // leave room for container + its border
70104
background: v.listBackgroundColor,
71105
...(p.isOpen && {

0 commit comments

Comments
 (0)