mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-26 17:16:11 -05:00
Merge pull request #3121 from benjiwheeler/formik-select
introduce formik select component
This commit is contained in:
commit
92430584dc
1 changed files with 62 additions and 0 deletions
62
src/components/formik-forms/formik-select.jsx
Normal file
62
src/components/formik-forms/formik-select.jsx
Normal file
|
@ -0,0 +1,62 @@
|
|||
const classNames = require('classnames');
|
||||
const PropTypes = require('prop-types');
|
||||
const React = require('react');
|
||||
import {Field} from 'formik';
|
||||
|
||||
const ValidationMessage = require('../forms/validation-message.jsx');
|
||||
|
||||
require('../forms/select.scss');
|
||||
require('../forms/row.scss');
|
||||
|
||||
const FormikSelect = ({
|
||||
className,
|
||||
error,
|
||||
options,
|
||||
validationClassName,
|
||||
...props
|
||||
}) => {
|
||||
const optionsList = options.map((item, index) => (
|
||||
<option
|
||||
key={index}
|
||||
value={item.value}
|
||||
>
|
||||
{item.label}
|
||||
</option>
|
||||
));
|
||||
return (
|
||||
<div className="col-sm-9 row">
|
||||
<Field
|
||||
className={classNames(
|
||||
'select',
|
||||
className
|
||||
)}
|
||||
component="select"
|
||||
{...props}
|
||||
>
|
||||
{optionsList}
|
||||
</Field>
|
||||
{error && (
|
||||
<ValidationMessage
|
||||
className={validationClassName}
|
||||
message={error}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
FormikSelect.propTypes = {
|
||||
className: PropTypes.string,
|
||||
error: PropTypes.string,
|
||||
options: PropTypes.arrayOf(PropTypes.shape({
|
||||
disabled: PropTypes.bool,
|
||||
label: PropTypes.string.isRequired,
|
||||
value: PropTypes.string.isRequired
|
||||
})).isRequired,
|
||||
validationClassName: PropTypes.string,
|
||||
// selected value
|
||||
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
|
||||
};
|
||||
|
||||
module.exports = FormikSelect;
|
Loading…
Reference in a new issue