Skip to content

Commit

Permalink
feat/SOF-6914 (#33)
Browse files Browse the repository at this point in the history
* feat: custom select and text field
* feat: add required prop to BasicTextField
* chore: cleanup
  • Loading branch information
seankwarren authored Oct 4, 2023
1 parent 259ba33 commit d436841
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/mui/components/select/BasicSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import MenuItem from "@mui/material/MenuItem";
import TextField from "@mui/material/TextField";
import React from "react";
import s from "underscore.string";

export type AvailableValue = {
id: string | undefined;
name: string;
};

type BasicSelectProps = {
disabled?: boolean;
selectedValue: string;
options: AvailableValue[];
onChange: (newValue: string) => void;
label: string;
labelAsPlaceholder?: boolean;
renderMenuItemContent?: (value: AvailableValue) => JSX.Element;
};

export default function BasicSelect({
disabled = false,
selectedValue,
options,
onChange = () => {}, // eslint-disable-line @typescript-eslint/no-empty-function
label,
labelAsPlaceholder = true,
renderMenuItemContent,
}: BasicSelectProps) {
const inputLabelProps = labelAsPlaceholder ? {} : { shrink: true };

const optionsList = options
? options.map((availableValue) => {
const menuItemContent = renderMenuItemContent
? renderMenuItemContent(availableValue)
: availableValue.name;

return (
<MenuItem key={availableValue.id} value={availableValue.id}>
{menuItemContent}
</MenuItem>
);
})
: [];

return (
<TextField
disabled={disabled}
label={s.capitalize(label)}
value={selectedValue}
onChange={(e) => onChange(e.target.value)}
variant="outlined"
fullWidth
select
size="small"
InputLabelProps={inputLabelProps}>
{optionsList}
</TextField>
);
}
3 changes: 3 additions & 0 deletions src/mui/components/select/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import BasicSelect from "./BasicSelect";

export { default } from "./Select";
export * from "./Select";
export { BasicSelect };
54 changes: 54 additions & 0 deletions src/mui/components/textField/BasicTextField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { SxProps } from "@mui/material";
import TextField, { TextFieldProps } from "@mui/material/TextField";
import React, { HTMLInputTypeAttribute } from "react";

type BasicTextFieldProps = {
label: string;
value?: string | number;
defaultValue?: string | number;
onChange?: (newValue: string | number) => void;
disabled?: boolean;
fullWidth?: boolean;
size?: "small" | "medium";
labelAsPlaceholder?: boolean;
sx?: SxProps;
type?: HTMLInputTypeAttribute;
variant?: "standard" | "filled" | "outlined";
required?: boolean;
};

function BasicTextField({
label,
value,
defaultValue,
onChange = () => {}, // eslint-disable-line @typescript-eslint/no-empty-function
disabled = false,
fullWidth = true,
size = "medium",
labelAsPlaceholder = true,
sx = {},
type = "text",
variant = "outlined",
required = false,
}: BasicTextFieldProps) {
const inputLabelProps = labelAsPlaceholder ? {} : { shrink: true };

return (
<TextField
type={type}
sx={sx}
label={label}
value={value}
defaultValue={defaultValue}
fullWidth={fullWidth}
disabled={disabled}
variant={variant}
size={size}
required={required}
InputLabelProps={inputLabelProps}
onChange={(e) => onChange(e.target.value)}
/>
);
}

export default BasicTextField;
3 changes: 3 additions & 0 deletions src/mui/components/textField/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import BasicTextField from "./BasicTextField";

export { BasicTextField };

0 comments on commit d436841

Please sign in to comment.