ghost-portal/packages/react-params/src/Param/Vote.tsx
2020-01-06 16:16:50 +01:00

54 lines
1.3 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-components authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Props } from '../types';
import BN from 'bn.js';
import React from 'react';
import { GenericVote } from '@polkadot/types';
import { Dropdown } from '@polkadot/react-components';
import Bare from './Bare';
const options = [
{ text: 'Nay', value: 0 },
{ text: 'Aye', value: -1 }
];
function onChange ({ onChange }: Props): (_: number) => void {
return function (value: number): void {
onChange && onChange({
isValid: true,
value
});
};
}
export default function Vote (props: Props): React.ReactElement<Props> {
const { className, defaultValue: { value }, isDisabled, isError, label, style, withLabel } = props;
const defaultValue = value instanceof BN
? value.toNumber()
: value instanceof GenericVote
? (value.isAye ? -1 : 0)
: value as number;
return (
<Bare
className={className}
style={style}
>
<Dropdown
className='full'
defaultValue={defaultValue}
isDisabled={isDisabled}
isError={isError}
label={label}
options={options}
onChange={onChange(props)}
withLabel={withLabel}
/>
</Bare>
);
}