fix(striker-ui): add input sensitivity and expose tooltip props in CommonFenceInputGroup

main
Tsu-ba-me 2 years ago
parent f928da854e
commit f62342317d
  1. 98
      striker-ui/components/CommonFenceInputGroup.tsx
  2. 10
      striker-ui/types/CommonFenceInputGroup.d.ts

@ -1,22 +1,30 @@
import { Box, styled, Tooltip } from '@mui/material';
import { FC, ReactElement, ReactNode, useMemo } from 'react'; import { FC, ReactElement, ReactNode, useMemo } from 'react';
import INPUT_TYPES from '../lib/consts/INPUT_TYPES';
import FlexBox from './FlexBox'; import FlexBox from './FlexBox';
import InputWithRef from './InputWithRef'; import InputWithRef from './InputWithRef';
import OutlinedInputWithLabel from './OutlinedInputWithLabel'; import OutlinedInputWithLabel from './OutlinedInputWithLabel';
import { ExpandablePanel } from './Panels'; import { ExpandablePanel } from './Panels';
import SelectWithLabel from './SelectWithLabel'; import SelectWithLabel from './SelectWithLabel';
import SwitchWithLabel from './SwitchWithLabel'; import SwitchWithLabel from './SwitchWithLabel';
import { BodyText } from './Text';
const CHECKED_STATES: Array<string | undefined> = ['1', 'on']; const CHECKED_STATES: Array<string | undefined> = ['1', 'on'];
const ID_SEPARATOR = '-'; const ID_SEPARATOR = '-';
const MAP_TO_INPUT_BUILDER: MapToInputBuilder = { const MAP_TO_INPUT_BUILDER: MapToInputBuilder = {
boolean: ({ id, isChecked = false, label, name = id }) => ( boolean: (args) => {
const { id, isChecked = false, label, name = id } = args;
return (
<InputWithRef <InputWithRef
key={`${id}-wrapper`} key={`${id}-wrapper`}
input={ input={
<SwitchWithLabel <SwitchWithLabel
checked={isChecked} checked={isChecked}
flexBoxProps={{ width: '100%' }}
id={id} id={id}
label={label} label={label}
name={name} name={name}
@ -24,15 +32,19 @@ const MAP_TO_INPUT_BUILDER: MapToInputBuilder = {
} }
valueType="boolean" valueType="boolean"
/> />
), );
select: ({ },
select: (args) => {
const {
id, id,
isRequired, isRequired,
label, label,
name = id, name = id,
selectOptions = [], selectOptions = [],
value = '', value = '',
}) => ( } = args;
return (
<InputWithRef <InputWithRef
key={`${id}-wrapper`} key={`${id}-wrapper`}
input={ input={
@ -46,27 +58,48 @@ const MAP_TO_INPUT_BUILDER: MapToInputBuilder = {
} }
required={isRequired} required={isRequired}
/> />
), );
string: ({ id, isRequired, label = '', name = id, value }) => ( },
string: (args) => {
const {
id,
isRequired,
isSensitive = false,
label = '',
name = id,
value,
} = args;
return (
<InputWithRef <InputWithRef
key={`${id}-wrapper`} key={`${id}-wrapper`}
input={ input={
<OutlinedInputWithLabel <OutlinedInputWithLabel
id={id} id={id}
inputProps={{
inputProps: { 'data-sensitive': isSensitive },
}}
label={label} label={label}
name={name} name={name}
value={value} value={value}
type={isSensitive ? INPUT_TYPES.password : undefined}
/> />
} }
required={isRequired} required={isRequired}
/> />
), );
},
}; };
const combineIds = (...pieces: string[]) => pieces.join(ID_SEPARATOR); const combineIds = (...pieces: string[]) => pieces.join(ID_SEPARATOR);
const FenceInputWrapper = styled(FlexBox)({
margin: '.4em 0',
});
const CommonFenceInputGroup: FC<CommonFenceInputGroupProps> = ({ const CommonFenceInputGroup: FC<CommonFenceInputGroupProps> = ({
fenceId, fenceId,
fenceParameterTooltipProps,
fenceTemplate, fenceTemplate,
previousFenceName, previousFenceName,
previousFenceParameters, previousFenceParameters,
@ -103,41 +136,64 @@ const CommonFenceInputGroup: FC<CommonFenceInputGroupProps> = ({
[ [
parameterId, parameterId,
{ {
content_type: contentType, content_type: parameterType,
default: parameterDefault, default: parameterDefault,
deprecated: rawDeprecated, deprecated: rawParameterDeprecated,
description: parameterDescription,
options: parameterSelectOptions, options: parameterSelectOptions,
required: rawRequired, required: rawParameterRequired,
}, },
], ],
) => { ) => {
const isParameterDeprecated = String(rawDeprecated) === '1'; const isParameterDeprecated =
String(rawParameterDeprecated) === '1';
if (!isParameterDeprecated) { if (!isParameterDeprecated) {
const { optional, required } = previous; const { optional, required } = previous;
const buildInput = const buildInput =
MAP_TO_INPUT_BUILDER[contentType] ?? MAP_TO_INPUT_BUILDER[parameterType] ??
MAP_TO_INPUT_BUILDER.string; MAP_TO_INPUT_BUILDER.string;
const fenceJoinParameterId = combineIds(fenceId, parameterId); const fenceJoinParameterId = combineIds(fenceId, parameterId);
const initialValue = const initialValue =
mapToPreviousFenceParameterValues[fenceJoinParameterId] ?? mapToPreviousFenceParameterValues[fenceJoinParameterId] ??
parameterDefault; parameterDefault;
const isParameterRequired = String(rawRequired) === '1'; const isParameterRequired =
String(rawParameterRequired) === '1';
const isParameterSensitive = /passw/i.test(parameterId);
const parameterInput = buildInput({ const parameterInput = buildInput({
id: fenceJoinParameterId, id: fenceJoinParameterId,
isChecked: CHECKED_STATES.includes(initialValue), isChecked: CHECKED_STATES.includes(initialValue),
isRequired: isParameterRequired, isRequired: isParameterRequired,
isSensitive: isParameterSensitive,
label: parameterId, label: parameterId,
selectOptions: parameterSelectOptions, selectOptions: parameterSelectOptions,
value: initialValue, value: initialValue,
}); });
const parameterInputWithTooltip = (
<Tooltip
componentsProps={{
tooltip: {
sx: {
maxWidth: { md: '62.6em' },
},
},
}}
disableInteractive
key={`${fenceJoinParameterId}-tooltip`}
placement="top-start"
title={<BodyText>{parameterDescription}</BodyText>}
{...fenceParameterTooltipProps}
>
<Box>{parameterInput}</Box>
</Tooltip>
);
if (isParameterRequired) { if (isParameterRequired) {
required.push(parameterInput); required.push(parameterInputWithTooltip);
} else { } else {
optional.push(parameterInput); optional.push(parameterInputWithTooltip);
} }
} }
@ -164,17 +220,23 @@ const CommonFenceInputGroup: FC<CommonFenceInputGroupProps> = ({
}} }}
> >
<ExpandablePanel expandInitially header="Required parameters"> <ExpandablePanel expandInitially header="Required parameters">
<FlexBox margin=".4em 0">{requiredInputs}</FlexBox> <FenceInputWrapper>{requiredInputs}</FenceInputWrapper>
</ExpandablePanel> </ExpandablePanel>
<ExpandablePanel header="Optional parameters"> <ExpandablePanel header="Optional parameters">
<FlexBox margin=".4em 0">{optionalInputs}</FlexBox> <FenceInputWrapper>{optionalInputs}</FenceInputWrapper>
</ExpandablePanel> </ExpandablePanel>
</FlexBox> </FlexBox>
); );
} }
return result; return result;
}, [fenceId, fenceTemplate, previousFenceName, previousFenceParameters]); }, [
fenceId,
fenceParameterTooltipProps,
fenceTemplate,
previousFenceName,
previousFenceParameters,
]);
return <>{fenceParameterElements}</>; return <>{fenceParameterElements}</>;
}; };

