1
Fork 0
mirror of https://github.com/Steffo99/bluelib.git synced 2024-12-22 11:34:21 +00:00

Improve onSimpleChange for FormSelect and FormMultiselect

`Select` and `Multiselect` inputs were left untouched as they might be used differently
This commit is contained in:
Steffo 2021-10-10 18:09:40 +02:00
parent 2f6a65a498
commit bc64c160e6
Signed by: steffo
GPG key ID: 6965406171929D01
4 changed files with 58 additions and 9 deletions

View file

@ -32,4 +32,16 @@ export const FormMultiselect = props => (
) )
FormMultiselect.args = { FormMultiselect.args = {
label: "Favourite colors", label: "Favourite colors",
options: {
"Red": "COLOR_RED",
"Orange": "COLOR_ORANGE",
"Yellow": "COLOR_YELLOW",
"Green": "COLOR_GREEN",
"Cyan": "COLOR_CYAN",
"Blue": "COLOR_BLUE",
"Purple": "COLOR_PURPLE",
"White": "COLOR_WHITE",
"Black": "COLOR_BLACK",
"Grey": "COLOR_GREY",
}
} }

View file

@ -3,31 +3,47 @@ import * as ReactDOM from "react-dom"
import * as Types from "../../types" import * as Types from "../../types"
import {BaseElement} from "../BaseElement" import {BaseElement} from "../BaseElement"
import mergeClassNames from "classnames" import mergeClassNames from "classnames"
import {Select} from "../inputs/Select"
import {FormPair, FormPairProps} from "./FormPair"; import {FormPair, FormPairProps} from "./FormPair";
import {FormLabel, FormLabelProps} from "./FormLabel"; import {FormLabel, FormLabelProps} from "./FormLabel";
import {Multiselect, MultiselectProps} from "../inputs/Multiselect"; import {Multiselect, MultiselectProps} from "../inputs/Multiselect";
export interface FormMultiselectOptions {
[key: string]: any,
}
export interface FormMultiselectProps extends MultiselectProps { export interface FormMultiselectProps extends MultiselectProps {
label: string, label: string,
validity?: Types.Validity, validity?: Types.Validity,
options: FormMultiselectOptions,
pairProps?: FormPairProps, pairProps?: FormPairProps,
labelProps?: FormLabelProps, labelProps?: FormLabelProps,
} }
export function FormMultiselect({label, validity, pairProps, labelProps, ...props}: FormMultiselectProps): JSX.Element { export function FormMultiselect({label, validity, pairProps, labelProps, onSimpleChange, options, ...props}: FormMultiselectProps): JSX.Element {
const onSimpleChangeWrapped = React.useCallback(
values => {
onSimpleChange?.(values.map((val: string) => options[val]))
},
[onSimpleChange, options]
)
return ( return (
<FormPair <FormPair
label={<FormLabel {...labelProps}>{label}</FormLabel>} label={<FormLabel {...labelProps}>{label}</FormLabel>}
input={<Multiselect {...props}/>} input={
<Multiselect onSimpleChange={onSimpleChangeWrapped} {...props}>
{Object.keys(options).map(key => <Multiselect.Option value={key}/>)}
</Multiselect>
}
validity={validity} validity={validity}
{...pairProps} {...pairProps}
/> />
) )
} }
FormMultiselect.Option = Multiselect.Option

View file

@ -25,4 +25,9 @@ export const FormSelect = props => (
) )
FormSelect.args = { FormSelect.args = {
label: "Ready check", label: "Ready check",
options: {
"I'm ready!": "READY",
"Please wait...": "WAIT",
"I won't be there.": "NO",
},
} }

View file

@ -8,25 +8,41 @@ import {FormLabel, FormLabelProps} from "./FormLabel";
import {Select, SelectProps} from "../inputs/Select"; import {Select, SelectProps} from "../inputs/Select";
export interface FormSelectOptions {
[key: string]: any,
}
export interface FormSelectProps extends SelectProps { export interface FormSelectProps extends SelectProps {
label: string, label: string,
validity?: Types.Validity, validity?: Types.Validity,
options: FormSelectOptions,
pairProps?: FormPairProps, pairProps?: FormPairProps,
labelProps?: FormLabelProps, labelProps?: FormLabelProps,
} }
export function FormSelect({label, validity, pairProps, labelProps, ...props}: FormSelectProps): JSX.Element { export function FormSelect({label, validity, options, pairProps, labelProps, onSimpleChange, ...props}: FormSelectProps): JSX.Element {
const onSimpleChangeWrapped = React.useCallback(
value => {
onSimpleChange?.(options[value])
},
[onSimpleChange, options]
)
return ( return (
<FormPair <FormPair
label={<FormLabel {...labelProps}>{label}</FormLabel>} label={<FormLabel {...labelProps}>{label}</FormLabel>}
input={<Select {...props}/>} input={
<Select onSimpleChange={onSimpleChangeWrapped} {...props}>
{Object.keys(options).map(key => <Select.Option value={key}/>)}
</Select>
}
validity={validity} validity={validity}
{...pairProps} {...pairProps}
/> />
) )
} }
FormSelect.Option = Select.Option