项目作者: JFusco

项目描述 :
WAI-ARIA compliant React autocomplete component
高级语言: JavaScript
项目地址: git://github.com/JFusco/react-predictive-input.git
创建时间: 2016-07-28T00:57:59Z
项目社区:https://github.com/JFusco/react-predictive-input

开源协议:MIT License

下载


react-predictive-input

Build Status

peerDependency Status
devDependency Status

MIT
npm

WAI-ARIA compliant React autocomplete component

Demo

https://jfusco.github.io/react-predictive-input

Getting Started

Installation

From the root of your project.

  1. npm install react-predictive-input --save

Usage

Implementation of autocomplete. See available options below.

  1. import React, { Component } from 'react';
  2. import { render } from 'react-dom';
  3. import Autocomplete from 'react-predictive-input';
  4. class Application extends Component{
  5. static defaultProps = {
  6. fruit: ['bananas', 'strawberries', 'blueberries', 'pineapples', 'apples', 'tomatos', 'mangos', 'oranges', 'grapes', 'Rasberries', 'Blackberries', 'starfruit']
  7. };
  8. static propTypes = {
  9. fruit: PropTypes.arrayOf(PropTypes.string)
  10. };
  11. constructor(props){
  12. super(props);
  13. }
  14. onItemSelected(value){
  15. console.log(`${value} was selected`);
  16. }
  17. render(){
  18. return (
  19. <div>
  20. <Autocomplete
  21. id="fruit"
  22. placeholder="Search a type of fruit"
  23. data={this.props.fruit}
  24. onSelected={this.onItemSelected.bind(this)} ></Autocomplete>
  25. </div>
  26. );
  27. }
  28. }
  29. render(<Application ></Application>, document.getElementById('application'));

Options

id ~ required

The unique id of the component - used for setting up accessibility

  1. <Autocomplete id="fruit" ></Autocomplete>

placeholder ~ optional ~ default null

A string used as placeholder text in the tags input field

  1. <Autocomplete placeholder="Search a type of fruit" ></Autocomplete>

data ~ optional ~ default []

An array of strings to be used as suggestions

  1. <Autocomplete data={['apples', 'bananas']} ></Autocomplete>

value ~ optional ~ default ''

A string to set the value of the input field

  1. <Autocomplete value="apples" ></Autocomplete>

fuzzy ~ optional ~ default true

A boolean that enables fuzzy search

  1. <Autocomplete fuzzy={true} ></Autocomplete>

clearValueOnSelect ~ optional

A boolean that allows the item to be cleared out of the input field upon selection

  1. <Autocomplete clearValueOnSelect={true} ></Autocomplete>

caseSensitive ~ optional ~ default false

An boolean that allows for case sensitive search

  1. <Auocomplete caseSensitive={false} ></Auocomplete>

onChange ~ optional

A method fired when user changes the input value

  1. onInputChange(value) {
  2. console.log(`${value} is the value`);
  3. }
  4. <Auocomplete onChange={this.onInputChange.bind(this)} ></Auocomplete>

onSelected ~ optional

A method fired when user selects one of the suggested values

  1. onItemSelected(value) {
  2. console.log(`${value} is the selected item`);
  3. }
  4. <Auocomplete onSelected={this.onItemSelected.bind(this)} ></Auocomplete>

Styling

Installation

Import the main SCSS file in to your application SCSS files

  1. @import "node_modules/react-predictive-input/src/component/scss/styles.scss";

There are a few variables set to !default that can be overriden. If you need to change it more just override the actual styles.

Any overriden variables needs to go above the @import statement to take effect

  1. //-- Global UI
  2. $ac-base-width
  3. $ac-base-border-radius
  4. $ac-base-font-family
  5. //-- Input field
  6. $ac-input-height
  7. $ac-input-width
  8. $ac-input-font-size
  9. $ac-input-border
  10. $ac-input-font-color
  11. $ac-input-background-color
  12. $ac-input-border-radius
  13. $ac-input-padding
  14. $ac-input-placeholder-color
  15. $ac-input-border-focus-color
  16. $ac-input-font-family
  17. $ac-input-typeahead-font-color
  18. //-- Suggestion list
  19. $ac-slist-border-radius
  20. $ac-slist-background-color
  21. //-- Suggestion
  22. $ac-s-mark-font-color
  23. $ac-s-mark-background
  24. $ac-s-mark-font-weight
  25. $ac-s-active-background-color
  26. $ac-s-active-font-color
  27. $ac-s-font-color
  28. $ac-s-font-size
  29. $ac-s-background-color
  30. $ac-s-font-family
  31. $ac-s-border
  32. $ac-s-padding

If you don’t care to override variables and just want to override actual styles you may choose to import the minified compiled version of the css instead

  1. @import "node_modules/react-predictive-input/dist/styles.css";

Tests

  1. npm test