├── jestEnvironment.js ├── src ├── styles │ ├── index.js │ └── PowerSelect.scss ├── index.js ├── RenderOption.js ├── Dropdown.js ├── SelectTrigger.js ├── AutoResizeInput.js ├── PowerSelect │ ├── BeforeOptionsWrapper.js │ ├── index.js │ └── __tests__ │ │ └── PowerSelect-test.js ├── __tests__ │ ├── test-utils │ │ ├── constants.js │ │ └── create-page-object.js │ └── utils │ │ └── matcher-test.js ├── Option.js ├── TriggerWrapper.js ├── PowerSelectMultiple │ ├── SelectedOption.js │ ├── SelectTrigger.js │ ├── index.js │ └── __tests__ │ │ └── PowerSelectMultiple-test.js ├── TypeAhead │ ├── index.js │ ├── SelectTrigger.js │ └── __tests__ │ │ └── TypeAhead-test.js ├── DropdownMenu.js ├── Options.js ├── utils.js └── Select.js ├── stories ├── index.stories.js ├── utils │ ├── Centered.js │ └── constants.js ├── Recipes.stories.js ├── Recipes │ ├── HighlightSearch.js │ └── TaggedInput.js ├── PowerSelectTypeAhead.stories.js ├── PowerSelectMultiple.stories.js ├── index.scss └── PowerSelect.stories.js ├── .babelrc ├── .storybook ├── preview-head.html ├── addons.js ├── config.js └── webpack.config.js ├── .travis.yml ├── .gitignore ├── .editorconfig ├── loaders └── snippet-loader.js ├── testSetup.js ├── LICENSE ├── webpack.config.js ├── README.md └── package.json /jestEnvironment.js: -------------------------------------------------------------------------------- 1 | require("babel-polyfill"); 2 | -------------------------------------------------------------------------------- /src/styles/index.js: -------------------------------------------------------------------------------- 1 | import './PowerSelect.scss'; 2 | -------------------------------------------------------------------------------- /stories/index.stories.js: -------------------------------------------------------------------------------- 1 | import 'src/styles'; 2 | import './index.scss'; 3 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015", 4 | "react", 5 | "stage-0" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /.storybook/preview-head.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "14" 5 | - "16" 6 | script: 7 | - npm test 8 | after_success: 'npm run coveralls' 9 | -------------------------------------------------------------------------------- /.storybook/addons.js: -------------------------------------------------------------------------------- 1 | import '@storybook/addon-knobs/register'; 2 | import '@storybook/addon-actions/register'; 3 | import '@storybook/addon-options/register'; 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # output 2 | lib/ 3 | dist/ 4 | coverage/ 5 | 6 | # dependencies 7 | /node_modules 8 | /bower_components 9 | 10 | # misc 11 | npm-debug.log 12 | .DS_Store 13 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import PowerSelect from './PowerSelect'; 2 | import TypeAhead from './TypeAhead'; 3 | import TypeAheadSelectTrigger from './TypeAhead/SelectTrigger'; 4 | import PowerSelectMultiple from './PowerSelectMultiple'; 5 | 6 | TypeAhead.Trigger = TypeAheadSelectTrigger; 7 | 8 | export { PowerSelect, TypeAhead, PowerSelectMultiple }; 9 | -------------------------------------------------------------------------------- /stories/utils/Centered.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export default story => ( 4 |
12 |
{story()}
13 |
14 | ); 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | indent_style = space 14 | indent_size = 2 15 | 16 | [*.hbs] 17 | insert_final_newline = false 18 | 19 | [*.{diff,md}] 20 | trim_trailing_whitespace = false 21 | -------------------------------------------------------------------------------- /loaders/snippet-loader.js: -------------------------------------------------------------------------------- 1 | function capitalize(string) { 2 | return string.charAt(0).toUpperCase() + string.slice(1); 3 | } 4 | 5 | module.exports = function(source) { 6 | var path = this.resourcePath; 7 | var splits = path.split('/'); 8 | var namedParts = splits.slice(splits.length - 2); 9 | var snippetName = 10 | capitalize(namedParts[0]) + capitalize(namedParts[1].replace('.js', '')); 11 | return `global['${snippetName}'] = ${JSON.stringify(source)}`; 12 | }; 13 | -------------------------------------------------------------------------------- /.storybook/config.js: -------------------------------------------------------------------------------- 1 | import { configure } from '@storybook/react'; 2 | import { setOptions } from '@storybook/addon-options'; 3 | 4 | setOptions({ 5 | name: 'React Power Select', 6 | url: 'https://github.com/selvagsz/react-power-select', 7 | addonPanelInRight: true, 8 | enableShortcuts: true, 9 | }); 10 | 11 | // automatically import all files ending in *.stories.js 12 | const req = require.context('../stories', true, /.stories.js$/); 13 | function loadStories() { 14 | req.keys().forEach((filename) => req(filename)); 15 | } 16 | 17 | configure(loadStories, module); 18 | -------------------------------------------------------------------------------- /stories/Recipes.stories.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { storiesOf } from '@storybook/react'; 3 | import { withKnobs } from '@storybook/addon-knobs/react'; 4 | import Centered from './utils/Centered'; 5 | 6 | import HighlightSearch from './Recipes/HighlightSearch'; 7 | import TaggedInput from './Recipes/TaggedInput'; 8 | 9 | const stories = storiesOf('More Recipes', module); 10 | stories.addDecorator(withKnobs); 11 | stories.addDecorator(Centered); 12 | 13 | stories.add('highlight search', () => ); 14 | stories.add('tagged input', () => ); 15 | -------------------------------------------------------------------------------- /testSetup.js: -------------------------------------------------------------------------------- 1 | import 'core-js/es6/map'; 2 | import 'core-js/es6/set'; 3 | import 'raf/polyfill'; 4 | import Enzyme from 'enzyme'; 5 | import Adapter from 'enzyme-adapter-react-16'; 6 | import $ from 'jquery'; 7 | 8 | $.prototype.exists = function() { 9 | return !!this.length; 10 | }; 11 | 12 | $.prototype.childAt = function(index) { 13 | return this.children().eq(index); 14 | }; 15 | 16 | $.prototype.simulate = function(event, params) { 17 | this.trigger(event, params); 18 | return this; 19 | }; 20 | 21 | $.prototype.at = function(index) { 22 | return this.eq(index); 23 | }; 24 | 25 | Enzyme.configure({ adapter: new Adapter() }); 26 | -------------------------------------------------------------------------------- /src/RenderOption.js: -------------------------------------------------------------------------------- 1 | import React, { Component, isValidElement, cloneElement } from 'react'; 2 | 3 | export default function RenderOption({ option, select, optionLabelPath, optionComponent }) { 4 | let publicProps = { option, select, optionLabelPath }; 5 | let OptionComponent = optionComponent; 6 | if (isValidElement(OptionComponent)) { 7 | return cloneElement(OptionComponent, publicProps); 8 | } 9 | if (OptionComponent) { 10 | return ; 11 | } 12 | if (typeof option === 'object') { 13 | if (optionLabelPath) { 14 | return {option[optionLabelPath]}; 15 | } 16 | } 17 | if (typeof option === 'string') { 18 | return {option}; 19 | } 20 | return null; 21 | } 22 | -------------------------------------------------------------------------------- /src/Dropdown.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import TetherComponent from 'react-tether'; 3 | import cx from 'classnames'; 4 | 5 | export default class Dropdown extends Component { 6 | render() { 7 | let { className, horizontalPosition, children } = this.props; 8 | return ( 9 | 20 | {children} 21 | 22 | ); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/SelectTrigger.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import RenderOption from './RenderOption'; 3 | import TriggerWrapper from './TriggerWrapper'; 4 | 5 | export default function Trigger({ 6 | selectedOption, 7 | optionLabelPath, 8 | selectedOptionComponent, 9 | placeholder, 10 | select, 11 | ...rest 12 | }) { 13 | return ( 14 | 15 |
16 | {selectedOption ? ( 17 | 23 | ) : ( 24 | {placeholder} 25 | )} 26 |
27 |
28 | ); 29 | } 30 | -------------------------------------------------------------------------------- /src/AutoResizeInput.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | 3 | export default class AutoResizeInput extends Component { 4 | state = {}; 5 | 6 | componentWillMount() { 7 | this.setInputSize(this.props.value); 8 | } 9 | 10 | componentWillReceiveProps(nextProps) { 11 | this.setInputSize(nextProps.value); 12 | } 13 | 14 | setInputSize(value) { 15 | this.setState({ 16 | length: value.length + 4, 17 | }); 18 | } 19 | 20 | setInputRef(input) { 21 | this.input = input; 22 | 23 | // Focus input if autoFocus passed 24 | if (this.props.autoFocus && this.input) { 25 | this.input.focus(); 26 | } 27 | } 28 | 29 | render() { 30 | let { autoFocus, ...rest } = this.props; 31 | return this.setInputRef(input)} size={this.state.length} {...rest} />; 32 | } 33 | } 34 | 35 | AutoResizeInput.defaultProps = { 36 | onChange: () => {}, 37 | value: '', 38 | }; 39 | -------------------------------------------------------------------------------- /src/PowerSelect/BeforeOptionsWrapper.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import AutoResizeInput from '../AutoResizeInput'; 3 | 4 | export default function BeforeOptionsWrapper({ 5 | searchEnabled, 6 | onChange, 7 | beforeOptionsComponent, 8 | searchPlaceholder, 9 | searchInputAutoFocus, 10 | ...otherProps 11 | }) { 12 | let BeforeOptionsComponent = beforeOptionsComponent; 13 | return ( 14 |
15 | {searchEnabled && ( 16 |
17 | 24 |
25 | )} 26 | {beforeOptionsComponent && } 27 |
28 | ); 29 | } 30 | -------------------------------------------------------------------------------- /src/__tests__/test-utils/constants.js: -------------------------------------------------------------------------------- 1 | export const KEY_CODES = { 2 | UP_ARROW: 38, 3 | DOWN_ARROW: 40, 4 | ESCAPE: 27, 5 | ENTER: 13, 6 | TAB: 9, 7 | }; 8 | 9 | export const frameworks = ['React', 'Ember', 'Angular', 'Vue', 'Inferno']; 10 | export const countries = [ 11 | { 12 | name: 'Argentina', 13 | code: 'ARG', 14 | flag: 'https://flagcdn.com/ar.svg', 15 | continent: 'South America', 16 | }, 17 | { 18 | name: 'Brazil', 19 | code: 'BRA', 20 | flag: 'https://flagcdn.com/br.svg', 21 | continent: 'South America', 22 | }, 23 | { 24 | name: 'Canada', 25 | code: 'CAN', 26 | flag: 'https://flagcdn.com/ca.svg', 27 | continent: 'North America', 28 | }, 29 | { 30 | name: 'China', 31 | code: 'CHN', 32 | flag: 'https://flagcdn.com/cn.svg', 33 | continent: 'Asia', 34 | }, 35 | { 36 | name: 'India', 37 | code: 'IND', 38 | flag: 'https://flagcdn.com/in.svg', 39 | continent: 'Asia', 40 | }, 41 | ]; 42 | -------------------------------------------------------------------------------- /src/Option.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import cx from 'classnames'; 3 | import RenderOption from './RenderOption'; 4 | 5 | export default class Option extends Component { 6 | render() { 7 | let { 8 | option, 9 | select, 10 | disabled, 11 | optionIndex, 12 | optionLabelPath, 13 | optionComponent, 14 | isHighlighted, 15 | onOptionClick, 16 | } = this.props; 17 | let isDisabled = disabled || option.disabled; 18 | return ( 19 |
27 | 33 |
34 | ); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/TriggerWrapper.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import cx from 'classnames'; 3 | import { renderComponent } from './utils'; 4 | 5 | export default function TriggerWrapper({ 6 | value, 7 | select, 8 | showClear, 9 | onClearClick, 10 | triggerLHSComponent, 11 | triggerRHSComponent, 12 | children, 13 | }) { 14 | return ( 15 |
20 | {triggerLHSComponent && ( 21 |
22 | {renderComponent(triggerLHSComponent, { select })} 23 |
24 | )} 25 | 26 | {children} 27 | 28 | {showClear && } 29 | 30 | {triggerRHSComponent && ( 31 |
32 | {renderComponent(triggerRHSComponent, { select })} 33 |
34 | )} 35 |
36 | ); 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Selva Ganesh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/PowerSelect/index.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import Select from '../Select'; 3 | import BeforeOptionsWrapper from './BeforeOptionsWrapper'; 4 | 5 | export default class PowerSelect extends Component { 6 | handleSearchInputChange = event => { 7 | // hackish 8 | this.select.handleSearchInputChange(event); 9 | }; 10 | 11 | render() { 12 | let { 13 | searchEnabled, 14 | searchPlaceholder, 15 | beforeOptionsComponent, 16 | searchInputAutoFocus, 17 | ...rest 18 | } = this.props; 19 | return ( 20 | (this.select = select)} 30 | triggerComponent={props => { 31 | return ; 32 | }} 33 | selectedOptionLabelPath={selectedOptionLabelPath} 34 | {...rest} 35 | onKeyDown={this.handleKeyDown} 36 | /> 37 | ); 38 | } 39 | } 40 | 41 | TypeAhead.displayName = 'TypeAhead'; 42 | TypeAhead.defaultProps = { 43 | triggerComponent: SelectTrigger, 44 | }; 45 | -------------------------------------------------------------------------------- /.storybook/webpack.config.js: -------------------------------------------------------------------------------- 1 | // you can use this file to add your custom webpack plugins, loaders and anything you like. 2 | // This is just the basic way to add additional webpack configurations. 3 | // For more information refer the docs: https://storybook.js.org/configurations/custom-webpack-config 4 | 5 | // IMPORTANT 6 | // When you add this file, we won't add the default configurations which is similar 7 | // to "React Create App". This only has babel loader to load JavaScript. 8 | 9 | const path = require('path'); 10 | 11 | module.exports = { 12 | context: process.cwd(), 13 | output: { 14 | path: path.resolve(process.cwd(), 'docs'), 15 | }, 16 | resolve: { 17 | extensions: ['.js', '.jsx', '.scss'], 18 | modules: [ 19 | path.resolve(process.cwd(), '.'), 20 | path.resolve(process.cwd(), 'node_modules'), 21 | path.resolve(process.cwd(), 'node_modules', 'velocity-react', 'node_modules'), 22 | ], 23 | }, 24 | 25 | plugins: [ 26 | // your custom plugins 27 | ], 28 | 29 | module: { 30 | rules: [ 31 | // add your custom rules. 32 | { 33 | test: /\.(css|scss)$/, 34 | use: [ 35 | { 36 | loader: 'style-loader', 37 | }, 38 | { 39 | loader: 'css-loader', 40 | options: { 41 | minimize: true, 42 | }, 43 | }, 44 | { 45 | loader: 'sass-loader', 46 | }, 47 | ], 48 | }, 49 | ], 50 | }, 51 | }; 52 | -------------------------------------------------------------------------------- /stories/PowerSelectTypeAhead.stories.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { useState } from 'react'; 3 | import { storiesOf } from '@storybook/react'; 4 | import { withKnobs } from '@storybook/addon-knobs/react'; 5 | import { text, array, boolean, object } from '@storybook/addon-knobs/react'; 6 | import { action } from '@storybook/addon-actions'; 7 | import { TypeAhead } from 'src'; 8 | import Centered from './utils/Centered'; 9 | import { countries } from './utils/constants'; 10 | 11 | const TypeAheadWithHooks = () => { 12 | const [selected, setCountry] = useState(countries[8]); 13 | return ( 14 | { 18 | action('onChange')(args); 19 | setCountry(args.option); 20 | }} 21 | optionLabelPath={text('optionLabelPath', 'name')} 22 | placeholder={text('placeholder', 'Select your favorite country')} 23 | disabled={boolean('disabled', false)} 24 | showClear={boolean('showClear', true)} 25 | searchPlaceholder={text('searchPlaceholder', 'Search...')} 26 | searchIndices={array('searchIndices', ['name', 'code'])} 27 | optionComponent={({ option }) => ( 28 |
29 | 30 | {option.name} ({option.code}) 31 |
32 | )} 33 | /> 34 | ); 35 | }; 36 | 37 | const stories = storiesOf('Power Select TypeAhead', module); 38 | stories.addDecorator(withKnobs); 39 | stories.addDecorator(Centered); 40 | 41 | stories.add('with configs & custom option component', () => ); 42 | -------------------------------------------------------------------------------- /src/DropdownMenu.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import cx from 'classnames'; 3 | import { renderComponent } from './utils'; 4 | import Options from './Options'; 5 | 6 | export default class DropdownMenu extends Component { 7 | componentWillMount() { 8 | this.validateAndClose(this.props.options); 9 | } 10 | 11 | componentWillReceiveProps({ options }) { 12 | this.validateAndClose(options); 13 | } 14 | 15 | validateAndClose(options) { 16 | let { beforeOptionsComponent, afterOptionsComponent, select } = this.props; 17 | if (!beforeOptionsComponent && !afterOptionsComponent && !options.length) { 18 | select.actions.close(); 19 | } 20 | } 21 | 22 | render() { 23 | let { 24 | className, 25 | select, 26 | handleKeyDown, 27 | highlightedOption, 28 | minWidth, 29 | onRef, 30 | beforeOptionsComponent, 31 | afterOptionsComponent, 32 | ...otherProps 33 | } = this.props; 34 | return ( 35 |
{ 39 | handleKeyDown(event, highlightedOption); 40 | }} 41 | style={{ minWidth }} 42 | ref={dropdownMenu => this.props.onRef(dropdownMenu)} 43 | > 44 | {beforeOptionsComponent && renderComponent(beforeOptionsComponent, { select })} 45 | 46 | 47 | 48 | {afterOptionsComponent && renderComponent(afterOptionsComponent, { select })} 49 |
50 | ); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const webpack = require('webpack'); 3 | const ExtractTextPlugin = require('extract-text-webpack-plugin'); 4 | 5 | const SRC_DIR = `${__dirname}/src`; 6 | const DIST_DIR = `${__dirname}/dist`; 7 | 8 | module.exports = { 9 | context: SRC_DIR, 10 | resolve: { 11 | extensions: ['.js', '.scss'], 12 | }, 13 | 14 | entry: { 15 | 'react-power-select': './index.js', 16 | css: './styles/index.js', 17 | }, 18 | 19 | output: { 20 | path: `${DIST_DIR}/`, 21 | filename: '[name].js', 22 | }, 23 | 24 | module: { 25 | rules: [ 26 | { 27 | test: /\.js$/, 28 | exclude: /node_modules/, 29 | use: [ 30 | { 31 | loader: 'babel-loader', 32 | options: { 33 | cacheDirectory: true, 34 | presets: ['es2015', 'react', 'stage-0'], 35 | }, 36 | }, 37 | ], 38 | }, 39 | { 40 | test: /\.scss$/, 41 | use: ExtractTextPlugin.extract({ 42 | fallback: 'style-loader', 43 | use: [ 44 | { 45 | loader: 'css-loader', 46 | options: { 47 | minimize: true, 48 | }, 49 | }, 50 | { 51 | loader: 'sass-loader', 52 | }, 53 | ], 54 | }), 55 | }, 56 | ], 57 | }, 58 | 59 | plugins: [ 60 | new webpack.ProvidePlugin({ 61 | React: 'react', 62 | }), 63 | 64 | new webpack.optimize.UglifyJsPlugin({ 65 | compress: { 66 | warnings: false, 67 | }, 68 | output: { 69 | comments: false, 70 | }, 71 | }), 72 | 73 | new ExtractTextPlugin({ 74 | filename: 'react-power-select.css', 75 | }), 76 | ], 77 | }; 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### :warning: NOT ACTIVELY WORKING ON THE PROJECT. Looking for Maintainers 2 | 3 | # react-power-select 4 | A highly composable & reusable select components 5 | 6 | [![npm version](https://badge.fury.io/js/react-power-select.svg)](https://www.npmjs.com/package/react-power-select) 7 | [![Build Status](https://travis-ci.com/selvagsz/react-power-select.svg?branch=master)](https://travis-ci.com/selvagsz/react-power-select) 8 | [![Coverage Status](https://coveralls.io/repos/github/selvagsz/react-power-select/badge.svg?branch=master)](https://coveralls.io/github/selvagsz/react-power-select) 9 | [![Storybook](https://github.com/storybooks/press/blob/master/badges/storybook.svg)](https://selvagsz.github.io/react-power-select) 10 | 11 | 12 | ### DEMO https://selvagsz.github.io/react-power-select/ 13 | 14 | # Installation 15 | 16 | ```bash 17 | npm i react-power-select --save 18 | ``` 19 | 20 | Import the CSS in your bundle 21 | 22 | ```js 23 | import 'react-power-select/dist/react-power-select.css' 24 | ``` 25 | 26 | # Usage 27 | 28 | ```js 29 | import React, { Component } from 'react' 30 | import { PowerSelect } from 'react-power-select' 31 | 32 | export default class Demo extends Component { 33 | state = {}; 34 | 35 | handleChange = ({ option }) => { 36 | this.setState({ 37 | selectedOption: option 38 | }) 39 | } 40 | 41 | render() { 42 | return ( 43 | 48 | ) 49 | } 50 | } 51 | ``` 52 | 53 | 54 | ## Credits 55 | 56 | Hat tip to [ember-power-select's](https://github.com/cibernox/ember-power-select) [architecture](http://www.ember-power-select.com/EPS_disected-48ad0d36e579a5554bff1407b51c554b.png) 57 | -------------------------------------------------------------------------------- /stories/utils/constants.js: -------------------------------------------------------------------------------- 1 | export const countries = [ 2 | { 3 | name: 'Argentina', 4 | code: 'ARG', 5 | flag: 'https://flagcdn.com/ar.svg', 6 | continent: 'South America', 7 | }, 8 | { 9 | name: 'Brazil', 10 | code: 'BRA', 11 | flag: 'https://flagcdn.com/br.svg', 12 | continent: 'South America', 13 | }, 14 | { 15 | name: 'Canada', 16 | code: 'CAN', 17 | flag: 'https://flagcdn.com/ca.svg', 18 | continent: 'North America', 19 | }, 20 | { 21 | name: 'China', 22 | code: 'CHN', 23 | flag: 'https://flagcdn.com/cn.svg', 24 | continent: 'Asia', 25 | }, 26 | { 27 | name: 'India', 28 | code: 'IND', 29 | flag: 'https://flagcdn.com/in.svg', 30 | continent: 'Asia', 31 | }, 32 | { 33 | name: 'Japan', 34 | code: 'JPN', 35 | flag: 'https://flagcdn.com/jp.svg', 36 | continent: 'Asia', 37 | }, 38 | { 39 | name: 'Portugal', 40 | code: 'PRT', 41 | flag: 'https://flagcdn.com/pr.svg', 42 | continent: 'Europe', 43 | }, 44 | { 45 | name: 'Russian Federation', 46 | code: 'RUS', 47 | flag: 'https://flagcdn.com/ru.svg', 48 | continent: 'Asia', 49 | }, 50 | { 51 | name: 'Spain', 52 | code: 'ESP', 53 | flag: 'https://flagcdn.com/es.svg', 54 | continent: 'Europe', 55 | }, 56 | { 57 | name: 'United Kingdom', 58 | code: 'GBR', 59 | flag: 'https://flagcdn.com/gb.svg', 60 | continent: 'Europe', 61 | }, 62 | { 63 | name: 'United States of America', 64 | code: 'USA', 65 | flag: 'https://flagcdn.com/us.svg', 66 | continent: 'North America', 67 | }, 68 | ]; 69 | 70 | export const frameworks = [ 71 | 'React', 72 | 'Ember', 73 | 'Angular', 74 | 'Vue', 75 | 'Preact', 76 | 'Meteor', 77 | 'Backbone', 78 | 'Knockout', 79 | 'SproutCore', 80 | 'Spine', 81 | ]; 82 | -------------------------------------------------------------------------------- /stories/PowerSelectMultiple.stories.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { useState } from 'react'; 3 | import { storiesOf } from '@storybook/react'; 4 | import { withKnobs } from '@storybook/addon-knobs/react'; 5 | import { text, array, boolean, object } from '@storybook/addon-knobs/react'; 6 | import { action } from '@storybook/addon-actions'; 7 | import { countries } from './utils/constants'; 8 | import PowerSelectMultiple from 'src/PowerSelectMultiple'; 9 | import Centered from './utils/Centered'; 10 | 11 | const PowerSelectMultipleWithHooks = props => { 12 | const [selected, setSelectedCountries] = useState([countries[8], countries[4]]); 13 | return ( 14 | { 18 | action('onChange')(args); 19 | setSelectedCountries(args.options); 20 | }} 21 | optionLabelPath={text('optionLabelPath', 'name')} 22 | placeholder={text('placeholder', 'Select your favorite countries')} 23 | disabled={boolean('disabled', false)} 24 | showClear={boolean('showClear', true)} 25 | searchIndices={array('searchIndices', ['name', 'code'])} 26 | optionComponent={({ option }) => ( 27 |
28 | 29 | {option.name} ({option.code}) 30 |
31 | )} 32 | selectedOptionComponent={({ option, select }) => ( 33 | 34 | 35 | {option.name} 36 | 37 | )} 38 | /> 39 | ); 40 | }; 41 | 42 | const stories = storiesOf('Power Select Multiple', module); 43 | stories.addDecorator(withKnobs); 44 | stories.addDecorator(Centered); 45 | 46 | stories.add('with configs & custom option/selected component', () => ( 47 | 48 | )); 49 | -------------------------------------------------------------------------------- /stories/index.scss: -------------------------------------------------------------------------------- 1 | @mixin placeholder() { 2 | color: #ccc; 3 | font-weight: lighter; 4 | font-size: 14px; 5 | } 6 | 7 | *, 8 | *::before, 9 | *::after { 10 | box-sizing: border-box; 11 | margin: 0; 12 | } 13 | 14 | html { 15 | height: 100%; 16 | } 17 | 18 | body { 19 | font-family: 'Lato', sans-serif; 20 | padding-bottom: 100px; 21 | } 22 | 23 | html, body { 24 | background-color: rgba(249, 249, 249, 0.44); 25 | } 26 | 27 | ::-webkit-input-placeholder { 28 | @include placeholder() 29 | } 30 | ::-moz-placeholder { 31 | @include placeholder() 32 | } 33 | :-ms-input-placeholder { 34 | @include placeholder() 35 | } 36 | :-moz-placeholder { 37 | @include placeholder() 38 | } 39 | 40 | .flag { 41 | width: 20px; 42 | height: 13px; 43 | margin-right: 8px; 44 | } 45 | 46 | .quick-create { 47 | padding: 8px 12px; 48 | border-top: 1px solid #e5e5e5; 49 | cursor: pointer; 50 | display: block; 51 | color: #3260b7; 52 | background-color: #e5ebf6; 53 | 54 | &:hover { 55 | color: #0039a6; 56 | } 57 | } 58 | 59 | .options-header { 60 | padding: 5px 10px; 61 | text-transform: uppercase; 62 | font-size: 13px; 63 | letter-spacing: 5px; 64 | font-weight: bold; 65 | border-bottom: 1px dashed rgba(89, 96, 206, 0.5); 66 | margin-bottom: 10px; 67 | background-color: rgba(89, 96, 206, 0.25); 68 | border-top: 1px dashed rgba(89, 96, 206, 0.5); 69 | } 70 | 71 | .code { 72 | float: right; 73 | font-size: 11px; 74 | color: #666; 75 | letter-spacing: 2px; 76 | margin-top: 4px; 77 | } 78 | 79 | .highlight { 80 | background-color: yellow; 81 | font-weight: bold; 82 | } 83 | 84 | .help { 85 | color: #aaa; 86 | font-size: 13px; 87 | margin-top: 8px; 88 | } 89 | 90 | textarea#options { 91 | max-height: 200px; 92 | } 93 | 94 | .TaggedInput { 95 | // Hides the caret 96 | .PowerSelect__TriggerStatus { 97 | display: none; 98 | } 99 | 100 | .PowerSelect__Clear { 101 | padding-right: 8px; 102 | } 103 | 104 | // Removes the padding allocated for caret 105 | .PowerSelectMultiple__SelectedOptions { 106 | padding-right: 0; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /stories/Recipes/TaggedInput.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { PowerSelectMultiple } from 'src'; 3 | 4 | // Hide the caret via CSS 5 | 6 | // .TaggedInput { 7 | // // Hides the caret 8 | // .PowerSelect__TriggerStatus { 9 | // display: none; 10 | // } 11 | 12 | // // Removes the padding allocated for caret 13 | // .PowerSelectMultiple__SelectedOptions { 14 | // padding-right: 0; 15 | // } 16 | // } 17 | 18 | // TaggedInput Component - composed on top of PowerSelectMultiple 19 | class TaggedInput extends Component { 20 | handleChange = ({ options, select }) => { 21 | this.props.onChange({ 22 | items: options, 23 | select, 24 | }); 25 | }; 26 | 27 | handleSearchInputChange = (value, select) => { 28 | if (value.length > 1 && value.charAt(value.length - 1) === ',') { 29 | let items = this.props.items.slice(); 30 | items.push(value.slice(0, -1)); 31 | 32 | this.props.onChange({ 33 | items, 34 | select, 35 | }); 36 | 37 | select.actions.search(''); 38 | select.actions.focus(); 39 | } 40 | }; 41 | 42 | render() { 43 | let { items, onChange, onSearchInputChange, ...rest } = this.props; 44 | return ( 45 | { 51 | this.handleSearchInputChange(event.target.value, select); 52 | if (onSearchInputChange) { 53 | onSearchInputChange((event, { select })); 54 | } 55 | }} 56 | {...rest} 57 | /> 58 | ); 59 | } 60 | } 61 | 62 | TaggedInput.defaultProps = { 63 | items: [], 64 | }; 65 | 66 | export default class TaggedInputDemo extends Component { 67 | state = { 68 | urls: [], 69 | }; 70 | 71 | handleChange = ({ items }) => { 72 | this.setState({ urls: items }); 73 | }; 74 | 75 | render() { 76 | return ( 77 | 82 | ); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/TypeAhead/SelectTrigger.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import TriggerWrapper from '../TriggerWrapper'; 3 | import AutoResizeInput from '../AutoResizeInput'; 4 | 5 | export default class SelectTrigger extends Component { 6 | state = {}; 7 | 8 | componentWillMount() { 9 | let value = this.getValueFromSelectedOption(this.props); 10 | this.setState({ value }); 11 | } 12 | 13 | componentWillReceiveProps(nextProps) { 14 | let value = 15 | nextProps.searchTerm !== null 16 | ? nextProps.searchTerm 17 | : this.getValueFromSelectedOption(nextProps); 18 | this.setState({ 19 | value, 20 | }); 21 | } 22 | 23 | getValueFromSelectedOption(props = this.props) { 24 | let { selectedOption, selectedOptionLabelPath, optionLabelPath } = props; 25 | let value = ''; 26 | selectedOptionLabelPath = selectedOptionLabelPath || optionLabelPath; 27 | if (selectedOption) { 28 | if (typeof selectedOption === 'string') { 29 | value = selectedOption; 30 | } else if (selectedOptionLabelPath) { 31 | value = selectedOption[selectedOptionLabelPath]; 32 | } 33 | } 34 | return value; 35 | } 36 | 37 | handleInputChange = event => { 38 | this.setState({ 39 | value: event.target.value, 40 | }); 41 | this.props.handleOnChange(event); 42 | }; 43 | 44 | render() { 45 | let { 46 | select, 47 | placeholder, 48 | disabled, 49 | autoFocus, 50 | handleOnChange, 51 | handleKeyDown, 52 | handleOnFocus, 53 | handleOnBlur, 54 | ...rest 55 | } = this.props; 56 | let value = this.state.value; 57 | 58 | return ( 59 | 60 |
61 | 74 |
75 |
76 | ); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/PowerSelectMultiple/SelectTrigger.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import AutoResizeInput from '../AutoResizeInput'; 3 | import SelectedOption from './SelectedOption'; 4 | import TriggerWrapper from '../TriggerWrapper'; 5 | 6 | export default class SelectTrigger extends Component { 7 | state = { 8 | value: '', 9 | }; 10 | 11 | componentWillReceiveProps(nextProps) { 12 | let value = nextProps.searchTerm !== null ? nextProps.searchTerm : ''; 13 | this.setState({ 14 | value, 15 | }); 16 | } 17 | 18 | handleClearClick = event => { 19 | this.props.onClearClick(event, { select: this.props.select }); 20 | }; 21 | 22 | render() { 23 | let { 24 | selectedOption, 25 | optionLabelPath, 26 | showOptionClose, 27 | select, 28 | placeholder, 29 | disabled, 30 | autoFocus, 31 | handleOnChange, 32 | handleKeyDown, 33 | handleOnFocus, 34 | handleOnBlur, 35 | selectedOptionComponent, 36 | ...rest 37 | } = this.props; 38 | let selected = selectedOption || []; 39 | return ( 40 | 46 |
47 |
    48 | {selected.map((selectedOption, index) => { 49 | return ( 50 | 59 | ); 60 | })} 61 |
  • 62 | 75 |
  • 76 |
77 |
78 |
79 | ); 80 | } 81 | } 82 | 83 | SelectTrigger.defaultProps = { 84 | onOptionCloseClick: () => {}, 85 | }; 86 | -------------------------------------------------------------------------------- /stories/PowerSelect.stories.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { useState } from 'react'; 3 | import { storiesOf } from '@storybook/react'; 4 | import { withKnobs } from '@storybook/addon-knobs/react'; 5 | import { text, array, boolean, object } from '@storybook/addon-knobs/react'; 6 | import { action } from '@storybook/addon-actions'; 7 | import { countries } from './utils/constants'; 8 | import PowerSelect from 'src/PowerSelect'; 9 | import Centered from './utils/Centered'; 10 | 11 | const powerSelect = storiesOf('Power Select', module); 12 | powerSelect.addDecorator(withKnobs); 13 | powerSelect.addDecorator(Centered); 14 | 15 | const PowerSelectWithHooks = props => { 16 | const [selected, setCountry] = useState(countries[8]); 17 | return ( 18 | { 22 | action('onChange')(args); 23 | setCountry(args.option); 24 | }} 25 | optionLabelPath={text('optionLabelPath', 'name')} 26 | placeholder={text('placeholder', 'Select your favorite country')} 27 | disabled={boolean('disabled', false)} 28 | showClear={boolean('showClear', true)} 29 | searchEnabled={boolean('searchEnabled', true)} 30 | searchInputAutoFocus={boolean('searchInputAutoFocus', true)} 31 | searchPlaceholder={text('searchPlaceholder', 'Search...')} 32 | searchIndices={array('searchIndices', ['name', 'code'])} 33 | {...props} 34 | /> 35 | ); 36 | }; 37 | 38 | powerSelect.add('with general configs', () => ); 39 | 40 | powerSelect.add('customOptionComponent', () => ( 41 | { 43 | return ( 44 |
45 | 46 | {option.name}  47 | {option.code} 48 |
49 | ); 50 | }} 51 | /> 52 | )); 53 | 54 | powerSelect.add('selectedOptionComponent', () => ( 55 | { 57 | return ( 58 |
59 | 60 | {option.name}  61 | ({option.code}) 62 |
63 | ); 64 | }} 65 | /> 66 | )); 67 | 68 | powerSelect.add('beforeOptionsComponent', () => ( 69 |
Before Option Component
} 71 | /> 72 | )); 73 | 74 | powerSelect.add('afterOptionsComponent', () => ( 75 | ( 77 | { 80 | alert('Lalalala'); 81 | select.actions.close(); 82 | }} 83 | > 84 | + Add New 85 | 86 | )} 87 | /> 88 | )); 89 | -------------------------------------------------------------------------------- /src/Options.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import cx from 'classnames'; 3 | import Option from './Option'; 4 | import { getOptionIndex, isOptGroup } from './utils'; 5 | 6 | export default class Options extends Component { 7 | componentWillReceiveProps({ options, highlightedOption }) { 8 | this.scrollTo({ options, highlightedOption }); 9 | } 10 | 11 | componentDidMount() { 12 | let { options, highlightedOption } = this.props; 13 | this.optionsListOffsetHeight = this.optionsList.offsetHeight; 14 | this.scrollTo({ options, highlightedOption }); 15 | } 16 | 17 | componentDidUpdate() { 18 | if (!this.optionsListOffsetHeight) { 19 | this.optionsListOffsetHeight = this.optionsList.offsetHeight; 20 | } 21 | } 22 | 23 | scrollTo({ options, highlightedOption }) { 24 | if (highlightedOption) { 25 | let optionIndex = getOptionIndex(options, highlightedOption); 26 | let $option = this.optionsList.querySelector(`[data-option-index="${optionIndex}"]`); 27 | let delta = 0; 28 | if ($option) { 29 | let $optionOffsetHeight = $option.offsetHeight; 30 | let $optionOffsetTop = $option.offsetTop; 31 | delta = $optionOffsetTop + $optionOffsetHeight - this.optionsListOffsetHeight; 32 | } 33 | if (delta > 0) { 34 | this.optionsList.scrollTop = delta; 35 | } else { 36 | this.optionsList.scrollTop = 0; 37 | } 38 | } 39 | } 40 | 41 | renderOptions(options, optGroupDisabled = false) { 42 | let { select, optionLabelPath, optionComponent, highlightedOption, onOptionClick } = this.props; 43 | return options.map((option, index) => { 44 | let optionIndex = getOptionIndex(this.props.options, option); 45 | if (isOptGroup(option)) { 46 | return ( 47 |
54 |
{option.label}
55 | {this.renderOptions(option.options, option.disabled)} 56 |
57 | ); 58 | } 59 | return ( 60 |