0.1.1 • Published 8 years ago

frh-react-phone-lookup v0.1.1

Weekly downloads
1
License
BSD
Repository
github
Last release
8 years ago

FRH React Phone Lookup

React component for looking up phone book entries by name or number. The user can select a number to call either by

  • typing part of the person's name or number, and selecting the callee from the drop-down list; or
  • entering a valid phone number (for numbers that are not in the list).

Demo

Try the demo here.

Installation

npm install --save frh-react-phone-lookup

Usage

import React       from 'react'
import ReactDOM    from 'react-dom'
import PhoneLookup from 'frh-react-phone-lookup'

class App extends React.Component {
  constructor(props) {
    super(props)
  }
  render() {
    return (
      <PhoneLookup entries ={[
        {
          name  : 'Danae Rothstein',
          phone : '+1-202-555-0125'
        },
        {
          name  : 'Leslee Bunnell',
          phone : '+1-202-555-0145'
        },
        {
          name  : 'Voncile Reams',
          phone : '202-555-0121'
        }
      ]}/>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('main')
)

Props

PropertyTypeDescriptionDefault
maxResultsNumberThe maximum number of items visible in the list of results.10
entriesArrayAn array of phone book entries. Each object in this array must have a name, and a phone property.[]
resultsComponentComponentThe component responsible for rendering the list of results.See 'Customization'
inputComponentComponentThe input field component.See 'Customization'
regexpRegExpThe regular expression used to determine when the user input is a valid phone number./^(\+?[0-9]{1,3}\-?|0)[0123456789]{9}$/
onCallNumberFunctionA callback that runs when the user clicks the 'Call' button.number => { console.log(number) }

Required props

Technically, all props are optional, but you should at least provide your own entries array and onCallNumber hook for any useful implementation.

Format of the entries array

[
  {
    name  : string
    phone : string 
  }
]

Customization

To change the appearance and behavior of the results drop-down or input field, you can provide your own implementation of these components as follows:

  <PhoneLookup 
    resultsComponent = {MyResultsComponent}
    inputComponent   = {MyInputComponent}
    entries          = {[ ... ]} />

See below for an explanation of the various props that are passed to these components.

Results

Props

PropertyTypeDescription
onSelectionChangeFunctionShould be called when the user selects a result from the list with the selected entry as the parameter.
resultsArrayThe array of phone book entries that should appear in the list.

Default implementation

class DefaultResults extends React.Component {
  constructor(props) {
    super(props)
  }
  render() {
    const { results, onSelectionChange } = this.props
    return (
      <ul style={ulStyles}>
        {results.map((result, key) => {
          return (
            <li key={key}>
              <span style={{float: 'right'}}>
                {result.phone}
              </span>
              <a href='#' onClick={() => onSelectionChange(result)}>
                {result.name} 
              </a>
            </li>
          )
        })}
      </ul>
    )
  }
}

Input

Props

PropertyTypeDescription
hasEntryBooleanWhether an entry is currently selected or not.
isValidNumberBooleanWhether the entered value is a valid phone number or not.
valueStringThe current value.
onResetFunctionCallback to reset the value.
onCallNumberFunctionCallback to run when the user clicks the 'Call' button.
onChangeFunctionChange handler that receives either the value, or an event object.

Default implementation

class DefaultInput extends React.Component {
  constructor(props) {
    super(props)
  }
  render() {
    const { hasEntry, value, onChange, onReset, onCallNumber, isValidNumber } = this.props
    const inputStyle = hasEntry ? {
      backgroundColor: '#fff4a8'
    } : isValidNumber ? {
      backgroundColor: '#a8f4a8'
    } : {}
    return (
      <div>
        <input 
          type     = 'text'
          style    = {inputStyle}
          value    = {value}
          onChange = {onChange}
        />
        {(hasEntry || isValidNumber) && (
          <span>
            <button onClick={onReset}>Reset</button>
            <button onClick={onCallNumber}>Call</button>
          </span>
        )}
      </div>
    )
  }
}

Example

See the source code for the demo for an example of customization, using the Bootstrap framework.

Contribute

License

BSD