fix(striker-ui): allow NetworkInitForm to toggle submit disabled prop

main
Tsu-ba-me 2 years ago
parent 23d6669bd7
commit 10afba41cb
  1. 119
      striker-ui/components/NetworkInitForm.tsx
  2. 1
      striker-ui/types/ToggleSubmitDisabledFunction.d.ts

@ -143,6 +143,13 @@ const REQUIRED_NETWORKS: NetworkInput[] = [
const BASE_INPUT_COUNT = 2; const BASE_INPUT_COUNT = 2;
const MAX_INTERFACES_PER_NETWORK = 2; const MAX_INTERFACES_PER_NETWORK = 2;
const PER_NETWORK_INPUT_COUNT = 3; const PER_NETWORK_INPUT_COUNT = 3;
const INPUT_TEST_IDS = {
dnsCSV: 'domainNameServerCSV',
gateway: 'gateway',
networkName: (prefix: string) => `${prefix}Name`,
networkIPAddress: (prefix: string) => `${prefix}IPAddress`,
networkSubnet: (prefix: string) => `${prefix}SubnetMask`,
};
const NETWORK_INTERFACE_TEMPLATE = Array.from( const NETWORK_INTERFACE_TEMPLATE = Array.from(
{ length: MAX_INTERFACES_PER_NETWORK }, { length: MAX_INTERFACES_PER_NETWORK },
@ -265,6 +272,8 @@ const NetworkForm: FC<{
setNetworkInterfaceInputMap: Dispatch< setNetworkInterfaceInputMap: Dispatch<
SetStateAction<NetworkInterfaceInputMap> SetStateAction<NetworkInterfaceInputMap>
>; >;
testAllInputs: (...excludeTestIds: string[]) => boolean;
toggleSubmitDisabled?: ToggleSubmitDisabledFunction;
}> = ({ }> = ({
createDropMouseUpHandler, createDropMouseUpHandler,
getNetworkTypeCount, getNetworkTypeCount,
@ -276,6 +285,8 @@ const NetworkForm: FC<{
optionalNetworkInputsLength, optionalNetworkInputsLength,
setNetworkInputs, setNetworkInputs,
setNetworkInterfaceInputMap, setNetworkInterfaceInputMap,
testAllInputs,
toggleSubmitDisabled,
}) => { }) => {
const theme = useTheme(); const theme = useTheme();
const breakpointMedium = useMediaQuery(theme.breakpoints.up('md')); const breakpointMedium = useMediaQuery(theme.breakpoints.up('md'));
@ -286,9 +297,23 @@ const NetworkForm: FC<{
const { inputUUID, interfaces, ipAddress, subnetMask, type } = networkInput; const { inputUUID, interfaces, ipAddress, subnetMask, type } = networkInput;
const inputTestPrefix = `network${networkIndex}`; const inputTestPrefix = useMemo(
() => `network${networkIndex}`,
[networkIndex],
);
const ipAddressInputTestId = useMemo(
() => INPUT_TEST_IDS.networkIPAddress(inputTestPrefix),
[inputTestPrefix],
);
const subnetMaskInputTestId = useMemo(
() => INPUT_TEST_IDS.networkSubnet(inputTestPrefix),
[inputTestPrefix],
);
const isNetworkOptional = networkIndex < optionalNetworkInputsLength; const isNetworkOptional = useMemo(
() => networkIndex < optionalNetworkInputsLength,
[networkIndex, optionalNetworkInputsLength],
);
networkInput.ipAddressInputRef = ipAddressInputRef; networkInput.ipAddressInputRef = ipAddressInputRef;
networkInput.subnetMaskInputRef = subnetMaskInputRef; networkInput.subnetMaskInputRef = subnetMaskInputRef;
@ -417,10 +442,19 @@ const NetworkForm: FC<{
inputLabelProps={{ isNotifyRequired: true }} inputLabelProps={{ isNotifyRequired: true }}
label="IP address" label="IP address"
onChange={({ target: { value } }) => { onChange={({ target: { value } }) => {
testInput({ const isLocalValid = testInput({
inputs: { [`${inputTestPrefix}IPAddress`]: { value } }, inputs: {
[ipAddressInputTestId]: {
value,
},
},
tests: inputTests, tests: inputTests,
}); });
toggleSubmitDisabled?.call(
null,
isLocalValid && testAllInputs(ipAddressInputTestId),
);
}} }}
value={ipAddress} value={ipAddress}
/> />
@ -434,10 +468,17 @@ const NetworkForm: FC<{
inputLabelProps={{ isNotifyRequired: true }} inputLabelProps={{ isNotifyRequired: true }}
label="Subnet mask" label="Subnet mask"
onChange={({ target: { value } }) => { onChange={({ target: { value } }) => {
testInput({ const isLocalValid = testInput({
inputs: { [`${inputTestPrefix}SubnetMask`]: { value } }, inputs: {
[subnetMaskInputTestId]: { value },
},
tests: inputTests, tests: inputTests,
}); });
toggleSubmitDisabled?.call(
null,
isLocalValid && testAllInputs(subnetMaskInputTestId),
);
}} }}
value={subnetMask} value={subnetMask}
/> />
@ -449,10 +490,15 @@ const NetworkForm: FC<{
); );
}; };
NetworkForm.defaultProps = { createDropMouseUpHandler: undefined }; NetworkForm.defaultProps = {
createDropMouseUpHandler: undefined,
toggleSubmitDisabled: undefined,
};
const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>( const NetworkInitForm = forwardRef<
(networkInitFormProps, ref) => { NetworkInitFormForwardRefContent,
{ toggleSubmitDisabled?: (testResult: boolean) => void }
>(({ toggleSubmitDisabled }, ref) => {
const [dragMousePosition, setDragMousePosition] = useState<{ const [dragMousePosition, setDragMousePosition] = useState<{
x: number; x: number;
y: number; y: number;
@ -541,8 +587,9 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
const inputTests: InputTestBatches = useMemo(() => { const inputTests: InputTestBatches = useMemo(() => {
const tests: InputTestBatches = { const tests: InputTestBatches = {
domainNameServerCSV: { [INPUT_TEST_IDS.dnsCSV]: {
defaults: { defaults: {
getValue: () => dnsCSVInputRef.current.getValue?.call(null),
onSuccess: () => { onSuccess: () => {
setDomainNameServerCSVInputMessage(undefined); setDomainNameServerCSVInputMessage(undefined);
}, },
@ -552,7 +599,7 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
onFailure: () => { onFailure: () => {
setDomainNameServerCSVInputMessage({ setDomainNameServerCSVInputMessage({
children: children:
'Domain name servers should be a comma-separated list of IPv4 addresses.', 'Domain name servers should be a comma-separated list of IPv4 addresses without trailing comma(s).',
}); });
}, },
test: ({ value }) => REP_IPV4_CSV.test(value as string), test: ({ value }) => REP_IPV4_CSV.test(value as string),
@ -560,8 +607,9 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
{ test: testNotBlank }, { test: testNotBlank },
], ],
}, },
gateway: { [INPUT_TEST_IDS.gateway]: {
defaults: { defaults: {
getValue: () => gatewayInputRef.current.getValue?.call(null),
onSuccess: () => { onSuccess: () => {
setGatewayInputMessage(undefined); setGatewayInputMessage(undefined);
}, },
@ -580,17 +628,19 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
}, },
}; };
networkInputs.forEach(({ name }, networkIndex) => { networkInputs.forEach(({ ipAddress, name, subnetMask }, networkIndex) => {
const inputTestPrefix = `network${networkIndex}`; const inputTestPrefix = `network${networkIndex}`;
tests[`${inputTestPrefix}Name`] = { tests[INPUT_TEST_IDS.networkName(inputTestPrefix)] = {
defaults: { value: name },
tests: [{ test: testNotBlank }], tests: [{ test: testNotBlank }],
}; };
tests[`${inputTestPrefix}IPAddress`] = { tests[INPUT_TEST_IDS.networkIPAddress(inputTestPrefix)] = {
defaults: { defaults: {
onSuccess: () => { onSuccess: () => {
setNetworkIPAddressInputMessage(networkIndex, undefined); setNetworkIPAddressInputMessage(networkIndex, undefined);
}, },
value: ipAddress,
}, },
tests: [ tests: [
{ {
@ -604,11 +654,12 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
{ test: testNotBlank }, { test: testNotBlank },
], ],
}; };
tests[`${inputTestPrefix}SubnetMask`] = { tests[INPUT_TEST_IDS.networkSubnet(inputTestPrefix)] = {
defaults: { defaults: {
onSuccess: () => { onSuccess: () => {
setNetworkSubnetMaskInputMessage(networkIndex, undefined); setNetworkSubnetMaskInputMessage(networkIndex, undefined);
}, },
value: subnetMask,
}, },
tests: [ tests: [
{ {
@ -633,6 +684,15 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
setNetworkSubnetMaskInputMessage, setNetworkSubnetMaskInputMessage,
]); ]);
const testAllInputs = useCallback(
(...excludeTestIds: string[]) =>
testInput({
excludeTestIds,
isIgnoreOnCallbacks: true,
tests: inputTests,
}),
[inputTests],
);
const clearNetworkInterfaceHeld = useCallback(() => { const clearNetworkInterfaceHeld = useCallback(() => {
setNetworkInterfaceHeld(undefined); setNetworkInterfaceHeld(undefined);
}, []); }, []);
@ -791,8 +851,7 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
type, type,
networkIndex, networkIndex,
)}`, )}`,
subnetMask: subnetMask: subnetMaskInputRef?.current.getValue?.call(null) ?? '',
subnetMaskInputRef?.current.getValue?.call(null) ?? '',
type, type,
}), }),
), ),
@ -907,6 +966,8 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
optionalNetworkInputsLength, optionalNetworkInputsLength,
setNetworkInputs, setNetworkInputs,
setNetworkInterfaceInputMap, setNetworkInterfaceInputMap,
testAllInputs,
toggleSubmitDisabled,
}} }}
/> />
); );
@ -923,10 +984,15 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
id="network-init-gateway" id="network-init-gateway"
inputLabelProps={{ isNotifyRequired: true }} inputLabelProps={{ isNotifyRequired: true }}
onChange={({ target: { value } }) => { onChange={({ target: { value } }) => {
testInput({ const isLocalValid = testInput({
inputs: { gateway: { value } }, inputs: { [INPUT_TEST_IDS.gateway]: { value } },
tests: inputTests, tests: inputTests,
}); });
toggleSubmitDisabled?.call(
null,
isLocalValid && testAllInputs(INPUT_TEST_IDS.gateway),
);
}} }}
label="Gateway" label="Gateway"
/> />
@ -939,10 +1005,15 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
id="network-init-dns-csv" id="network-init-dns-csv"
inputLabelProps={{ isNotifyRequired: true }} inputLabelProps={{ isNotifyRequired: true }}
onChange={({ target: { value } }) => { onChange={({ target: { value } }) => {
testInput({ const isLocalValid = testInput({
inputs: { domainNameServerCSV: { value } }, inputs: { [INPUT_TEST_IDS.dnsCSV]: { value } },
tests: inputTests, tests: inputTests,
}); });
toggleSubmitDisabled?.call(
null,
isLocalValid && testAllInputs(INPUT_TEST_IDS.dnsCSV),
);
}} }}
label="Domain name server(s)" label="Domain name server(s)"
/> />
@ -960,9 +1031,9 @@ const NetworkInitForm = forwardRef<NetworkInitFormForwardRefContent>(
</MUIBox> </MUIBox>
</MUIBox> </MUIBox>
); );
}, });
);
NetworkInitForm.defaultProps = { toggleSubmitDisabled: undefined };
NetworkInitForm.displayName = 'NetworkInitForm'; NetworkInitForm.displayName = 'NetworkInitForm';
export type { export type {

@ -0,0 +1 @@
type ToggleSubmitDisabledFunction = (testResult: boolean) => void;
Loading…
Cancel
Save