Toggle.tsx 714 Bytes
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1
import * as React from 'react';
Viacheslav Pavlov's avatar
i18n  
Viacheslav Pavlov committed
2
import {translate} from 'react-i18next';
Maxym Borodenko's avatar
Maxym Borodenko committed
3

Matija Obreza's avatar
Matija Obreza committed
4 5
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Switch from '@material-ui/core/Switch';
Maxym Borodenko's avatar
Maxym Borodenko committed
6 7 8

//
// Render a switch form control (a yes/no toggler)
Viacheslav Pavlov's avatar
i18n  
Viacheslav Pavlov committed
9
const renderToggler = ({input, falseIsNull = false, label, meta, t, ...rest}: { falseIsNull?: boolean } & any) => {
Maxym Borodenko's avatar
Maxym Borodenko committed
10 11 12 13 14 15 16 17

    const onChange = (event, checked) => {
      input.onChange(checked ? checked : (falseIsNull && ! checked ? null : checked));
    };

    return (
        <FormControlLabel
            control={ <Switch checked={ input.value } onChange={ onChange } /> }
Viacheslav Pavlov's avatar
i18n  
Viacheslav Pavlov committed
18
            label={ t(label) }
Maxym Borodenko's avatar
Maxym Borodenko committed
19 20 21 22
        />
    );
};

Viacheslav Pavlov's avatar
i18n  
Viacheslav Pavlov committed
23
export default translate()(renderToggler);