import React, { Fragment } from 'react';
import Dropdown from 'react-bootstrap/lib/Dropdown';
import MenuItem from 'react-bootstrap/lib/MenuItem';
import classnames from 'classnames';
import normalizeOptions, { Options } from './utils/normalizeOptions';
import styled from 'styled-components';
const DefaultRenderer: React.FC<{ caption: any }> = props => (
{props.caption}
);
const StyledDropdown = styled(Dropdown)`
&.dropdown-block {
display: block;
> .btn {
float: none;
text-align: left;
&.btn-block {
display: flex;
justify-content: space-between;
align-items: center;
}
}
.dropdown-menu {
min-width: 100%;
}
}
`;
export interface ShrinkSelectProps {
options: Options;
value: T;
defaultSelect?: T;
onChange?: (value: T) => void;
bsSize?: string;
bsStyle?: string;
block?: boolean;
disabled?: boolean;
className?: string;
renderer?: React.ComponentType;
/**
* Set to true to use a number as a `value`
*/
numericalValue?: boolean;
}
const ShrinkSelect = (
props: ShrinkSelectProps
): React.ReactElement> => {
const {
value,
defaultSelect = null,
onChange = () => {},
bsSize = undefined,
bsStyle = 'default',
block = false,
disabled,
className,
renderer: Renderer = DefaultRenderer,
numericalValue = false
} = props;
const options = normalizeOptions(props.options);
const title: React.ReactChild =
value in options ? (
) : defaultSelect !== null ? (
options[defaultSelect].caption
) : (
' '
);
const handleChange = (key: string) => {
if (typeof onChange === 'function') {
onChange((numericalValue ? parseFloat(key) : key) as T);
}
};
return (
{title}
{Object.keys(options).map(key => (
))}
);
};
export default ShrinkSelect;