Skip to content

Commit

Permalink
fix select when datatype is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed May 17, 2022
1 parent e835463 commit 7663558
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions packages/react-ui-bootstrap/components/form/inputs/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import { useIntlContext } from '@vulcanjs/react-i18n';
import { useVulcanComponents } from '@vulcanjs/react-ui';
import React from 'react';
import { FormControl } from 'react-bootstrap';
import { useIntlContext } from "@vulcanjs/react-i18n";
import { useVulcanComponents } from "@vulcanjs/react-ui";
import React from "react";
import { FormControl } from "react-bootstrap";

// copied from vulcan:forms/utils.js to avoid extra dependency
const getFieldType = datatype => datatype && datatype[0].type;
const getFieldType = (datatype) =>
datatype && datatype.length && datatype[0].type;

export const FormComponentSelect = ({ refFunction, inputProperties, itemProperties, datatype, options }) => {
const Components = useVulcanComponents()
const intl = useIntlContext()
export const FormComponentSelect = ({
refFunction,
inputProperties,
itemProperties,
datatype,
options,
}) => {
const Components = useVulcanComponents();
const intl = useIntlContext();
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
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,
};
let otherOptions = Array.isArray(options) && options.length ? options : [];
const allOptions = [noneOption, ...otherOptions];
const { options: deleteOptions, ...newInputProperties } = inputProperties;
return (
<Components.FormItem path={inputProperties.path} label={inputProperties.label} {...itemProperties}>
<Components.FormItem
path={inputProperties.path}
label={inputProperties.label}
{...itemProperties}
>
<FormControl as="select" {...newInputProperties} ref={refFunction}>
{allOptions.map(({ value, label, intlId, ...rest }) => (
<option key={value} value={value} {...rest}>
Expand Down

0 comments on commit 7663558

Please sign in to comment.