Skip to content

Commit 3ab37c2

Browse files
Merge pull request #1 from Ryzoo/master
Added possibility to focus search on start.
2 parents e3591b3 + 64128ce commit 3ab37c2

File tree

6 files changed

+40
-27
lines changed

6 files changed

+40
-27
lines changed

README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,22 @@ export default App;
5656

5757
## 👀 Props
5858

59-
| Prop | Description | Type | Default |
60-
| --------------------- | --------------------------------- | ------------------ | ------- |
61-
| `labelledBy` | value for `aria-labelledby` | `string` | |
62-
| `options` | options for dropdown | `[{label, value}]` | |
63-
| `value` | pre-selected rows | `[{label, value}]` | `[]` |
64-
| `hasSelectAll` | toggle 'Select All' option | `boolean` | `true` |
65-
| `isLoading` | show spinner on select | `boolean` | `false` |
66-
| `shouldToggleOnHover` | toggle dropdown on hover option | `boolean` | `false` |
67-
| `overrideStrings` | Override default strings for i18n | `object` | |
68-
| `onChange` | onChhange callback | `function` | |
69-
| `disabled` | disable dropdown | `boolean` | `false` |
70-
| `selectAllLabel` | _select all_ label | `string` | |
71-
| `disableSearch` | hide search textbox | `boolean` | `false` |
72-
| `filterOptions` | custom filter options | `function` | |
73-
| `theme` | theme variables | `object` | |
59+
| Prop | Description | Type | Default |
60+
| --------------------- | ---------------------------------- | ------------------ | ------- |
61+
| `labelledBy` | value for `aria-labelledby` | `string` | |
62+
| `options` | options for dropdown | `[{label, value}]` | |
63+
| `value` | pre-selected rows | `[{label, value}]` | `[]` |
64+
| `focusSearchOnOpen` | focus on search input when opening | `boolean` | `true` |
65+
| `hasSelectAll` | toggle 'Select All' option | `boolean` | `true` |
66+
| `isLoading` | show spinner on select | `boolean` | `false` |
67+
| `shouldToggleOnHover` | toggle dropdown on hover option | `boolean` | `false` |
68+
| `overrideStrings` | Override default strings for i18n | `object` | |
69+
| `onChange` | onChhange callback | `function` | |
70+
| `disabled` | disable dropdown | `boolean` | `false` |
71+
| `selectAllLabel` | _select all_ label | `string` | |
72+
| `disableSearch` | hide search textbox | `boolean` | `false` |
73+
| `filterOptions` | custom filter options | `function` | |
74+
| `theme` | theme variables | `object` | |
7475

