fix(striker-ui): add input validation to AddUpsInputGroup

main
Tsu-ba-me 2 years ago committed by digimer
parent d3894081f6
commit aa5aad4689
  1. 57
      striker-ui/components/ManageUps/AddUpsInputGroup.tsx
  2. 8
      striker-ui/types/AddUpsInputGroup.d.ts

@ -1,23 +1,41 @@
import { FC, ReactElement, ReactNode, useMemo, useState } from 'react'; import { ReactElement, ReactNode, useMemo, useState } from 'react';
import { BLACK } from '../../lib/consts/DEFAULT_THEME'; import { BLACK } from '../../lib/consts/DEFAULT_THEME';
import CommonUpsInputGroup from './CommonUpsInputGroup'; import CommonUpsInputGroup, {
INPUT_ID_UPS_IP,
INPUT_ID_UPS_NAME,
} from './CommonUpsInputGroup';
import FlexBox from '../FlexBox'; import FlexBox from '../FlexBox';
import Link from '../Link'; import Link from '../Link';
import SelectWithLabel from '../SelectWithLabel'; import SelectWithLabel from '../SelectWithLabel';
import Spinner from '../Spinner'; import Spinner from '../Spinner';
import { BodyText } from '../Text'; import { BodyText } from '../Text';
import useIsFirstRender from '../../hooks/useIsFirstRender';
const INPUT_ID_UPS_TYPE_ID = 'add-ups-select-ups-type-id'; const INPUT_ID_UPS_TYPE = 'add-ups-select-ups-type-id';
const AddUpsInputGroup: FC<AddUpsInputGroupProps> = ({ const INPUT_LABEL_UPS_TYPE = 'UPS type';
const AddUpsInputGroup = <
M extends {
[K in
| typeof INPUT_ID_UPS_IP
| typeof INPUT_ID_UPS_NAME
| typeof INPUT_ID_UPS_TYPE]: string;
},
>({
formUtils,
loading: isExternalLoading, loading: isExternalLoading,
previous = {}, previous = {},
upsTemplate, upsTemplate,
}) => { }: AddUpsInputGroupProps<M>): ReactElement => {
const { buildInputFirstRenderFunction, setValidity } = formUtils;
const { upsTypeId: previousUpsTypeId = '' } = previous; const { upsTypeId: previousUpsTypeId = '' } = previous;
const isFirstRender = useIsFirstRender();
const [inputUpsTypeIdValue, setInputUpsTypeIdValue] = const [inputUpsTypeIdValue, setInputUpsTypeIdValue] =
useState<string>(previousUpsTypeId); useState<string>(previousUpsTypeId);
@ -75,17 +93,19 @@ const AddUpsInputGroup: FC<AddUpsInputGroupProps> = ({
upsTemplate && ( upsTemplate && (
<SelectWithLabel <SelectWithLabel
formControlProps={{ sx: { marginTop: '.3em' } }} formControlProps={{ sx: { marginTop: '.3em' } }}
id={INPUT_ID_UPS_TYPE_ID} id={INPUT_ID_UPS_TYPE}
label="UPS type" label={INPUT_LABEL_UPS_TYPE}
onChange={({ target: { value: rawNewValue } }) => { onChange={({ target: { value: rawNewValue } }) => {
const newValue = String(rawNewValue); const newValue = String(rawNewValue);
setValidity(INPUT_ID_UPS_TYPE, true);
setInputUpsTypeIdValue(newValue); setInputUpsTypeIdValue(newValue);
}} }}
required required
selectItems={upsTypeOptions} selectItems={upsTypeOptions}
selectProps={{ selectProps={{
onClearIndicatorClick: () => { onClearIndicatorClick: () => {
setValidity(INPUT_ID_UPS_TYPE, false);
setInputUpsTypeIdValue(''); setInputUpsTypeIdValue('');
}, },
renderValue: (rawValue) => { renderValue: (rawValue) => {
@ -98,8 +118,9 @@ const AddUpsInputGroup: FC<AddUpsInputGroupProps> = ({
value={inputUpsTypeIdValue} value={inputUpsTypeIdValue}
/> />
), ),
[inputUpsTypeIdValue, upsTypeOptions, upsTemplate], [upsTemplate, upsTypeOptions, inputUpsTypeIdValue, setValidity],
); );
const content = useMemo<ReactElement>( const content = useMemo<ReactElement>(
() => () =>
isExternalLoading ? ( isExternalLoading ? (
@ -107,15 +128,29 @@ const AddUpsInputGroup: FC<AddUpsInputGroupProps> = ({
) : ( ) : (
<FlexBox> <FlexBox>
{pickUpsTypeElement} {pickUpsTypeElement}
{inputUpsTypeIdValue && <CommonUpsInputGroup previous={previous} />} {inputUpsTypeIdValue && (
<CommonUpsInputGroup formUtils={formUtils} previous={previous} />
)}
</FlexBox> </FlexBox>
), ),
[inputUpsTypeIdValue, isExternalLoading, pickUpsTypeElement, previous], [
formUtils,
inputUpsTypeIdValue,
isExternalLoading,
pickUpsTypeElement,
previous,
],
); );
if (isFirstRender) {
buildInputFirstRenderFunction(INPUT_ID_UPS_TYPE)({
isValid: Boolean(inputUpsTypeIdValue),
});
}
return content; return content;
}; };
export { INPUT_ID_UPS_TYPE_ID }; export { INPUT_ID_UPS_TYPE, INPUT_LABEL_UPS_TYPE };
export default AddUpsInputGroup; export default AddUpsInputGroup;

@ -1,7 +1,11 @@
type AddUpsInputGroupOptionalProps = { type AddUpsInputGroupOptionalProps = {
loading?: boolean; loading?: boolean;
previous?: CommonUpsInputGroupProps['previous'] & { upsTypeId?: string }; previous?: CommonUpsInputGroupOptionalProps['previous'] & {
upsTypeId?: string;
};
upsTemplate?: APIUpsTemplate; upsTemplate?: APIUpsTemplate;
}; };
type AddUpsInputGroupProps = AddUpsInputGroupOptionalProps; type AddUpsInputGroupProps<M extends MapToInputTestID> =
AddUpsInputGroupOptionalProps &
Pick<CommonUpsInputGroupProps<M>, 'formUtils'>;

Loading…
Cancel
Save