Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement - Options to allow key value array #762

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/Select/Select.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MenuItem, TextField } from "@mui/material";

import PropTypes from "prop-types";
import React from "react";

Expand All @@ -15,9 +16,14 @@ export default function Select({
options = [],
required = false,
size = "medium",
value,
value = "",
variant = "outlined"
}) {
// set value for select, when options is a key value pair the value will be in option.value otherwise option is the value
const setValue = options.some(
option => option.value === value || option === value
);

// return components
return (
<TextField
Expand All @@ -28,18 +34,23 @@ export default function Select({
margin={margin}
fullWidth
label={label}
value={options.includes(value) ? value : ""}
value={setValue ? value : ""}
onChange={onChange}
disabled={disabled}
id={label}
helperText={helperText}
size={size}
>
{options.map(option => (
<MenuItem value={option} key={option}>
{option}
</MenuItem>
))}
{/* Render the options */}
{options.map(option => {
const value = option.value || option;
const key = option.key || option;
return (
<MenuItem value={value} key={key}>
{value}
</MenuItem>
);
})}
</TextField>
);
}
Expand Down Expand Up @@ -76,10 +87,17 @@ Select.propTypes = {
*/
onChange: PropTypes.func,
/**
* Array of options to display.
* Array of options to display. Each option can be a string, number, or an object with `key` and `value` properties.
*/
options: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.shape({
key: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
})
])
),
/**
* If true, the label will indicate that the input is required.
Expand Down
19 changes: 19 additions & 0 deletions src/Select/Select.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,22 @@ export const Default = {

render: Template
};

export const KeyValueOptions = {
args: {
disabled: false,
error: false,
helperText: "",
label: "Select an option",
margin: "normal",
options: [
{ key: "option-a", value: "Option A" },
{ key: "option-b", value: "Option B" }
],
required: true,
size: "medium",
variant: "outlined"
},

render: Template
};