React TokenInput
Live Demo: https://seawind543.github.io/react-token-input/
React TokenInput (react-customize-token-input)
A react token (tag) controlled
input component, which support:
- Accept customize data structure.
- Customize token (tag) Look & Feel on the
label
Demo, delete button
Demo, or even override the whole Token component
Demo. - Customize separate characters for separate end-user input string. Demo
- Inline editing on exist token.
- Paste values. Demo
- Preprocessing function for normalized user input value.
It could be helpful to reproduce a single value into multiple values too. Demo
- Validate function.
Installation
- Install the latest version of react and react-customize-token-input:
yarn add react react-customize-token-input
- At this point you can import
react-token-input
and its styles in your application by:
import TokenInput from 'react-customize-token-input';
import 'react-customize-token-input/dist/react-customize-token-input.css';
Dev
- Run
yarn install
to install required packages. - Run
yarn dev
to launch webpack-dev-server
. - After step 2, you will see following message output in command console.
「wds」: Project is running at http://0.0.0.0:8000/
「wds」: webpack output is served from /
「wds」: Content not from webpack is served from ../docs
Note: To stop the program, just type ctrl + c
in command console.
- After step 3 complete, you could access
http://localhost:8000/
to see result.
Usage
See Live Examples: https://seawind543.github.io/react-token-input/
Note: Sources code of Examples in the folder examples
Props
style: PropTypes.object,
className: PropTypes.string,
readOnly: PropTypes.bool,
autoFocus: PropTypes.bool,
placeholder: PropTypes.string,
separators: PropTypes.array,
tokenValues: PropTypes.array.isRequired,
onTokenValuesChange: PropTypes.func,
onPreprocess: PropTypes.func,
onBuildTokenValue: PropTypes.func,
onInputValueChange: PropTypes.func,
onTokenValueValidate: PropTypes.func,
onGetTokenClassName: PropTypes.func,
onGetTokenDisplayLabel: PropTypes.func,
onRenderTokenDeleteButtonContent: PropTypes.func,
onGetTokenEditableValue: PropTypes.func,
onGetTokenErrorMessage: PropTypes.func,
customizeTokenComponent: PropTypes.func,
Beta props
specialKeyDown: PropTypes.object,
Predefined KeyDown Event Handlers
TokenInput have following Predefined event handlers on each Special KeyDown.
For Token Create
KeyDown | Description | Note |
---|
Backspace | In case the value is an empty string , the token in the list tail will be deleted. | |
Escape | Clear the input value. | A.K.A. Reset. |
Enter | Create the token with the inputValue and continually focused on the inputBox for next inputting. | |
Tab | Same as onEnter. | - Default not apply
- Under Beta
|
For Inline editing
KeyDown | Description | Note |
---|
Escape | End editing without change the value of the token. | A.K.A. Reset |
Enter | End editing and apply the new value. In case the new value is an empty string , will perform the onEscape . | |
Default value of optional Props
className: '',
readOnly: false,
autoFocus: false,
placeholder: '',
separators: [
',',
';',
'\n',
'\r',
'\r\n',
];
specialKeyDown: {
onBackspace: 1,
onTab: 0,
onEnter: 1,
onEscape: 1,
},
onBuildTokenValue: buildDefaultTokenValue,
onInputValueChange: () => {},
onTokenValueValidate: () => {},
onTokenValuesChange: () => {},
onGetTokenClassName: () => {},
onGetTokenDisplayLabel: getDefaultTokenEditableValue,
onGetTokenEditableValue: getDefaultTokenEditableValue,
onGetTokenErrorMessage: getDefaultTokenErrorMessage,
Props of customizeTokenComponent
Could also reference this Example Demo and its source code ExampleCustomizeToken
from the folder examples
.
readOnly: PropTypes.bool.isRequired,
tokenValue: PropTypes.any.isRequired,
tokenMeta: PropTypes.object.isRequired,
onGetClassName: PropTypes.func.isRequired,
onGetDisplayLabel: PropTypes.func.isRequired,
onRenderDeleteButtonContent: PropTypes.func,
onGetEditableValue: PropTypes.func.isRequired,
onGetErrorMessage: PropTypes.func.isRequired,
onBuildTokenValue: PropTypes.func.isRequired,
onEditStart: PropTypes.func.isRequired,
onEditEnd: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
License
MIT