Skip to content

Commit

Permalink
fix Select input for boostrap, expose reusable FormOption type
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed May 17, 2022
1 parent 7663558 commit 3986c6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
32 changes: 18 additions & 14 deletions packages/react-ui-bootstrap/components/form/inputs/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
import { useIntlContext } from "@vulcanjs/react-i18n";
import { useVulcanComponents } from "@vulcanjs/react-ui";
import React from "react";
import { FormInputProps, useVulcanComponents } from "@vulcanjs/react-ui";
import type { FormOption } from "@vulcanjs/react-ui";
import React, { ElementType } from "react";
import { FormControl } from "react-bootstrap";

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

export const FormComponentSelect = ({
path,
label,
refFunction,
inputProperties,
itemProperties,
datatype,
options,
}) => {
}: FormInputProps<HTMLSelectElement>) => {
const Components = useVulcanComponents();
const intl = useIntlContext();
const noneOption = {
const noneOption: FormOption<any> = {
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
value: datatype === String || 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];
// @ts-ignore
const { options: deleteOptions, ...newInputProperties } = inputProperties;
return (
<Components.FormItem
path={inputProperties.path}
path={path}
label={inputProperties.label}
name={inputProperties.name}
{...itemProperties}
>
<FormControl as="select" {...newInputProperties} ref={refFunction}>
/* @ts-ignpore */
<FormControl
// @ts-ignore
as={"select" as ElementType}
{...newInputProperties}
ref={refFunction}
>
{allOptions.map(({ value, label, intlId, ...rest }) => (
<option key={value} value={value} {...rest}>
{label}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-ui/components/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export * from "./core/FormContext";
export * from "./core/Form";
export * from "./core/FormContainer";

export type { FormInputProps } from "./typings";
export type { FormInputProps, FormOption } from "./typings";

export * from "./utils";
12 changes: 12 additions & 0 deletions packages/react-ui/components/form/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import type {
export interface FormComponentProps<TField = any>
extends Omit<FormField, "type"> {
document: any;
/**
* Legacy type was Array<{type: Number | String | Boolean} = the "type" of a SimpleSchema object
* Now it's just the type from the JSON schema:
*/
datatype: VulcanFieldType; // TODO: type of the field, replace this by a cleaner value like we do in graphql to get the field type
disabled: boolean;
errors: Array<any>;
Expand Down Expand Up @@ -52,6 +56,14 @@ export type FormOption<TField = any> = {
value: TField;
/** Can force a default value */
checked?: boolean;
/**
* Translated option
*/
intlId?: string;
/**
* Non clickable option (visual clue)
*/
disabled?: boolean;
};

/**
Expand Down

0 comments on commit 3986c6d

Please sign in to comment.