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

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

@ -1,72 +1,105 @@
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) => {
<InputWithRef const { id, isChecked = false, label, name = id } = args;
key={`${id}-wrapper`}
input={ return (
<SwitchWithLabel <InputWithRef
checked={isChecked} key={`${id}-wrapper`}
id={id} input={
label={label} <SwitchWithLabel
name={name} checked={isChecked}
/> flexBoxProps={{ width: '100%' }}
} id={id}
valueType="boolean" label={label}
/> name={name}
), />
select: ({ }
id, valueType="boolean"
isRequired, />
label, );
name = id, },
selectOptions = [], select: (args) => {
value = '', const {
}) => ( id,
<InputWithRef isRequired,
key={`${id}-wrapper`} label,
input={ name = id,
<SelectWithLabel selectOptions = [],
id={id} value = '',
label={label} } = args;
name={name}
selectItems={selectOptions} return (
value={value} <InputWithRef
/> key={`${id}-wrapper`}
} input={
required={isRequired} <SelectWithLabel
/> id={id}
), label={label}
string: ({ id, isRequired, label = '', name = id, value }) => ( name={name}
<InputWithRef selectItems={selectOptions}
key={`${id}-wrapper`} value={value}
input={ />
<OutlinedInputWithLabel }
id={id} required={isRequired}
label={label} />
name={name} );
value={value} },
/> string: (args) => {
} const {
required={isRequired} id,
/> isRequired,
), isSensitive = false,
label = '',
name = id,
value,
} = args;
return (
<InputWithRef
key={`${id}-wrapper`}
input={
<OutlinedInputWithLabel
id={id}
inputProps={{
inputProps: { 'data-sensitive': isSensitive },
}}
label={label}
name={name}
value={value}
type={isSensitive ? INPUT_TYPES.password : undefined}
/>
}
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