-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from grucloud/select-autocomplete-default-option
Select autocomplete default option
- Loading branch information
Showing
35 changed files
with
1,361 additions
and
461 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
42 changes: 42 additions & 0 deletions
42
bau-ui/examples/bau-storybook/src/pages/autocomplete/autocomplete-default-option.ts
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,42 @@ | ||
import { Context } from "@grucloud/bau-ui/context"; | ||
import autocomplete from "@grucloud/bau-ui/autocomplete"; | ||
|
||
export default (context: Context) => { | ||
const { bau, css } = context; | ||
const { section, div, span } = bau.tags; | ||
|
||
const Autocomplete = autocomplete(context); | ||
|
||
const defaultCode = "AD"; | ||
|
||
const options = [ | ||
{ code: "AD", label: "Andorra", phone: "376" }, | ||
{ code: "AF", label: "Afghanistan", phone: "93" }, | ||
]; | ||
|
||
const Option = (option: any) => | ||
div( | ||
{ | ||
class: css` | ||
display: flex; | ||
justify-content: space-between; | ||
gap: 0.5rem; | ||
`, | ||
}, | ||
span(option.label), | ||
span(option.code) | ||
); | ||
return () => | ||
section( | ||
Autocomplete({ | ||
options, | ||
Option, | ||
defaultOption: options.find(({ code }) => code == defaultCode), | ||
getOptionValue: ({ code }: any) => code, | ||
getOptionLabel: ({ label }: any) => label, | ||
label: "Country", | ||
placeholder: "Search countries", | ||
id: "country", | ||
}) | ||
); | ||
}; |
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
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
46 changes: 46 additions & 0 deletions
46
bau-ui/examples/bau-storybook/src/pages/select/select-default-option.ts
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,46 @@ | ||
import select from "@grucloud/bau-ui/select"; | ||
import { Context } from "@grucloud/bau-ui/context"; | ||
|
||
export default (context: Context) => { | ||
const { bau, css } = context; | ||
const { section, div, span } = bau.tags; | ||
|
||
const Select = select(context); | ||
|
||
const defaultCode = "AD"; | ||
|
||
const options = [ | ||
{ code: "AD", label: "Andorra", phone: "376" }, | ||
{ | ||
code: "AE", | ||
label: "United Arab Emirates", | ||
phone: "971", | ||
}, | ||
{ code: "AF", label: "Afghanistan", phone: "93" }, | ||
]; | ||
|
||
const Option = (option: any) => | ||
div( | ||
{ | ||
class: css` | ||
display: flex; | ||
justify-content: space-between; | ||
gap: 0.5rem; | ||
`, | ||
}, | ||
span(option.label), | ||
span(option.code) | ||
); | ||
|
||
return () => | ||
section( | ||
Select({ | ||
options, | ||
Option, | ||
defaultOption: options.find(({ code }) => code == defaultCode), | ||
getOptionValue: ({ code }: any) => code, | ||
getOptionLabel: ({ label }: any) => label, | ||
label: "Select a country...", | ||
}) | ||
); | ||
}; |
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
78 changes: 78 additions & 0 deletions
78
bau-ui/examples/bau-storybook/src/pages/select/select-loading.ts
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,78 @@ | ||
import { Context } from "@grucloud/bau-ui/context"; | ||
import select from "@grucloud/bau-ui/select"; | ||
import button from "@grucloud/bau-ui/button"; | ||
|
||
export default (context: Context) => { | ||
const { bau, css } = context; | ||
const { section, div, span } = bau.tags; | ||
|
||
const Button = button(context, { variant: "outline" }); | ||
const Select = select(context); | ||
|
||
const dataState = bau.state([]); | ||
const loadingState = bau.state(false); | ||
const errorMessageState = bau.state(""); | ||
|
||
async function fetchData({ url, transform = (x: any) => x }: any) { | ||
try { | ||
loadingState.val = true; | ||
const response = await fetch(url, {}); | ||
if (response.ok) { | ||
const json = await response.json(); | ||
dataState.val = transform(json); | ||
} else { | ||
errorMessageState.val = response.statusText; | ||
} | ||
} catch (error: any) { | ||
errorMessageState.val = error.message; | ||
} finally { | ||
loadingState.val = false; | ||
} | ||
} | ||
const fetchCountries = () => | ||
fetchData({ | ||
url: "https://restcountries.com/v3.1/all?fields=name,flag", | ||
transform: (data: any) => | ||
data.sort((a: any, b: any) => | ||
a.name.common.localeCompare(b.name.common) | ||
), | ||
}); | ||
|
||
fetchCountries(); | ||
|
||
const Option = (option: any) => | ||
div( | ||
{ | ||
class: css` | ||
display: flex; | ||
justify-content: space-between; | ||
gap: 0.5rem; | ||
`, | ||
}, | ||
span(option.flag), | ||
span(option.name.common) | ||
); | ||
|
||
return () => | ||
section( | ||
div( | ||
{ | ||
class: css` | ||
display: flex; | ||
gap: 1rem; | ||
`, | ||
}, | ||
() => | ||
Select({ | ||
options: dataState.val, | ||
Option, | ||
getOptionValue: ({ name }: any) => name.common, | ||
getOptionLabel: ({ name }: any) => name.common, | ||
label: "Country", | ||
id: "country", | ||
loading: loadingState.val, | ||
}), | ||
Button({ onclick: () => fetchCountries() }, "Reload") | ||
) | ||
); | ||
}; |
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
Oops, something went wrong.