2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-10-31 23:22:22 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import React, { Component, PropTypes } from 'react';
|
2017-05-16 12:25:47 +02:00
|
|
|
import keycode from 'keycode';
|
2016-10-31 23:22:22 +01:00
|
|
|
import { uniq } from 'lodash';
|
2017-05-16 12:25:47 +02:00
|
|
|
import { Input as SemanticInput } from 'semantic-ui-react';
|
2016-10-31 23:22:22 +01:00
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
import { parseI18NString } from '@parity/shared/util/messages';
|
|
|
|
import { arrayOrObjectProptype, nodeOrStringProptype } from '@parity/shared/util/proptypes';
|
2016-12-27 11:02:53 +01:00
|
|
|
|
2017-05-18 11:48:53 +02:00
|
|
|
import LabelWrapper from '~/ui/Form/LabelWrapper';
|
2017-05-16 12:25:47 +02:00
|
|
|
|
|
|
|
import Chip from './Chip';
|
2016-10-31 23:22:22 +01:00
|
|
|
|
|
|
|
export default class InputChip extends Component {
|
2017-05-16 12:25:47 +02:00
|
|
|
static contextTypes = {
|
|
|
|
intl: PropTypes.object
|
|
|
|
};
|
|
|
|
|
2016-10-31 23:22:22 +01:00
|
|
|
static propTypes = {
|
2017-05-18 11:48:53 +02:00
|
|
|
autoFocus: PropTypes.bool,
|
2016-12-27 11:02:53 +01:00
|
|
|
addOnBlur: PropTypes.bool,
|
|
|
|
clearOnBlur: PropTypes.bool,
|
2016-10-31 23:22:22 +01:00
|
|
|
className: PropTypes.string,
|
2016-12-27 11:02:53 +01:00
|
|
|
hint: nodeOrStringProptype(),
|
|
|
|
label: nodeOrStringProptype(),
|
2016-10-31 23:22:22 +01:00
|
|
|
onTokensChange: PropTypes.func,
|
|
|
|
onBlur: PropTypes.func,
|
2017-05-16 12:25:47 +02:00
|
|
|
tokens: arrayOrObjectProptype().isRequired
|
|
|
|
};
|
2016-10-31 23:22:22 +01:00
|
|
|
|
|
|
|
static defaultProps = {
|
2016-11-01 13:51:02 +01:00
|
|
|
clearOnBlur: false,
|
|
|
|
addOnBlur: false
|
2017-05-16 12:25:47 +02:00
|
|
|
};
|
2016-11-01 13:51:02 +01:00
|
|
|
|
|
|
|
state = {
|
2017-05-16 12:25:47 +02:00
|
|
|
textValue: ''
|
|
|
|
};
|
2016-10-31 23:22:22 +01:00
|
|
|
|
2017-05-18 11:48:53 +02:00
|
|
|
// TODO: autoFocus to be implemented (same as with Form/Input)
|
2016-10-31 23:22:22 +01:00
|
|
|
render () {
|
2017-05-16 12:25:47 +02:00
|
|
|
const { className, hint, label, tokens } = this.props;
|
|
|
|
const { textValue } = this.state;
|
2016-11-01 13:51:02 +01:00
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
return (
|
2017-05-18 11:48:53 +02:00
|
|
|
<LabelWrapper
|
2017-05-16 12:25:47 +02:00
|
|
|
className={ className }
|
|
|
|
label={ label }
|
|
|
|
>
|
|
|
|
<SemanticInput
|
|
|
|
fluid
|
|
|
|
onBlur={ this.onBlur }
|
|
|
|
onChange={ this.onChange }
|
|
|
|
onKeyDown={ this.onKeyDown }
|
|
|
|
placeholder={ parseI18NString(this.context, hint) }
|
|
|
|
ref='chipInput'
|
|
|
|
value={ textValue }
|
|
|
|
>
|
|
|
|
<input />
|
|
|
|
</SemanticInput>
|
|
|
|
<div>
|
|
|
|
{ tokens.map(this.renderChip) }
|
|
|
|
</div>
|
2017-05-18 11:48:53 +02:00
|
|
|
</LabelWrapper>
|
2017-05-16 12:25:47 +02:00
|
|
|
);
|
|
|
|
}
|
2016-11-01 13:51:02 +01:00
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
renderChip = (chip) => {
|
|
|
|
const onDelete = (event) => this.handleTokenDelete(chip);
|
2016-11-01 13:51:02 +01:00
|
|
|
|
2016-10-31 23:22:22 +01:00
|
|
|
return (
|
2017-05-16 12:25:47 +02:00
|
|
|
<Chip
|
|
|
|
key={ chip }
|
|
|
|
label={ chip }
|
|
|
|
onDelete={ onDelete }
|
2016-11-01 13:51:02 +01:00
|
|
|
/>
|
2016-10-31 23:22:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTokenAdd = (value) => {
|
2017-05-16 12:25:47 +02:00
|
|
|
const { tokens } = this.props;
|
2016-10-31 23:22:22 +01:00
|
|
|
const newTokens = uniq([].concat(tokens, value));
|
|
|
|
|
|
|
|
this.handleTokensChange(newTokens);
|
2017-05-16 12:25:47 +02:00
|
|
|
this.setState({ textValue: '' });
|
2016-10-31 23:22:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTokenDelete = (value) => {
|
|
|
|
const { tokens } = this.props;
|
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
this.handleTokensChange(
|
|
|
|
uniq(
|
|
|
|
[]
|
|
|
|
.concat(tokens)
|
|
|
|
.filter((token) => token !== value)
|
|
|
|
)
|
|
|
|
);
|
2017-01-06 17:23:04 +01:00
|
|
|
|
2016-10-31 23:22:22 +01:00
|
|
|
this.refs.chipInput.focus();
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
handleTokensChange = (tokens) => {
|
|
|
|
const { onTokensChange } = this.props;
|
2016-10-31 23:22:22 +01:00
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
onTokensChange(tokens.filter((token) => token && token.length > 0));
|
|
|
|
}
|
2016-10-31 23:22:22 +01:00
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
onBlur = () => {
|
|
|
|
const { onBlur, addOnBlur } = this.props;
|
2016-10-31 23:22:22 +01:00
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
if (addOnBlur) {
|
|
|
|
const { textValue } = this.state;
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
this.handleTokenAdd(textValue);
|
2016-10-31 23:22:22 +01:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
onBlur && onBlur();
|
2016-10-31 23:22:22 +01:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
onChange = (event, data) => {
|
|
|
|
this.setState({ textValue: data.value.trim() });
|
|
|
|
}
|
2016-10-31 23:22:22 +01:00
|
|
|
|
2017-05-16 12:25:47 +02:00
|
|
|
onKeyDown = (event, data) => {
|
|
|
|
const { textValue } = this.state;
|
|
|
|
|
|
|
|
switch (keycode(event)) {
|
|
|
|
case 'enter':
|
|
|
|
case 'space':
|
|
|
|
this.handleTokenAdd(textValue);
|
|
|
|
break;
|
|
|
|
}
|
2016-10-31 23:22:22 +01:00
|
|
|
}
|
|
|
|
}
|