-
Notifications
You must be signed in to change notification settings - Fork 226
Extract model output dropdown to its own component #2839
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import * as React from "react"; | ||
| import { ChangeEvent, useCallback, useMemo } from "react"; | ||
| import { Dropdown } from "../common/Dropdown"; | ||
| import { ModeledMethod } from "../../model-editor/modeled-method"; | ||
| import { Method, getArgumentsList } from "../../model-editor/method"; | ||
|
|
||
| type Props = { | ||
| method: Method; | ||
| modeledMethod: ModeledMethod | undefined; | ||
| onChange: (method: Method, modeledMethod: ModeledMethod) => void; | ||
| }; | ||
|
|
||
| export const ModelOutputDropdown = ({ | ||
| method, | ||
| modeledMethod, | ||
| onChange, | ||
| }: Props): JSX.Element => { | ||
| const argumentsList = useMemo( | ||
| () => getArgumentsList(method.methodParameters), | ||
| [method.methodParameters], | ||
| ); | ||
|
|
||
| const options = useMemo( | ||
| () => [ | ||
| { value: "ReturnValue", label: "ReturnValue" }, | ||
| { value: "Argument[this]", label: "Argument[this]" }, | ||
| ...argumentsList.map((argument, index) => ({ | ||
| value: `Argument[${index}]`, | ||
| label: `Argument[${index}]: ${argument}`, | ||
| })), | ||
| ], | ||
| [argumentsList], | ||
| ); | ||
|
|
||
| const enabled = useMemo( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is a primitive type and the value is not hard to compute, I don't believe we gain anything by using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm following the same pattern as in #2837 so I'd rather keep it consistent. Happy to stick to |
||
| () => | ||
| modeledMethod?.type && | ||
| ["source", "summary"].includes(modeledMethod?.type), | ||
| [modeledMethod?.type], | ||
| ); | ||
|
|
||
| const handleChange = useCallback( | ||
| (e: ChangeEvent<HTMLSelectElement>) => { | ||
| if (!modeledMethod) { | ||
| return; | ||
| } | ||
|
|
||
| const target = e.target as HTMLSelectElement; | ||
|
|
||
| onChange(method, { | ||
| ...modeledMethod, | ||
| output: target.value, | ||
| }); | ||
| }, | ||
| [onChange, method, modeledMethod], | ||
| ); | ||
|
|
||
| return ( | ||
| <Dropdown | ||
| value={modeledMethod?.output} | ||
| options={options} | ||
| disabled={!enabled} | ||
| onChange={handleChange} | ||
| aria-label="Output" | ||
| /> | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
argumentListis only used to computeoptions, so we could merge these together and callgeArgumentListon line 27 and it wouldn't change any behaviour. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good point. I could do the same in
ModelInputDropdown.If we keep things as is we would be consistent with
ModelInputDropdownandModelTypeDropdownthough.I don't have strong opinions.