2019-10-19 16:38:07 +02:00
|
|
|
import React from 'react';
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
import { AddButton } from '../buttons';
|
|
|
|
|
import InputField from './InputField';
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
addEntry: (p: string) => void;
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
buttonLabel: string;
|
|
|
|
|
fieldLabel: string;
|
|
|
|
|
errorMessage: string;
|
|
|
|
|
helpText?: string;
|
|
|
|
|
validateEntry?: (p: string) => boolean;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2019-10-19 16:38:07 +02:00
|
|
|
entryToAdd: string;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AddEntryToTableField extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
2019-10-19 16:38:07 +02:00
|
|
|
entryToAdd: '',
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-07 06:01:13 +01:00
|
|
|
isValid = () => {
|
2019-10-19 16:38:07 +02:00
|
|
|
const { validateEntry } = this.props;
|
|
|
|
|
if (
|
|
|
|
|
!this.state.entryToAdd ||
|
|
|
|
|
this.state.entryToAdd === '' ||
|
|
|
|
|
!validateEntry
|
|
|
|
|
) {
|
2019-02-07 06:01:13 +01:00
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return validateEntry(this.state.entryToAdd);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
render() {
|
2018-10-04 10:16:20 +02:00
|
|
|
const {
|
|
|
|
|
disabled,
|
|
|
|
|
buttonLabel,
|
|
|
|
|
fieldLabel,
|
|
|
|
|
errorMessage,
|
2019-10-19 16:38:07 +02:00
|
|
|
helpText,
|
2018-10-04 10:16:20 +02:00
|
|
|
} = this.props;
|
2018-09-03 16:17:36 +02:00
|
|
|
return (
|
|
|
|
|
<div className="field">
|
|
|
|
|
<InputField
|
|
|
|
|
label={fieldLabel}
|
|
|
|
|
errorMessage={errorMessage}
|
|
|
|
|
onChange={this.handleAddEntryChange}
|
2019-02-07 06:01:13 +01:00
|
|
|
validationError={!this.isValid()}
|
2018-09-03 16:17:36 +02:00
|
|
|
value={this.state.entryToAdd}
|
|
|
|
|
onReturnPressed={this.appendEntry}
|
|
|
|
|
disabled={disabled}
|
2018-10-02 10:59:44 +02:00
|
|
|
helpText={helpText}
|
2018-09-03 16:17:36 +02:00
|
|
|
/>
|
|
|
|
|
<AddButton
|
|
|
|
|
label={buttonLabel}
|
|
|
|
|
action={this.addButtonClicked}
|
2019-10-19 16:38:07 +02:00
|
|
|
disabled={disabled || this.state.entryToAdd === '' || !this.isValid()}
|
2018-09-03 16:17:36 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addButtonClicked = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
this.appendEntry();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
appendEntry = () => {
|
|
|
|
|
const { entryToAdd } = this.state;
|
|
|
|
|
this.props.addEntry(entryToAdd);
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
entryToAdd: '',
|
|
|
|
|
});
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleAddEntryChange = (entryname: string) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
2019-10-19 16:38:07 +02:00
|
|
|
entryToAdd: entryname,
|
2018-09-03 16:17:36 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 10:59:44 +02:00
|
|
|
export default AddEntryToTableField;
|