Skip to content

Commit

Permalink
fix: only expose focus on Select
Browse files Browse the repository at this point in the history
  • Loading branch information
shuowu-okta committed Dec 8, 2023
1 parent 13aa484 commit c519ee9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 41 deletions.
11 changes: 9 additions & 2 deletions packages/odyssey-react-mui/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export type SelectProps<
/**
* The ref is forwarded to input element in the Select
*/
inputRef: React.RefObject<HTMLInputElement>;
inputRef: React.RefObject<{
focus: () => void;
} | null>;
} & Pick<
FieldComponentProps,
| "errorMessage"
Expand Down Expand Up @@ -178,7 +180,12 @@ const Select = <
const inputElement = (
ref.current as unknown as HTMLElement
).querySelector("input");
return inputElement as HTMLInputElement;
if (!inputElement) {
return null;
}
return {
focus: inputElement.focus,
};
},
[]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

import { useRef } from "react";
import { Box } from "@mui/material";
import { Meta, StoryObj } from "@storybook/react";
import { Select, SelectProps } from "@okta/odyssey-react-mui";
import { MuiThemeDecorator } from "../../../../.storybook/components";
Expand Down Expand Up @@ -218,15 +216,6 @@ const storybookMeta: Meta<SelectProps<string | string[], boolean>> = {
},
},
},
inputRef: {
control: null,
description: "The ref is forwarded to the root element.",
table: {
type: {
summary: "HTMLElement",
},
},
},
},
args: {
hint: "Select your destination in the Sol system.",
Expand Down Expand Up @@ -406,31 +395,3 @@ export const ControlledPreselectedMultipleSelect: StoryObj<typeof Select> = {
return <Select {...props} value={localValue} onChange={onChange} />;
},
};

export const WithRef: StoryObj<typeof Select> = {
args: {
value: [],
hasMultipleChoices: true,
},
render: function C(args) {
const [refHtml, setRefHtml] = useState("");
const ref = useRef<HTMLInputElement>(null);

const handleGetRefInnerHtml = () => {
setRefHtml(ref.current?.outerHTML as string);
};

return (
<Box
component="div"
sx={{ display: "flex", flexFlow: "column", gap: "1rem" }}
>
<Select {...args} inputRef={ref} />
<Box>
<button onClick={handleGetRefInnerHtml}>Get ref Html</button>
</Box>
<div>Ref HTML: {refHtml}</div>
</Box>
);
},
};

0 comments on commit c519ee9

Please sign in to comment.