Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions apps/demo/src/Views/Ui/components/FormWidgets/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,185 @@ const [selectedValue, setSelectedValue] = useState<string>("");
hasError={!selectedValue}
errorMessage={!selectedValue ? "Required field" : ""}
placeholder={t("select.placeholder")}
/>'
/>
</Section>
<Section title={t("select.usage.group")}>
<Select
label={t("select.label")}
name="select"
options={[
{
label: "Europe",
options: [
{ label: "Germany", value: "DE" },
{ label: "France", value: "FR" },
{ disabled: true, label: "Belgium", value: "BE" },
],
},
{
label: "Asia",
options: [
{ label: "Nepal", value: "NP" },
{ label: "India", value: "IN" },
],
},
]}
value={singleSelectValue}
onChange={(value: string) => setSingleSelectValue(value)}
placeholder={t("select.placeholder")}
/>
<CodeBlock
exampleCode='
const [singleSelectValue, setSingleSelectValue] = useState<string>("");

<Select
label={t("select.label")}
name="select"
options={[
{
label: "Europe",
options: [
{ label: "Germany", value: "DE" },
{ label: "France", value: "FR" },
{ disabled: true, label: "Belgium", value: "BE" },
],
},
{
label: "Asia",
options: [
{ label: "Nepal", value: "NP" },
{ label: "India", value: "IN" },
],
},
]}
value={singleSelectValue}
onChange={(value: string) => setSingleSelectValue(value)}
placeholder={t("select.placeholder")}
/>'
/>
<p>
<Trans
i18nKey={"ui:select.autoSortOptionsInfo"}
components={{ code: <code /> }}
></Trans>
</p>
</Section>
<Section title={t("select.usage.groupMultiSelect")}>
<Select
label={t("select.label")}
name="select"
options={[
{
label: "Europe",
options: [
{ label: "Germany", value: "DE" },
{ label: "France", value: "FR" },
{ disabled: true, label: "Belgium", value: "BE" },
],
},
{
label: "Asia",
options: [
{ label: "Nepal", value: "NP" },
{ label: "India", value: "IN" },
],
},
]}
multiple={true}
value={multiselectValue}
onChange={(value: string[]) => setMultiselectValue(value)}
placeholder={t("select.multiSelectPlaceholder")}
/>
<CodeBlock
exampleCode='
const [multiSelectPlaceholder, setMultiSelectPlaceholder] = useState<string>("");

<Select
label={t("select.label")}
name="select"
options={
[
{
label: "Europe",
options: [
{ label: "Germany", value: "DE" },
{ label: "France", value: "FR" },
{ disabled: true, label: "Belgium", value: "BE" },
],
},
{
label: "Asia",
options: [
{ label: "Nepal", value: "NP" },
{ label: "India", value: "IN" },
],
},
]}
multiple={true}
value={multiselectValue}
onChange={(value: string[]) => setMultiselectValue(value)}
placeholder={t("select.multiSelectPlaceholder")}
/>'
/>
</Section>
<Section title={t("select.usage.groupMultiSelectDisabled")}>
<Select
label={t("select.label")}
name="select"
disableGroupSelect={true}
options={[
{
label: "Europe",
options: [
{ label: "Germany", value: "DE" },
{ label: "France", value: "FR" },
{ disabled: true, label: "Belgium", value: "BE" },
],
},
{
label: "Asia",
options: [
{ label: "Nepal", value: "NP" },
{ label: "India", value: "IN" },
],
},
]}
multiple={true}
value={multiselectValue}
onChange={(value: string[]) => setMultiselectValue(value)}
placeholder={t("select.multiSelectPlaceholder")}
/>
<CodeBlock
exampleCode='
const [multiSelectPlaceholder, setMultiSelectPlaceholder] = useState<string>("");

<Select
label={t("select.label")}
name="select"
disableGroupSelect={true}
options={
[
{
label: "Europe",
options: [
{ label: "Germany", value: "DE" },
{ label: "France", value: "FR" },
{ disabled: true, label: "Belgium", value: "BE" },
],
},
{
label: "Asia",
options: [
{ label: "Nepal", value: "NP" },
{ label: "India", value: "IN" },
],
},
]}
multiple={true}
value={multiselectValue}
onChange={(value: string[]) => setMultiselectValue(value)}
placeholder={t("select.multiSelectPlaceholder")}
/>'
/>
</Section>
Expand Down
3 changes: 3 additions & 0 deletions apps/demo/src/locales/en/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@
"usage": {
"basic": "Basic",
"disabled": "Disabled",
"group": "With grouping",
"groupMultiSelect": "Multiselect with grouping",
"groupMultiSelectDisabled": "Multiselect with group select disabled",
"invalid": "Invalid",
"key": "With label and value keys",
"multiple": "Multiple select",
Expand Down
3 changes: 3 additions & 0 deletions apps/demo/src/locales/fr/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@
"usage": {
"basic": "Basic",
"disabled": "Disabled (fr)",
"group": "With grouping (fr)",
"groupMultiSelect": "Multiselect with grouping (fr)",
"groupMultiSelectDisabled": "Multiselect with group select disabled (fr)",
"invalid": "Invalid (fr)",
"key": "With label and value keys (fr)",
"multiple": "Multiple select (fr)",
Expand Down
22 changes: 16 additions & 6 deletions packages/form/src/components/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Select as BasicSelect, ISelectProperties } from "@dzangolab/react-ui";
import React, { useEffect } from "react";
import {
Select as BasicSelect,
ISelectProperties,
Option,
} from "@dzangolab/react-ui";
import React, { useEffect, useMemo } from "react";
import { Controller, useFormContext } from "react-hook-form";

interface ValidationMessages {
Expand Down Expand Up @@ -42,17 +46,23 @@ export const Select = <T extends string | number>({
if (showValidState && !invalid) return false;
};

const flatOptions = useMemo(() => {
return options.flatMap((opt) =>
"options" in opt ? opt.options : [opt],
) as Option<T>[];
}, [options]);

//TODO [MA 2024-05-31]: remove this redundant useEffect for auto selecting single option
useEffect(() => {
if (
autoSelectSingleOption &&
!multiple &&
options.length === 1 &&
!options[0].disabled
flatOptions.length === 1 &&
!flatOptions[0].disabled
) {
setValue(name, options[0].value);
setValue(name, flatOptions[0].value);
}
}, [options]);
}, [flatOptions]);

return (
<Controller
Expand Down
Loading