generated from abelflopes/lerna-monorepo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75c4350
commit bdfc124
Showing
10 changed files
with
305 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
type MockFetch<T extends object> = () => Promise<{ json: () => T }>; | ||
|
||
function mockResizeObserverImplementation<T extends object>(data: T | Error): MockFetch<T> { | ||
return jest.fn().mockImplementation( | ||
async (): ReturnType<MockFetch<T>> => | ||
data instanceof Error | ||
? Promise.reject(data) | ||
: Promise.resolve({ | ||
json: () => data, | ||
}), | ||
); | ||
} | ||
|
||
export const mockResizeObserver = (): void => { | ||
Object.defineProperty(window, "ResizeObserver", { | ||
writable: true, | ||
value: class { | ||
public observe = (): void => undefined; | ||
public disconnect = (): void => undefined; | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
import React from "react"; | ||
import { Menu } from "@react-ck/provisional"; | ||
import React, { useContext, useMemo } from "react"; | ||
import { SelectContext } from "./context"; | ||
import { componentToText } from "@react-ck/react-utils"; | ||
import { type SelectOptionProps } from "./types"; | ||
|
||
export type SelectOptionProps = React.OptionHTMLAttributes<HTMLOptionElement>; | ||
export const SelectOption = ({ | ||
value, | ||
disabled, | ||
children, | ||
...props | ||
}: Readonly<SelectOptionProps>): React.ReactElement => { | ||
const context = useContext(SelectContext); | ||
|
||
export const SelectOption = ({ ...props }: Readonly<SelectOptionProps>): React.ReactElement => ( | ||
<option {...props} /> | ||
); | ||
const childrenText = useMemo(() => componentToText(children), [children]); | ||
|
||
const computedValue = useMemo(() => { | ||
const v = value ?? childrenText; | ||
|
||
if (!v || v.length === 0) throw new Error("Select option has no value"); | ||
|
||
return v; | ||
}, [childrenText, value]); | ||
|
||
const displayChildren = useMemo(() => children ?? value, [children, value]); | ||
|
||
const isCurrentlySelected = context?.selectedValues.includes(computedValue); | ||
|
||
return ( | ||
<Menu.Item | ||
{...props} | ||
disabled={disabled} | ||
skin={isCurrentlySelected ? "primary" : "default"} | ||
onClick={() => { | ||
if (disabled) return; | ||
|
||
context?.handleChange(computedValue, isCurrentlySelected ? "deselect" : "select"); | ||
}}> | ||
{displayChildren} | ||
</Menu.Item> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createContext } from "react"; | ||
import { type ChangeHandler, type SelectedValues } from "./types"; | ||
|
||
export interface SelectContextProps { | ||
selectedValues: SelectedValues; | ||
handleChange: ChangeHandler; | ||
} | ||
|
||
export const SelectContext = createContext<SelectContextProps | undefined>(undefined); |
Oops, something went wrong.