21224: added chevron, moved description section Arvados-DCO-1.1-Signed-off-by: Lisa...
[arvados.git] / services / workbench2 / 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 React from 'react';
6 import { memoize } from 'lodash/fp';
7 import { BooleanCommandInputParameter } from 'models/workflow';
8 import { Field } from 'redux-form';
9 import { Switch } from '@material-ui/core';
10 import { GenericInputProps, GenericInput } from './generic-input';
11
12 export interface BooleanInputProps {
13     input: BooleanCommandInputParameter;
14 }
15 export const BooleanInput = ({ input }: BooleanInputProps) =>
16     <Field
17         name={input.id}
18         commandInput={input}
19         component={BooleanInputComponent}
20         normalize={normalize}
21     />;
22
23 const normalize = (_: any, prevValue: boolean) => !prevValue;
24
25 const BooleanInputComponent = (props: GenericInputProps) =>
26     <GenericInput
27         component={Input}
28         {...props} />;
29
30 const Input = ({ input, commandInput }: GenericInputProps) =>
31     <Switch
32         color='primary'
33         checked={input.value}
34         onChange={handleChange(input.onChange, input.value)}
35         disabled={commandInput.disabled} />;
36
37 const handleChange = memoize(
38     (onChange: (value: string) => void, value: string) => () => onChange(value)
39 );