// @flow import * as React from 'react'; import { FormattedMessage, injectIntl } from 'react-intl'; import Button from '../../components/button/Button'; import TextInput from '../../components/text-input/TextInput'; import Tooltip from '../../components/tooltip/Tooltip'; import commonMessages from '../../common/messages'; import IconInfo from '../../icons/general/IconInfo'; import messages from './messages'; import './CustomInstanceNewField.scss'; type Props = { intl: any, isCancellable: boolean, onAdd: (key: string, value: MetadataFieldValue) => void, onCancel: () => void, properties: MetadataFields, }; type State = { error: React.Node, key: string, value: string, }; class CustomInstanceNewField extends React.PureComponent { state: State = { key: '', value: '', error: '', }; /** * Common change handler * * @param {Event} event - keyboard event * @param {string} attr - key or value * @return {void} */ onChange(event: SyntheticKeyboardEvent, attr: string) { const currentTarget = (event.currentTarget: HTMLInputElement); this.setState({ error: '', [attr]: currentTarget.value, }); } /** * Change handler for the key * * @param {Event} event - keyboard event * @return {void} */ onKeyChange = (event: SyntheticKeyboardEvent) => { this.onChange(event, 'key'); }; /** * Change handler for the value * * @param {Event} event - keyboard event * @return {void} */ onValueChange = (event: SyntheticKeyboardEvent) => { this.onChange(event, 'value'); }; /** * Persists the new metadata added or shows an error * * @return {void} */ onAdd = () => { const { key, value }: State = this.state; const { onAdd, properties }: Props = this.props; if (Object.prototype.hasOwnProperty.call(properties, key)) { this.setState({ error: , }); } else if (key.startsWith('$')) { this.setState({ error: , }); } else if (key) { onAdd(key, value); } else { this.setState({ error: , }); } }; render() { const { intl, isCancellable, onCancel }: Props = this.props; const { key, value, error }: State = this.state; return (
}>
} onChange={this.onKeyChange} placeholder={intl.formatMessage(messages.customKeyPlaceholder)} type="text" value={key} /> } onChange={this.onValueChange} placeholder={intl.formatMessage(messages.customValuePlaceholder)} type="text" value={value} />
{isCancellable && ( )}
); } } export { CustomInstanceNewField as CustomInstanceNewFieldBase }; export default injectIntl(CustomInstanceNewField);