Vulcan/packages/vulcan-ui-bootstrap/lib/components/forms/Select.jsx

36 lines
1.3 KiB
React
Raw Normal View History

import React from 'react';
import { intlShape } from 'meteor/vulcan:i18n';
2019-01-26 21:21:09 +09:00
import Form from 'react-bootstrap/lib/Form';
2019-01-24 18:01:53 +09:00
import { Components, registerComponent } from 'meteor/vulcan:core';
// copied from vulcan:forms/utils.js to avoid extra dependency
const getFieldType = datatype => datatype && datatype[0].type;
2019-01-24 18:01:53 +09:00
const SelectComponent = ({ refFunction, inputProperties, datatype, ...properties }, { intl }) => {
const noneOption = {
label: intl.formatMessage({ id: 'forms.select_option' }),
value: getFieldType(datatype) === String || getFieldType(datatype) === Number ? '' : null, // depending on field type, empty value can be '' or null
disabled: true,
};
2019-01-24 18:01:53 +09:00
let otherOptions =
Array.isArray(inputProperties.options) && inputProperties.options.length
? inputProperties.options
: [];
const options = [noneOption, ...otherOptions];
2019-01-24 18:01:53 +09:00
return (
<Components.FormItem {...inputProperties}>
<Form.Control as="select" {...inputProperties} ref={refFunction}>
{options.map((option, i) => (
<option key={i} {...option}>{option.value}</option>
))}
</Form.Control>
</Components.FormItem>
);
};
SelectComponent.contextTypes = {
intl: intlShape,
};
registerComponent('FormComponentSelect', SelectComponent);