docbrown/app/javascript/organization/OrganizationPicker.jsx
dependabot[bot] 62e150f5af
Bump eslint-config-preact from 1.1.3 to 1.1.4 (#13516)
* Bump eslint-config-preact from 1.1.3 to 1.1.4

Bumps [eslint-config-preact](https://github.com/preactjs/eslint-config-preact) from 1.1.3 to 1.1.4.
- [Release notes](https://github.com/preactjs/eslint-config-preact/releases)
- [Commits](https://github.com/preactjs/eslint-config-preact/compare/v1.1.3...v1.1.4)

Signed-off-by: dependabot[bot] <support@github.com>

* Add missing key prop for elements in iterators where necessary

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>
2021-04-27 18:54:43 +02:00

64 lines
1.5 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 key={organization.id} 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
aria-label="Select an organization"
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,
};