docbrown/app/javascript/organization/OrganizationPicker.jsx
2020-05-29 11:59:37 -04:00

54 lines
1.4 KiB
JavaScript

import { h } from 'preact';
import PropTypes from 'prop-types';
import { organizationPropType } from '../common-prop-types';
const orgOptions = (organizations, organizationId, emptyLabel) => {
const orgs = organizations.map((organization) => {
if (organizationId === organization.id) {
return (
<option value={organization.id} selected>
{organization.name}
</option>
);
}
return <option value={organization.id}>{organization.name}</option>;
});
const nullOrgOption =
organizationId === null ? (
<option value="" selected>
{emptyLabel}
</option>
) : (
<option value="">{emptyLabel}</option>
);
return [nullOrgOption, ...orgs];
};
export const OrganizationPicker = ({
name,
id,
className,
organizations,
organizationId,
onToggle,
emptyLabel
}) => (
<select name={name} id={id} className={className} onBlur={onToggle}>
{orgOptions(organizations, organizationId, emptyLabel)}
</select>
);
OrganizationPicker.defaultProps = {
emptyLabel: "None"
}
OrganizationPicker.propTypes = {
name: PropTypes.string.isRequired,
id: PropTypes.string.isRequired,
className: PropTypes.string.isRequired,
emptyLabel: PropTypes.string,
onToggle: PropTypes.func.isRequired,
organizationId: PropTypes.number.isRequired,
organizations: PropTypes.arrayOf(organizationPropType).isRequired,
};