set repository name suggestion in import form just at leaving the input field

This commit is contained in:
Eduard Heimbuch
2020-12-03 09:38:23 +01:00
parent dd0bcc4a3f
commit 715c76e2f4
2 changed files with 17 additions and 7 deletions

View File

@@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
import React, { ChangeEvent, KeyboardEvent } from "react"; import React, { ChangeEvent, KeyboardEvent, FocusEvent } from "react";
import classNames from "classnames"; import classNames from "classnames";
import LabelWithHelpIcon from "./LabelWithHelpIcon"; import LabelWithHelpIcon from "./LabelWithHelpIcon";
import { createAttributesForTesting } from "../devBuild"; import { createAttributesForTesting } from "../devBuild";
@@ -41,6 +41,7 @@ type Props = {
helpText?: string; helpText?: string;
className?: string; className?: string;
testId?: string; testId?: string;
onBlur?: (value: string, name?: string) => void;
}; };
class InputField extends React.Component<Props> { class InputField extends React.Component<Props> {
@@ -61,6 +62,12 @@ class InputField extends React.Component<Props> {
this.props.onChange(event.target.value, this.props.name); this.props.onChange(event.target.value, this.props.name);
}; };
handleBlur = (event: FocusEvent<HTMLInputElement>) => {
if (this.props.onBlur) {
this.props.onBlur(event.target.value, this.props.name);
}
};
handleKeyPress = (event: KeyboardEvent<HTMLInputElement>) => { handleKeyPress = (event: KeyboardEvent<HTMLInputElement>) => {
const onReturnPressed = this.props.onReturnPressed; const onReturnPressed = this.props.onReturnPressed;
if (!onReturnPressed) { if (!onReturnPressed) {
@@ -102,6 +109,7 @@ class InputField extends React.Component<Props> {
onChange={this.handleInput} onChange={this.handleInput}
onKeyPress={this.handleKeyPress} onKeyPress={this.handleKeyPress}
disabled={disabled} disabled={disabled}
onBlur={this.handleBlur}
{...createAttributesForTesting(testId)} {...createAttributesForTesting(testId)}
/> />
</div> </div>

View File

@@ -48,19 +48,20 @@ const ImportFromUrlForm: FC<Props> = ({ repository, onChange, setValid, disabled
const [urlValidationError, setUrlValidationError] = useState(false); const [urlValidationError, setUrlValidationError] = useState(false);
const handleImportUrlChange = (importUrl: string) => { const handleImportUrlChange = (importUrl: string) => {
const changedRepo = { ...repository, importUrl }; onChange({ ...repository, importUrl });
const valid = validation.isUrlValid(importUrl);
setUrlValidationError(!valid);
setValid(valid);
};
const handleImportUrlBlur = (importUrl: string) => {
if (!repository.name) { if (!repository.name) {
// If the repository name is not fill we set a name suggestion // If the repository name is not fill we set a name suggestion
const match = importUrl.match(/([^\/]+?)(?:.git)?$/); const match = importUrl.match(/([^\/]+?)(?:.git)?$/);
if (match && match[1]) { if (match && match[1]) {
changedRepo.name = match[1]; onChange({ ...repository, name: match[1] });
} }
} }
onChange(changedRepo);
const valid = validation.isUrlValid(importUrl);
setUrlValidationError(!valid);
setValid(valid);
}; };
return ( return (
@@ -74,6 +75,7 @@ const ImportFromUrlForm: FC<Props> = ({ repository, onChange, setValid, disabled
validationError={urlValidationError} validationError={urlValidationError}
errorMessage={t("validation.url-invalid")} errorMessage={t("validation.url-invalid")}
disabled={disabled} disabled={disabled}
onBlur={handleImportUrlBlur}
/> />
</Column> </Column>
<Column className="column is-half"> <Column className="column is-half">