PromucFlow_constructor/app/client/src/components/ads/TextInput.tsx

202 lines
5.1 KiB
TypeScript
Raw Normal View History

2020-08-14 04:58:03 +00:00
import React, { forwardRef, Ref, useCallback, useMemo, useState } from "react";
import { CommonComponentProps, hexToRgba, Classes } from "./common";
2020-08-14 04:58:03 +00:00
import styled from "styled-components";
import Text, { TextType } from "./Text";
import {
FORM_VALIDATION_INVALID_EMAIL,
ERROR_MESSAGE_NAME_EMPTY,
} from "constants/messages";
import { isEmail } from "utils/formhelpers";
Homepage redesign with themes (#482) * Updating homepage body color * WIP: Fixing scrolls and adding anchors * Removing divider from bottom of the page. * Adding hover background color to app card * Changing edit and launch icons. * Fixing app name paddding in card. * Fixing workspaces overflow * Adding right padding to applications view * Adding share icon to share btn * Fixing Application card styles. * Fixing text decoration in button. * Adding new workspace button * Fixing new workspace and new app styles. * Adding icon sizes. * Fixing Org Name and Org settings menu. * Application menu working * Fixing overlay visibility on app card * Fixing settings page content width. * Fixing workspace icon * Changing app card colors. * Removing debugger * Adding app icon. * Fixing the spaces in application card * Adding storybook-static folder to gitignore. * Adding other storybook files. * Adding menu items for app * Removing cypress selector from text. * Menu width issue fixed * Default app icon color added * Removing hardcoded colors * Removing hardcoded colors. * Light Mode on! * Showing correct icon and color in menu * Update color working properly. * Updating appIcon * Editable text working. * Adding validator * Adding edit permissions to menu * Removing box shadow on app card. * Fixing context menu fill color * Fixing Menu hover issues. * Fixing menu open close hover issues. * Fixing settings pages * Changed Workspace to org. * Fix: State management in EditableText Component (#540) * Error state height is fixed as per design * savingState prop condition fixed * Fixing createnew. * Fixing saving state for application card. * Fixed application card editable text error. * Fixing issue caused during merge. * Fixing tests in create org. * Removing commented code. * Removing unwanted vars. * Fixing delete duplicate tests. * Latest color palette. * Fixing form and table widget. * Removing switcher from header * Removing unused files * Fixing app card context dropdown * Show overlay fix * Adding localStorage support to theme. * Making dark mode the default. Co-authored-by: Rohit Kumawat <rohit.kumawat@primathon.in>
2020-09-16 11:50:47 +00:00
import { useSelector } from "react-redux";
import { getThemeDetails } from "selectors/themeSelectors";
export type Validator = (
value: string,
) => {
isValid: boolean;
message: string;
};
export function emailValidator(email: string) {
let isValid = true;
if (email) {
isValid = isEmail(email);
}
return {
isValid: isValid,
message: !isValid ? FORM_VALIDATION_INVALID_EMAIL : "",
};
}
export function notEmptyValidator(value: string) {
const isValid = !!value;
return {
isValid: isValid,
message: !isValid ? ERROR_MESSAGE_NAME_EMPTY : "",
};
}
2020-08-14 04:58:03 +00:00
export type TextInputProps = CommonComponentProps & {
placeholder?: string;
2020-08-14 04:58:03 +00:00
fill?: boolean;
defaultValue?: string;
validator?: (value: string) => { isValid: boolean; message: string };
onChange?: (value: string) => void;
readOnly?: boolean;
};
2020-08-14 04:58:03 +00:00
type boxReturnType = {
bgColor: string;
color: string;
borderColor: string;
};
Homepage redesign with themes (#482) * Updating homepage body color * WIP: Fixing scrolls and adding anchors * Removing divider from bottom of the page. * Adding hover background color to app card * Changing edit and launch icons. * Fixing app name paddding in card. * Fixing workspaces overflow * Adding right padding to applications view * Adding share icon to share btn * Fixing Application card styles. * Fixing text decoration in button. * Adding new workspace button * Fixing new workspace and new app styles. * Adding icon sizes. * Fixing Org Name and Org settings menu. * Application menu working * Fixing overlay visibility on app card * Fixing settings page content width. * Fixing workspace icon * Changing app card colors. * Removing debugger * Adding app icon. * Fixing the spaces in application card * Adding storybook-static folder to gitignore. * Adding other storybook files. * Adding menu items for app * Removing cypress selector from text. * Menu width issue fixed * Default app icon color added * Removing hardcoded colors * Removing hardcoded colors. * Light Mode on! * Showing correct icon and color in menu * Update color working properly. * Updating appIcon * Editable text working. * Adding validator * Adding edit permissions to menu * Removing box shadow on app card. * Fixing context menu fill color * Fixing Menu hover issues. * Fixing menu open close hover issues. * Fixing settings pages * Changed Workspace to org. * Fix: State management in EditableText Component (#540) * Error state height is fixed as per design * savingState prop condition fixed * Fixing createnew. * Fixing saving state for application card. * Fixed application card editable text error. * Fixing issue caused during merge. * Fixing tests in create org. * Removing commented code. * Removing unwanted vars. * Fixing delete duplicate tests. * Latest color palette. * Fixing form and table widget. * Removing switcher from header * Removing unused files * Fixing app card context dropdown * Show overlay fix * Adding localStorage support to theme. * Making dark mode the default. Co-authored-by: Rohit Kumawat <rohit.kumawat@primathon.in>
2020-09-16 11:50:47 +00:00
const boxStyles = (
props: TextInputProps,
isValid: boolean,
theme: any,
): boxReturnType => {
let bgColor = theme.colors.textInput.normal.bg;
let color = theme.colors.textInput.normal.text;
let borderColor = theme.colors.textInput.normal.border;
2020-08-14 04:58:03 +00:00
if (props.disabled) {
bgColor = theme.colors.textInput.disable.bg;
color = theme.colors.textInput.disable.text;
borderColor = theme.colors.textInput.disable.border;
2020-08-14 04:58:03 +00:00
}
if (props.readOnly) {
bgColor = theme.colors.textInput.readOnly.bg;
color = theme.colors.textInput.readOnly.text;
borderColor = theme.colors.textInput.readOnly.border;
}
2020-08-14 04:58:03 +00:00
if (!isValid) {
bgColor = hexToRgba(theme.colors.danger.main, 0.1);
color = theme.colors.danger.main;
borderColor = theme.colors.danger.main;
}
return { bgColor, color, borderColor };
};
const StyledInput = styled.input<
TextInputProps & { inputStyle: boxReturnType; isValid: boolean }
>`
2020-12-24 04:32:25 +00:00
width: ${(props) => (props.fill ? "100%" : "320px")};
2020-08-14 04:58:03 +00:00
border-radius: 0;
outline: 0;
box-shadow: none;
2020-12-24 04:32:25 +00:00
border: 1px solid ${(props) => props.inputStyle.borderColor};
padding: 0px ${(props) => props.theme.spaces[6]}px;
height: 38px;
2020-12-24 04:32:25 +00:00
background-color: ${(props) => props.inputStyle.bgColor};
color: ${(props) => props.inputStyle.color};
2020-08-14 04:58:03 +00:00
&::placeholder {
2020-12-24 04:32:25 +00:00
color: ${(props) => props.theme.colors.textInput.placeholder};
2020-08-14 04:58:03 +00:00
}
&:disabled {
cursor: not-allowed;
}
2020-12-24 04:32:25 +00:00
${(props) =>
!props.readOnly
? `
2020-08-14 04:58:03 +00:00
&:focus {
border: 1px solid
${
2020-08-14 04:58:03 +00:00
props.isValid
? props.theme.colors.info.main
: props.theme.colors.danger.main
};
box-shadow: ${
props.isValid
? "0px 0px 4px 4px rgba(203, 72, 16, 0.18)"
: "0px 0px 4px 4px rgba(226, 44, 44, 0.18)"
};
2020-08-14 04:58:03 +00:00
}
`
: null};
2020-08-14 04:58:03 +00:00
`;
const InputWrapper = styled.div`
display: flex;
flex-direction: column;
align-items: flex-start;
position: relative;
2020-08-14 04:58:03 +00:00
.${Classes.TEXT} {
2020-12-24 04:32:25 +00:00
color: ${(props) => props.theme.colors.danger.main};
2020-08-14 04:58:03 +00:00
}
`;
const ErrorWrapper = styled.div`
position: absolute;
bottom: -17px;
`;
2020-08-14 04:58:03 +00:00
const TextInput = forwardRef(
(props: TextInputProps, ref: Ref<HTMLInputElement>) => {
const initialValidation = () => {
let validationObj = { isValid: true, message: "" };
if (props.defaultValue && props.validator) {
validationObj = props.validator(props.defaultValue);
}
return validationObj;
};
const [validation, setValidation] = useState<{
isValid: boolean;
message: string;
}>(initialValidation());
Homepage redesign with themes (#482) * Updating homepage body color * WIP: Fixing scrolls and adding anchors * Removing divider from bottom of the page. * Adding hover background color to app card * Changing edit and launch icons. * Fixing app name paddding in card. * Fixing workspaces overflow * Adding right padding to applications view * Adding share icon to share btn * Fixing Application card styles. * Fixing text decoration in button. * Adding new workspace button * Fixing new workspace and new app styles. * Adding icon sizes. * Fixing Org Name and Org settings menu. * Application menu working * Fixing overlay visibility on app card * Fixing settings page content width. * Fixing workspace icon * Changing app card colors. * Removing debugger * Adding app icon. * Fixing the spaces in application card * Adding storybook-static folder to gitignore. * Adding other storybook files. * Adding menu items for app * Removing cypress selector from text. * Menu width issue fixed * Default app icon color added * Removing hardcoded colors * Removing hardcoded colors. * Light Mode on! * Showing correct icon and color in menu * Update color working properly. * Updating appIcon * Editable text working. * Adding validator * Adding edit permissions to menu * Removing box shadow on app card. * Fixing context menu fill color * Fixing Menu hover issues. * Fixing menu open close hover issues. * Fixing settings pages * Changed Workspace to org. * Fix: State management in EditableText Component (#540) * Error state height is fixed as per design * savingState prop condition fixed * Fixing createnew. * Fixing saving state for application card. * Fixed application card editable text error. * Fixing issue caused during merge. * Fixing tests in create org. * Removing commented code. * Removing unwanted vars. * Fixing delete duplicate tests. * Latest color palette. * Fixing form and table widget. * Removing switcher from header * Removing unused files * Fixing app card context dropdown * Show overlay fix * Adding localStorage support to theme. * Making dark mode the default. Co-authored-by: Rohit Kumawat <rohit.kumawat@primathon.in>
2020-09-16 11:50:47 +00:00
const theme = useSelector(getThemeDetails).theme;
const inputStyle = useMemo(
() => boxStyles(props, validation.isValid, theme),
[props, validation.isValid, theme],
Homepage redesign with themes (#482) * Updating homepage body color * WIP: Fixing scrolls and adding anchors * Removing divider from bottom of the page. * Adding hover background color to app card * Changing edit and launch icons. * Fixing app name paddding in card. * Fixing workspaces overflow * Adding right padding to applications view * Adding share icon to share btn * Fixing Application card styles. * Fixing text decoration in button. * Adding new workspace button * Fixing new workspace and new app styles. * Adding icon sizes. * Fixing Org Name and Org settings menu. * Application menu working * Fixing overlay visibility on app card * Fixing settings page content width. * Fixing workspace icon * Changing app card colors. * Removing debugger * Adding app icon. * Fixing the spaces in application card * Adding storybook-static folder to gitignore. * Adding other storybook files. * Adding menu items for app * Removing cypress selector from text. * Menu width issue fixed * Default app icon color added * Removing hardcoded colors * Removing hardcoded colors. * Light Mode on! * Showing correct icon and color in menu * Update color working properly. * Updating appIcon * Editable text working. * Adding validator * Adding edit permissions to menu * Removing box shadow on app card. * Fixing context menu fill color * Fixing Menu hover issues. * Fixing menu open close hover issues. * Fixing settings pages * Changed Workspace to org. * Fix: State management in EditableText Component (#540) * Error state height is fixed as per design * savingState prop condition fixed * Fixing createnew. * Fixing saving state for application card. * Fixed application card editable text error. * Fixing issue caused during merge. * Fixing tests in create org. * Removing commented code. * Removing unwanted vars. * Fixing delete duplicate tests. * Latest color palette. * Fixing form and table widget. * Removing switcher from header * Removing unused files * Fixing app card context dropdown * Show overlay fix * Adding localStorage support to theme. * Making dark mode the default. Co-authored-by: Rohit Kumawat <rohit.kumawat@primathon.in>
2020-09-16 11:50:47 +00:00
);
2020-08-14 04:58:03 +00:00
const memoizedChangeHandler = useCallback(
2020-12-24 04:32:25 +00:00
(el) => {
const inputValue = el.target.value.trim();
const validation = props.validator && props.validator(inputValue);
if (validation) {
props.validator && setValidation(validation);
return (
validation.isValid && props.onChange && props.onChange(inputValue)
);
} else {
return props.onChange && props.onChange(inputValue);
}
2020-08-14 04:58:03 +00:00
},
[props],
);
const ErrorMessage = (
<ErrorWrapper>
<Text type={TextType.P3}>{validation.message}</Text>
</ErrorWrapper>
);
2020-08-14 04:58:03 +00:00
return (
<InputWrapper>
2020-08-14 04:58:03 +00:00
<StyledInput
type="text"
ref={ref}
inputStyle={inputStyle}
isValid={validation.isValid}
defaultValue={props.defaultValue}
{...props}
placeholder={props.placeholder}
2020-08-14 04:58:03 +00:00
onChange={memoizedChangeHandler}
readOnly={props.readOnly}
data-cy={props.cypressSelector}
2020-08-14 04:58:03 +00:00
/>
{ErrorMessage}
2020-08-14 04:58:03 +00:00
</InputWrapper>
);
},
);
TextInput.displayName = "TextInput";
export default TextInput;