Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions example/examples/TokenFocusExample.react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, {Fragment} from 'react';

import {Typeahead} from '../../src';
import options from '../exampleData';

/* example-start */
class TokenFocusExample extends React.Component {
state = {
selected: undefined,
};

render() {
const {selected} = this.state;

return (
<Fragment>
<Typeahead
clearButton
defaultSelected={options.slice(0, 3)}
labelKey="name"
multiple
options={options}
placeholder="Choose a state..."
onTokenFocus={this._onTokenFocus}
/>
{
selected ?
<span>
{`${selected.name} has a population of
${selected.population}. Its capital is ${selected.capital}.`}
</span> : null
}
</Fragment>
);
}

_onTokenFocus = (option, focused) => {
this.setState({
selected: focused ? option : undefined,
});
}
}
/* example-start */

export default TokenFocusExample;
10 changes: 10 additions & 0 deletions example/sections/BehaviorsSection.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import InputSizeExample from '../examples/InputSizeExample.react';
import MenuAlignExample from '../examples/MenuAlignExample.react';
import PaginationExample from '../examples/PaginationExample.react';
import SelectionsExample from '../examples/SelectionsExample.react';
import TokenFocusExample from '../examples/TokenFocusExample.react';

/* eslint-disable import/no-unresolved */
import BasicBehaviorsExampleCode from '!raw-loader!../examples/BasicBehaviorsExample.react';
Expand All @@ -16,6 +17,7 @@ import InputSizeExampleCode from '!raw-loader!../examples/InputSizeExample.react
import MenuAlignExampleCode from '!raw-loader!../examples/MenuAlignExample.react';
import PaginationExampleCode from '!raw-loader!../examples/PaginationExample.react';
import SelectionsExampleCode from '!raw-loader!../examples/SelectionsExample.react';
import TokenFocusExampleCode from '!raw-loader!../examples/TokenFocusExample.react';
/* eslint-enable import/no-unresolved */

import ExampleSection from '../components/ExampleSection.react';
Expand Down Expand Up @@ -45,6 +47,14 @@ const BehaviorsSection = (props) => (
<ExampleSection code={SelectionsExampleCode}>
<SelectionsExample />
</ExampleSection>
<Title>Token focus</Title>
<Markdown>
You can react on tokens being focused, by setting the ```onTokenFocus```
handler.
</Markdown>
<ExampleSection code={TokenFocusExampleCode}>
<TokenFocusExample />
</ExampleSection>
<Title>Input Size</Title>
<Markdown>
Besides the default input size, you can specify either a `small` or
Expand Down
1 change: 1 addition & 0 deletions src/Typeahead.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Typeahead extends React.Component {
'onFocus',
'onKeyDown',
'onRemove',
'onTokenFocus',
'placeholder',
'renderToken',
'selected',
Expand Down
4 changes: 4 additions & 0 deletions src/TypeaheadInputMulti.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class TypeaheadInputMulti extends React.Component {
onRemove,
renderToken,
selected,
onTokenFocus,
...props
} = this.props;

Expand Down Expand Up @@ -128,6 +129,9 @@ TypeaheadInputMulti.defaultProps = {
disabled={props.disabled}
key={idx}
onRemove={props.onRemove}
onTokenFocus={props.onTokenFocus ?
(focused) => props.onTokenFocus(option, focused) : undefined
}
tabIndex={props.tabIndex}>
{getOptionLabel(option, props.labelKey)}
</Token>
Expand Down
2 changes: 1 addition & 1 deletion src/TypeaheadInputSingle.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import inputContainer from './containers/inputContainer';

class TypeaheadInputSingle extends React.Component {
render() {
const {className, inputRef, ...props} = this.props;
const {className, inputRef, onTokenFocus, ...props} = this.props;

return (
<input
Expand Down
2 changes: 2 additions & 0 deletions src/containers/inputContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function inputContainer(Input) {
onFocus,
onKeyDown,
onRemove,
onTokenFocus,
placeholder,
renderToken,
selected,
Expand All @@ -53,6 +54,7 @@ function inputContainer(Input) {
onClick: onFocus,
onFocus,
onKeyDown,
onTokenFocus,
placeholder: selected.length ? null : placeholder,
// Comboboxes are single-select by definition:
// https://www.w3.org/TR/wai-aria-practices-1.1/#combobox
Expand Down
9 changes: 8 additions & 1 deletion src/containers/tokenContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const tokenContainer = (Component) => {
};

render() {
const {onTokenFocus, ...props} = this.props;
return (
<RootCloseWrapper onRootClose={this._handleBlur}>
<Component
{...this.props}
{...props}
{...this.state}
onBlur={this._handleBlur}
onClick={this._handleActive}
Expand All @@ -30,6 +31,9 @@ const tokenContainer = (Component) => {
}

_handleBlur = (e) => {
if (this.state.active && this.props.onTokenFocus) {
this.props.onTokenFocus(false);
}
this.setState({active: false});
}

Expand All @@ -50,6 +54,9 @@ const tokenContainer = (Component) => {

_handleActive = (e) => {
e.stopPropagation();
if (!this.state.active && this.props.onTokenFocus) {
this.props.onTokenFocus(true);
}
this.setState({active: true});
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/containers/typeaheadContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,10 @@ function typeaheadContainer(Component) {
* Invoked when the pagination menu item is clicked. Receives an event.
*/
onPaginate: PropTypes.func,
/**
* Invoked when the focus on a token changes.
*/
onTokenFocus: PropTypes.func,
/**
* Whether or not the menu should be displayed. `undefined` allows the
* component to control visibility, while `true` and `false` show and hide
Expand Down