@ -1,12 +1,17 @@
type FenceParameterInputBuilder = (args: { type FenceParameterInputBuilderParameters = {
id: string; id: string;
isChecked?: boolean; isChecked?: boolean;
isRequired?: boolean; isRequired?: boolean;
isSensitive?: boolean;
label?: string; label?: string;
name?: string; name?: string;
selectOptions?: string[]; selectOptions?: string[];
value?: string; value?: string;
}) => ReactElement; };
type FenceParameterInputBuilder = (
args: FenceParameterInputBuilderParameters,
) => ReactElement;
type MapToInputBuilder = Partial< type MapToInputBuilder = Partial<
Record<Exclude<FenceParameterType, 'string'>, FenceParameterInputBuilder> Record<Exclude<FenceParameterType, 'string'>, FenceParameterInputBuilder>
@ -17,6 +22,7 @@ type CommonFenceInputGroupOptionalProps = {
fenceTemplate?: APIFenceTemplate; fenceTemplate?: APIFenceTemplate;
previousFenceName?: string; previousFenceName?: string;
previousFenceParameters?: FenceParameters; previousFenceParameters?: FenceParameters;
fenceParameterTooltipProps?: import('@mui/material').TooltipProps;
}; };
type CommonFenceInputGroupProps = CommonFenceInputGroupOptionalProps; type CommonFenceInputGroupProps = CommonFenceInputGroupOptionalProps;

Loading…
Cancel
Save