Commit f350b105 authored by tom's avatar tom

empty, filled, disabled and error styles

parent 138c1689
...@@ -58,11 +58,16 @@ function getFloatingVariantStylesForSize(size: 'md' | 'lg', props: StyleFunction ...@@ -58,11 +58,16 @@ function getFloatingVariantStylesForSize(size: 'md' | 'lg', props: StyleFunction
label: activeLabelStyles, label: activeLabelStyles,
'input, textarea': activeInputStyles, 'input, textarea': activeInputStyles,
}, },
'&[aria-active=true] label': activeLabelStyles,
// label styles // label styles
label: FormLabel.sizes?.[size](props) || {}, label: FormLabel.sizes?.[size](props) || {},
'input:not(:placeholder-shown) + label, textarea:not(:placeholder-shown) + label': activeLabelStyles, 'input:not(:placeholder-shown) + label, textarea:not(:placeholder-shown) + label': activeLabelStyles,
'input[aria-invalid=true] + label, textarea[aria-invalid=true] + label': { [`
input[aria-invalid=true] + label,
textarea[aria-invalid=true] + label,
&[aria-invalid=true] label
`]: {
color: getColor(theme, errorColor), color: getColor(theme, errorColor),
}, },
...@@ -74,7 +79,11 @@ function getFloatingVariantStylesForSize(size: 'md' | 'lg', props: StyleFunction ...@@ -74,7 +79,11 @@ function getFloatingVariantStylesForSize(size: 'md' | 'lg', props: StyleFunction
padding: inputPx, padding: inputPx,
}, },
'input:not(:placeholder-shown), textarea:not(:placeholder-shown)': activeInputStyles, 'input:not(:placeholder-shown), textarea:not(:placeholder-shown)': activeInputStyles,
'input[disabled] + label, textarea[disabled] + label': { [`
input[disabled] + label,
textarea[disabled] + label,
&[aria-disabled=true] label
`]: {
backgroundColor: 'transparent', backgroundColor: 'transparent',
}, },
...@@ -82,7 +91,11 @@ function getFloatingVariantStylesForSize(size: 'md' | 'lg', props: StyleFunction ...@@ -82,7 +91,11 @@ function getFloatingVariantStylesForSize(size: 'md' | 'lg', props: StyleFunction
'input:not(:placeholder-shown) + label .chakra-form__required-indicator, textarea:not(:placeholder-shown) + label .chakra-form__required-indicator': { 'input:not(:placeholder-shown) + label .chakra-form__required-indicator, textarea:not(:placeholder-shown) + label .chakra-form__required-indicator': {
color: getColor(theme, focusPlaceholderColor), color: getColor(theme, focusPlaceholderColor),
}, },
'input[aria-invalid=true] + label .chakra-form__required-indicator, textarea[aria-invalid=true] + label .chakra-form__required-indicator': { [`
input[aria-invalid=true] + label .chakra-form__required-indicator,
textarea[aria-invalid=true] + label .chakra-form__required-indicator,
&[aria-invalid=true] .chakra-form__required-indicator
`]: {
color: getColor(theme, errorColor), color: getColor(theme, errorColor),
}, },
}, },
......
...@@ -22,14 +22,6 @@ const variantFloating = defineStyle((props) => { ...@@ -22,14 +22,6 @@ const variantFloating = defineStyle((props) => {
const { focusPlaceholderColor } = getDefaultFormColors(props); const { focusPlaceholderColor } = getDefaultFormColors(props);
const bc = backgroundColor || mode('white', 'black')(props); const bc = backgroundColor || mode('white', 'black')(props);
const activeStyles = {
backgroundColor: bc,
color: getColor(theme, focusPlaceholderColor),
fontSize: 'xs',
lineHeight: '16px',
borderTopRightRadius: 'none',
};
return { return {
left: '2px', left: '2px',
top: '2px', top: '2px',
...@@ -46,8 +38,13 @@ const variantFloating = defineStyle((props) => { ...@@ -46,8 +38,13 @@ const variantFloating = defineStyle((props) => {
overflow: 'hidden', overflow: 'hidden',
whiteSpace: 'nowrap', whiteSpace: 'nowrap',
textOverflow: 'ellipsis', textOverflow: 'ellipsis',
_focusWithin: activeStyles, _focusWithin: {
'&[data-active=true]': activeStyles, backgroundColor: bc,
color: getColor(theme, focusPlaceholderColor),
fontSize: 'xs',
lineHeight: '16px',
borderTopRightRadius: 'none',
},
}; };
}); });
...@@ -58,17 +55,14 @@ const variants = { ...@@ -58,17 +55,14 @@ const variants = {
const sizes = { const sizes = {
lg: defineStyle((props) => { lg: defineStyle((props) => {
if (props.variant === 'floating') { if (props.variant === 'floating') {
const activeStyles = {
padding: '16px 24px 2px 24px',
};
return { return {
fontSize: 'md', fontSize: 'md',
lineHeight: '24px', lineHeight: '24px',
padding: '28px 24px', padding: '28px 24px',
right: '26px', right: '26px',
_focusWithin: activeStyles, _focusWithin: {
'&[data-active=true]': activeStyles, padding: '16px 24px 2px 24px',
},
'&[data-fancy=true]': { '&[data-fancy=true]': {
right: '36px', right: '36px',
}, },
...@@ -79,17 +73,14 @@ const sizes = { ...@@ -79,17 +73,14 @@ const sizes = {
}), }),
md: defineStyle((props) => { md: defineStyle((props) => {
if (props.variant === 'floating') { if (props.variant === 'floating') {
const activeStyles = {
padding: '10px 16px 2px 16px',
};
return { return {
fontSize: 'md', fontSize: 'md',
lineHeight: '20px', lineHeight: '20px',
padding: '18px 16px', padding: '18px 16px',
right: '18px', right: '18px',
_focusWithin: activeStyles, _focusWithin: {
'&[data-active=true]': activeStyles, padding: '10px 16px 2px 16px',
},
'&[data-fancy=true]': { '&[data-fancy=true]': {
right: '36px', right: '36px',
}, },
......
import { FormControl, useBoolean, useToken } from '@chakra-ui/react'; import { FormControl, useBoolean, useToken, useColorMode } from '@chakra-ui/react';
import type { Size, CSSObjectWithLabel, OptionsOrGroups, GroupBase, SingleValue, MultiValue } from 'chakra-react-select'; import type { Size, CSSObjectWithLabel, OptionsOrGroups, GroupBase, SingleValue, MultiValue } from 'chakra-react-select';
import { Select } from 'chakra-react-select'; import { Select } from 'chakra-react-select';
import React from 'react'; import React from 'react';
...@@ -6,7 +6,7 @@ import type { FieldError } from 'react-hook-form'; ...@@ -6,7 +6,7 @@ import type { FieldError } from 'react-hook-form';
import type { Option } from './types'; import type { Option } from './types';
import { chakraStyles } from 'ui/shared/FancySelect/utils'; import { getChakraStyles } from 'ui/shared/FancySelect/utils';
import InputPlaceholder from 'ui/shared/InputPlaceholder'; import InputPlaceholder from 'ui/shared/InputPlaceholder';
interface Props { interface Props {
...@@ -25,6 +25,7 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur ...@@ -25,6 +25,7 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur
const [ hasValue, setHasValue ] = useBoolean(false); const [ hasValue, setHasValue ] = useBoolean(false);
const menuZIndex = useToken('zIndices', 'dropdown'); const menuZIndex = useToken('zIndices', 'dropdown');
const { colorMode } = useColorMode();
const handleChange = React.useCallback((newValue: SingleValue<Option> | MultiValue<Option>) => { const handleChange = React.useCallback((newValue: SingleValue<Option> | MultiValue<Option>) => {
if (Array.isArray(newValue)) { if (Array.isArray(newValue)) {
...@@ -48,8 +49,17 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur ...@@ -48,8 +49,17 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur
menuPortal: (provided: CSSObjectWithLabel) => ({ ...provided, zIndex: menuZIndex }), menuPortal: (provided: CSSObjectWithLabel) => ({ ...provided, zIndex: menuZIndex }),
}), [ menuZIndex ]); }), [ menuZIndex ]);
const chakraStyles = React.useMemo(() => getChakraStyles(colorMode), [ colorMode ]);
return ( return (
<FormControl variant="floating" size={ size } isRequired={ isRequired }> <FormControl
variant="floating"
size={ size }
isRequired={ isRequired }
{ ...(error ? { 'aria-invalid': true } : {}) }
{ ...(isDisabled ? { 'aria-disabled': true } : {}) }
{ ...(hasValue ? { 'aria-active': true } : {}) }
>
<Select <Select
menuPortalTarget={ window.document.body } menuPortalTarget={ window.document.body }
placeholder="" placeholder=""
...@@ -67,7 +77,6 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur ...@@ -67,7 +77,6 @@ const FancySelect = ({ size = 'md', options, placeholder, name, onChange, onBlur
/> />
<InputPlaceholder <InputPlaceholder
text={ placeholder } text={ placeholder }
isActive={ hasValue }
error={ error } error={ error }
isFancy isFancy
/> />
......
import type { ColorMode } from '@chakra-ui/react';
import type { Size, ChakraStylesConfig } from 'chakra-react-select'; import type { Size, ChakraStylesConfig } from 'chakra-react-select';
import type { Option } from './types'; import type { Option } from './types';
...@@ -40,25 +41,32 @@ function getSingleValueStyles(size?: Size) { ...@@ -40,25 +41,32 @@ function getSingleValueStyles(size?: Size) {
} }
} }
const chakraStyles: ChakraStylesConfig<Option> = { const getChakraStyles: (colorMode: ColorMode) => ChakraStylesConfig<Option> = (colorMode) => {
// control: (provided) => ({ const emptyInputBorderColor = colorMode === 'dark' ? 'gray.700' : 'gray.100';
// ...provided, const filledInputBorderColor = colorMode === 'dark' ? 'gray.600' : 'gray.300';
// }),
inputContainer: (provided) => ({ return {
...provided, control: (provided, state) => ({
py: 0, ...provided,
mx: 0, borderColor: state.hasValue ? filledInputBorderColor : emptyInputBorderColor,
}), }),
valueContainer: (provided, state) => ({ inputContainer: (provided) => ({
...provided, ...provided,
...getValueContainerStyles(state.selectProps.size), py: 0,
}), mx: 0,
singleValue: (provided, state) => ({ }),
...provided, valueContainer: (provided, state) => ({
mx: 0, ...provided,
transform: 'none', ...getValueContainerStyles(state.selectProps.size),
...getSingleValueStyles(state.selectProps.size), py: 0,
}), }),
singleValue: (provided, state) => ({
...provided,
mx: 0,
transform: 'none',
...getSingleValueStyles(state.selectProps.size),
}),
};
}; };
export { chakraStyles }; export { getChakraStyles };
...@@ -6,11 +6,10 @@ interface Props { ...@@ -6,11 +6,10 @@ interface Props {
text: string; text: string;
error?: Partial<FieldError>; error?: Partial<FieldError>;
className?: string; className?: string;
isActive?: boolean;
isFancy?: boolean; isFancy?: boolean;
} }
const InputPlaceholder = ({ text, error, className, isActive, isFancy }: Props) => { const InputPlaceholder = ({ text, error, className, isFancy }: Props) => {
let errorMessage = error?.message; let errorMessage = error?.message;
if (!errorMessage && error?.type === 'pattern') { if (!errorMessage && error?.type === 'pattern') {
...@@ -20,7 +19,6 @@ const InputPlaceholder = ({ text, error, className, isActive, isFancy }: Props) ...@@ -20,7 +19,6 @@ const InputPlaceholder = ({ text, error, className, isActive, isFancy }: Props)
return ( return (
<FormLabel <FormLabel
className={ className } className={ className }
{ ...(isActive ? { 'data-active': true } : {}) }
{ ...(isFancy ? { 'data-fancy': true } : {}) } { ...(isFancy ? { 'data-fancy': true } : {}) }
> >
<chakra.span>{ text }</chakra.span> <chakra.span>{ text }</chakra.span>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment