Extract BooleanInput normalize function
[arvados-workbench2.git] / src / views / run-process-panel / inputs / boolean-input.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { BooleanCommandInputParameter } from '~/models/workflow';
7 import { Field } from 'redux-form';
8 import { Switch } from '@material-ui/core';
9 import { GenericInputProps, GenericInput } from './generic-input';
10
11 export interface BooleanInputProps {
12     input: BooleanCommandInputParameter;
13 }
14 export const BooleanInput = ({ input }: BooleanInputProps) =>
15     <Field
16         name={input.id}
17         commandInput={input}
18         component={BooleanInputComponent}
19         normalize={normalize}
20     />;
21
22 const normalize = (_: any, prevValue: boolean) => !prevValue;
23
24 const BooleanInputComponent = (props: GenericInputProps) =>
25     <GenericInput
26         component={Input}
27         {...props} />;
28
29 const Input = (props: GenericInputProps) =>
30     <Switch
31         color='primary'
32         checked={props.input.value}
33         onChange={() => props.input.onChange(props.input.value)} 
34         disabled={props.commandInput.disabled} />;