1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-10-06 12:34:19 +02:00
foris-js/src/bootstrap/Select.js

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-08-23 15:20:22 +02:00
/*
* Copyright (C) 2019-2021 CZ.NIC z.s.p.o. (http://www.nic.cz/)
2019-08-23 15:20:22 +02:00
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
2019-08-27 16:09:18 +02:00
import React from "react";
import PropTypes from "prop-types";
2019-10-10 17:25:00 +02:00
import { useUID } from "react-uid";
2019-08-23 15:20:22 +02:00
Select.propTypes = {
/** Select field Label. */
label: PropTypes.string.isRequired,
2019-08-27 16:09:18 +02:00
/** Choices if form of {value : "Label",...}. */
2019-08-23 15:20:22 +02:00
choices: PropTypes.object.isRequired,
/** Current value. */
2020-08-18 15:39:00 +02:00
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
2019-08-23 15:20:22 +02:00
/** Help text message. */
helpText: PropTypes.string,
/** Turns on/off alphabetical ordering of the Select options. */
customOrder: PropTypes.bool,
2019-08-23 15:20:22 +02:00
};
export function Select({ label, choices, helpText, customOrder, ...props }) {
2019-08-23 15:20:22 +02:00
const uid = useUID();
const keys = Object.keys(choices);
if (!customOrder) {
keys.sort((a, b) => a - b || a.toString().localeCompare(b.toString()));
}
const options = keys.map((key) => (
<option key={key} value={key}>
{choices[key]}
</option>
));
2019-08-27 16:09:18 +02:00
return (
2020-01-07 13:27:49 +01:00
<div className="form-group">
2019-08-27 16:09:18 +02:00
<label htmlFor={uid}>{label}</label>
2020-08-18 15:39:00 +02:00
<select className="custom-select" id={uid} {...props}>
2019-08-27 16:09:18 +02:00
{options}
</select>
2020-08-18 15:39:00 +02:00
{helpText ? (
<small className="form-text text-muted">{helpText}</small>
) : null}
2019-08-27 16:09:18 +02:00
</div>
);
2019-08-23 15:20:22 +02:00
}