-
Notifications
You must be signed in to change notification settings - Fork 90
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
de82254
commit 88a4d9f
Showing
8 changed files
with
210 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
useListControl, | ||
ListControlProps, | ||
} from "../list-control/ListControlState"; | ||
|
||
import { SyntheticEvent } from "react"; | ||
import { OptionValue } from "../list-control/ListControlContext"; | ||
|
||
export function useComboBoxNext(props: ListControlProps) { | ||
const { | ||
open, | ||
defaultOpen, | ||
onOpenChange, | ||
multiselect, | ||
defaultSelected, | ||
selected, | ||
onSelectionChange, | ||
defaultValue, | ||
value, | ||
} = props; | ||
|
||
const listControl = useListControl({ | ||
open, | ||
defaultOpen, | ||
onOpenChange, | ||
multiselect, | ||
defaultSelected, | ||
selected, | ||
onSelectionChange, | ||
defaultValue, | ||
value, | ||
}); | ||
|
||
const { selectedState, getOptionsMatching, setValueState, setSelectedState } = | ||
listControl; | ||
|
||
const select = (event: SyntheticEvent, option: OptionValue) => { | ||
const { disabled, value } = option; | ||
|
||
if (disabled) { | ||
return; | ||
} | ||
|
||
let newSelected = [value]; | ||
|
||
if (multiselect) { | ||
if (selectedState.includes(value)) { | ||
newSelected = selectedState.filter((item) => item !== value); | ||
} else { | ||
newSelected = selectedState.concat([value]); | ||
} | ||
} | ||
|
||
setSelectedState(newSelected); | ||
const newValue = getOptionsMatching((option) => | ||
newSelected.includes(option.value) | ||
).map((option) => option.text); | ||
setValueState(multiselect ? "" : newValue[0]); | ||
onSelectionChange?.(event, newSelected); | ||
}; | ||
|
||
return { ...listControl, select }; | ||
} |
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,52 @@ | ||
[ | ||
"Alabama", | ||
"Alaska", | ||
"Arizona", | ||
"Arkansas", | ||
"California", | ||
"Colorado", | ||
"Connecticut", | ||
"Delaware", | ||
"Florida", | ||
"Georgia", | ||
"Hawaii", | ||
"Idaho", | ||
"Illinois", | ||
"Indiana", | ||
"Iowa", | ||
"Kansas", | ||
"Kentucky", | ||
"Louisiana", | ||
"Maine", | ||
"Maryland", | ||
"Massachusetts", | ||
"Michigan", | ||
"Minnesota", | ||
"Mississippi", | ||
"Missouri", | ||
"Montana", | ||
"Nebraska", | ||
"Nevada", | ||
"New Hampshire", | ||
"New Jersey", | ||
"New Mexico", | ||
"New York", | ||
"North Carolina", | ||
"North Dakota", | ||
"Ohio", | ||
"Oklahoma", | ||
"Oregon", | ||
"Pennsylvania", | ||
"Rhode Island", | ||
"South Carolina", | ||
"South Dakota", | ||
"Tennessee", | ||
"Texas", | ||
"Utah", | ||
"Vermont", | ||
"Virginia", | ||
"Washington", | ||
"West Virginia", | ||
"Wisconsin", | ||
"Wyoming" | ||
] |
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,66 @@ | ||
import { ChangeEvent, ReactElement, SyntheticEvent, useState } from "react"; | ||
import { ComboBoxNext, Option } from "@salt-ds/lab"; | ||
import useSWR from "swr"; | ||
import { Spinner } from "@salt-ds/core"; | ||
|
||
const fetcher = async (url: string, filter: string) => { | ||
// Sleep for 1 second to highlight to loading state | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
const rawData = await fetch(url); | ||
const data = (await rawData.json()) as string[]; | ||
return data.filter((state) => | ||
state.toLowerCase().includes(filter.trim().toLowerCase()) | ||
); | ||
}; | ||
|
||
export const ServerSideData = (): ReactElement => { | ||
const [value, setValue] = useState(""); | ||
const { data, isLoading } = useSWR<string[]>( | ||
`/example-data/states.json?s=${value}`, | ||
(url: string) => fetcher(url, value), | ||
{ | ||
fallbackData: [], | ||
} | ||
); | ||
|
||
const loading = isLoading; | ||
|
||
const handleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
const value = event.target.value; | ||
setValue(value); | ||
}; | ||
|
||
const handleSelectionChange = ( | ||
event: SyntheticEvent, | ||
newSelected: string[] | ||
) => { | ||
if (newSelected.length === 1) { | ||
setValue(newSelected[0]); | ||
} else { | ||
setValue(""); | ||
} | ||
}; | ||
|
||
return ( | ||
<ComboBoxNext | ||
onChange={handleChange} | ||
onSelectionChange={handleSelectionChange} | ||
value={value} | ||
style={{ width: "266px" }} | ||
endAdornment={loading && <Spinner size="small" />} | ||
> | ||
{!loading ? ( | ||
data?.map((color) => ( | ||
<Option value={color} key={color}> | ||
{color} | ||
</Option> | ||
)) | ||
) : ( | ||
<Option value="loading" key="loading"> | ||
Loading | ||
</Option> | ||
)} | ||
</ComboBoxNext> | ||
); | ||
}; |
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