7576
## 🌐 Internationalization
7677

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-multi-select-component",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "Simple multiple selection dropdown component with checkboxes, search and select-al`",
55
"author": "harshzalavadiya",
66
"license": "MIT",
@@ -19,10 +19,10 @@
1919
"build-storybook": "build-storybook"
2020
},
2121
"dependencies": {
22-
"@emotion/core": "^10.0.22",
23-
"@emotion/styled": "^10.0.23",
22+
"@emotion/core": "^10.0.28",
23+
"@emotion/styled": "^10.0.27",
2424
"@rooks/use-outside-click": "^3.4.1",
25-
"emotion-theming": "^10.0.19"
25+
"emotion-theming": "^10.0.27"
2626
},
2727
"peerDependencies": {
2828
"react": "^16.12.0",
@@ -34,20 +34,20 @@
3434
"@storybook/addon-links": "^5.2.6",
3535
"@storybook/addons": "^5.2.6",
3636
"@storybook/react": "^5.2.6",
37-
"@types/react": "^16.9.13",
38-
"@types/react-dom": "^16.9.4",
37+
"@types/react": "^16.9.23",
38+
"@types/react-dom": "^16.9.5",
3939
"awesome-typescript-loader": "^5.2.1",
4040
"babel-core": "^6.26.3",
4141
"babel-loader": "^7.1.5",
4242
"babel-runtime": "^6.26.0",
4343
"react": "^16.4.1",
4444
"react-dom": "^16.4.1",
45-
"react-scripts-ts": "^3.1.0",
45+
"react-scripts-ts": "^4.0.8",
4646
"rollup": "^1.27.5",
4747
"rollup-plugin-babel": "^4.3.3",
4848
"rollup-plugin-commonjs": "^10.1.0",
4949
"rollup-plugin-node-resolve": "^5.2.0",
50-
"rollup-plugin-peer-deps-external": "^2.2.0",
50+
"rollup-plugin-peer-deps-external": "^2.2.2",
5151
"rollup-plugin-postcss": "^2.0.3",
5252
"rollup-plugin-typescript2": "^0.25.2",
5353
"rollup-plugin-url": "^3.0.1",

src/lib/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface IStyledProps {
2222
export interface ISelectProps {
2323
options: Option[];
2424
value: Option[];
25+
focusSearchOnOpen?: boolean;
2526
onChange?;
2627
valueRenderer?: (selected: Option[], options: Option[]) => string;
2728
ItemRenderer?: Function;

src/multi-select/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const MultiSelectBox = styled.div`
1515
`;
1616

1717
const MultiSelect = ({
18+
focusSearchOnOpen = true,
1819
hasSelectAll = true,
1920
shouldToggleOnHover = false,
2021
options,
@@ -48,6 +49,7 @@ const MultiSelect = ({
4849
onChange,
4950
disabled,
5051
disableSearch,
52+
focusSearchOnOpen,
5153
filterOptions,
5254
overrideStrings
5355
}}

src/select-panel/index.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface ISelectPanelProps {
1616
ItemRenderer?: Function;
1717
options: Option[];
1818
value: Option[];
19+
focusSearchOnOpen: boolean;
1920
selectAllLabel?: string;
2021
onChange: (selected: Option[]) => void;
2122
disabled?: boolean;
@@ -25,6 +26,11 @@ interface ISelectPanelProps {
2526
overrideStrings?: { [key: string]: string };
2627
}
2728

29+
enum FocusType {
30+
SEARCH = -1,
31+
NONE,
32+
}
33+
2834
const SelectSearchContainer = styled.div`
2935
width: 100%;
3036
border-bottom: 1px solid ${(props: any) => props.theme.border};
@@ -47,11 +53,12 @@ export const SelectPanel = (props: ISelectPanelProps) => {
4753
ItemRenderer,
4854
disabled,
4955
disableSearch,
56+
focusSearchOnOpen,
5057
hasSelectAll,
5158
overrideStrings
5259
} = props;
5360
const [searchText, setSearchText] = useState("");
54-
const [focusIndex, setFocusIndex] = useState(0);
61+
const [focusIndex, setFocusIndex] = useState(focusSearchOnOpen ? FocusType.SEARCH : FocusType.NONE);
5562

5663
const selectAllOption = {
5764
label: selectAllLabel || getString("selectAll", overrideStrings),
@@ -67,7 +74,7 @@ export const SelectPanel = (props: ISelectPanelProps) => {
6774

6875
const handleSearchChange = e => {
6976
setSearchText(e.target.value);
70-
setFocusIndex(-1);
77+
setFocusIndex(FocusType.SEARCH);
7178
};
7279

7380
const handleItemClicked = (index: number) => setFocusIndex(index);
@@ -78,7 +85,7 @@ export const SelectPanel = (props: ISelectPanelProps) => {
7885
if (e.altKey) {
7986
return;
8087
}
81-
updateFocus(-1);
88+
updateFocus(FocusType.SEARCH);
8289
break;
8390
case 40: // Down Arrow
8491
if (e.altKey) {
@@ -94,7 +101,7 @@ export const SelectPanel = (props: ISelectPanelProps) => {
94101
};
95102

96103
const handleSearchFocus = () => {
97-
setFocusIndex(-1);
104+
setFocusIndex(FocusType.SEARCH);
98105
};
99106

100107
const allAreSelected = () => options.length === value.length;
@@ -116,6 +123,7 @@ export const SelectPanel = (props: ISelectPanelProps) => {
116123
{!disableSearch && (
117124
<SelectSearchContainer>
118125
<input
126+
autoFocus={focusSearchOnOpen}
119127
placeholder={getString("search", overrideStrings)}
120128
type="text"
121129
aria-describedby={getString("search", overrideStrings)}

src/stories/default.stories.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const toStorybook = () => {
2424
<pre>{JSON.stringify(selected)}</pre>
2525
<MultiSelect
2626
options={options}
27+
focusSearchOnOpen={boolean("focusSearchOnOpen", true)}
2728
hasSelectAll={boolean("hasSelectAll", true)}
2829
isLoading={boolean("isLoading", false)}
2930
shouldToggleOnHover={boolean("shouldToggleOnHover", false)}

0 commit comments

Comments
 (0)