2020-01-27 21:28:27 -07:00
|
|
|
import React, { useState } from "react";
|
2020-01-17 02:50:57 -07:00
|
|
|
import styled from "@emotion/styled";
|
2020-01-27 21:28:27 -07:00
|
|
|
import { Input, useColorMode } from "@chakra-ui/core";
|
2020-01-17 02:50:57 -07:00
|
|
|
|
|
|
|
const StyledInput = styled(Input)`
|
|
|
|
&::placeholder {
|
|
|
|
color: ${props => props.placeholderColor};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-01-27 21:28:27 -07:00
|
|
|
const fqdnPattern = /(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)/gim;
|
|
|
|
|
|
|
|
const bg = { dark: "whiteAlpha.100", light: "white" };
|
|
|
|
const color = { dark: "whiteAlpha.800", light: "gray.400" };
|
|
|
|
const border = { dark: "whiteAlpha.50", light: "gray.100" };
|
|
|
|
const placeholderColor = { dark: "whiteAlpha.400", light: "gray.400" };
|
|
|
|
|
|
|
|
const QueryTarget = ({
|
|
|
|
placeholder,
|
|
|
|
register,
|
|
|
|
setFqdn,
|
|
|
|
name,
|
|
|
|
value,
|
2020-01-28 18:42:28 -10:00
|
|
|
setTarget,
|
2020-01-27 21:28:27 -07:00
|
|
|
resolveTarget,
|
|
|
|
displayValue,
|
|
|
|
setDisplayValue
|
|
|
|
}) => {
|
2020-01-17 02:50:57 -07:00
|
|
|
const { colorMode } = useColorMode();
|
2020-01-27 21:28:27 -07:00
|
|
|
|
|
|
|
const handleBlur = () => {
|
|
|
|
if (resolveTarget && displayValue && fqdnPattern.test(displayValue)) {
|
|
|
|
setFqdn(displayValue);
|
|
|
|
} else if (resolveTarget && !displayValue) {
|
|
|
|
setFqdn(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const handleChange = e => {
|
|
|
|
setDisplayValue(e.target.value);
|
2020-01-28 18:42:28 -10:00
|
|
|
setTarget({ field: name, value: e.target.value });
|
2020-01-27 21:28:27 -07:00
|
|
|
};
|
|
|
|
const handleKeyDown = e => {
|
|
|
|
if ([9, 13].includes(e.keyCode)) {
|
|
|
|
handleBlur();
|
|
|
|
}
|
|
|
|
};
|
2020-01-17 02:50:57 -07:00
|
|
|
return (
|
2020-01-27 21:28:27 -07:00
|
|
|
<>
|
|
|
|
<input hidden readOnly name={name} ref={register} value={value} />
|
|
|
|
<StyledInput
|
|
|
|
size="lg"
|
|
|
|
name="query_target_display"
|
|
|
|
bg={bg[colorMode]}
|
|
|
|
onBlur={handleBlur}
|
|
|
|
onFocus={handleBlur}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
value={displayValue}
|
|
|
|
borderRadius="0.25rem"
|
|
|
|
onChange={handleChange}
|
|
|
|
color={color[colorMode]}
|
|
|
|
placeholder={placeholder}
|
|
|
|
borderColor={border[colorMode]}
|
|
|
|
placeholderColor={placeholderColor[colorMode]}
|
|
|
|
/>
|
|
|
|
</>
|
2020-01-17 02:50:57 -07:00
|
|
|
);
|
|
|
|
};
|
2020-01-27 21:28:27 -07:00
|
|
|
|
|
|
|
QueryTarget.displayName = "QueryTarget";
|
|
|
|
export default QueryTarget;
|