saleor-dashboard/src/components/SingleSelectField/SingleSelectField.tsx

127 lines
3.1 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import FormControl from "@material-ui/core/FormControl";
import FormHelperText from "@material-ui/core/FormHelperText";
import { InputProps } from "@material-ui/core/Input";
2019-06-19 14:40:52 +00:00
import InputLabel from "@material-ui/core/InputLabel";
import MenuItem from "@material-ui/core/MenuItem";
import OutlinedInput from "@material-ui/core/OutlinedInput";
2019-06-19 14:40:52 +00:00
import Select, { SelectProps } from "@material-ui/core/Select";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import classNames from "classnames";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage } from "react-intl";
2019-06-19 14:40:52 +00:00
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
formControl: {
"& label": {
top: "-3px"
},
width: "100%"
2019-09-11 09:47:39 +00:00
},
2019-12-03 15:28:40 +00:00
noLabel: {
padding: theme.spacing(2, 1.5)
}
}),
{ name: "SingleSelectField" }
);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface SingleSelectFieldProps {
2019-06-19 14:40:52 +00:00
choices: Array<{
value: string;
label: string | React.ReactNode;
}>;
className?: string;
disabled?: boolean;
error?: boolean;
hint?: string;
label?: string;
name?: string;
selectProps?: SelectProps;
placeholder?: string;
value?: string;
2019-12-17 14:20:54 +00:00
InputProps?: InputProps;
2019-06-19 14:40:52 +00:00
onChange(event: any);
}
2019-10-30 14:34:24 +00:00
export const SingleSelectField: React.FC<SingleSelectFieldProps> = props => {
const {
2019-06-19 14:40:52 +00:00
className,
disabled,
error,
label,
choices,
value,
onChange,
name,
hint,
selectProps,
2019-12-17 14:20:54 +00:00
placeholder,
InputProps
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
const choicesByKey: { [key: string]: string } =
choices === undefined
? {}
: choices.reduce((prev, curr) => {
prev[curr.value] = curr.label;
return prev;
}, {});
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
return (
<FormControl
className={classNames(classes.formControl, className)}
error={error}
disabled={disabled}
>
<InputLabel shrink={!!value}>{label}</InputLabel>
<Select
variant="outlined"
fullWidth
renderValue={choiceValue =>
choiceValue ? choicesByKey[choiceValue.toString()] : placeholder
}
value={value || ""}
onChange={onChange}
2019-11-06 15:48:20 +00:00
input={
<OutlinedInput
classes={{
input: classNames({
[classes.noLabel]: !label
})
}}
name={name}
labelWidth={180}
2019-12-17 14:20:54 +00:00
{...InputProps}
2019-11-06 15:48:20 +00:00
/>
}
2019-10-30 14:34:24 +00:00
{...selectProps}
2019-06-19 14:40:52 +00:00
>
2019-10-30 14:34:24 +00:00
{choices.length > 0 ? (
choices.map(choice => (
2020-08-21 11:03:14 +00:00
<MenuItem
2020-08-24 14:23:45 +00:00
data-test="selectFieldOption"
2020-08-21 11:03:14 +00:00
data-test-id={choice.value}
value={choice.value}
key={choice.value}
>
2019-10-30 14:34:24 +00:00
{choice.label}
</MenuItem>
2019-10-30 14:34:24 +00:00
))
) : (
2020-08-21 11:03:14 +00:00
<MenuItem
2020-08-24 14:23:45 +00:00
data-test="selectFieldOption"
2020-08-21 11:03:14 +00:00
data-test-disabled
disabled={true}
>
2019-10-30 14:34:24 +00:00
<FormattedMessage defaultMessage="No results found" />
</MenuItem>
)}
</Select>
{hint && <FormHelperText>{hint}</FormHelperText>}
</FormControl>
);
};
2019-06-19 14:40:52 +00:00
SingleSelectField.displayName = "SingleSelectField";
export default SingleSelectField;