Skip to content

Commit

Permalink
feat(OneOfControl): add segmented option to OneOfControl
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewHoo committed Mar 21, 2024
1 parent b8394e0 commit 4b47cb9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/controls/combinators/CombinatorSchemaSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Radio, RadioChangeEvent, Select } from "antd"
import { Radio, RadioChangeEvent, Segmented, Select } from "antd"
import { OneOfControlOptions } from "../../ui-schema"
import merge from "lodash.merge"
import { useEffect, useState } from "react"
Expand Down Expand Up @@ -102,6 +102,16 @@ export function CombinatorSchemaSwitcher({
defaultValue={selectedIndex}
/>
)
case "segmented":
return (
<Segmented
options={options}
onChange={(combinatorIndex: number) =>
setSelectedIndex(combinatorIndex)
}
value={selectedIndex}
/>
)
case "radio":
default:
return (
Expand Down
20 changes: 20 additions & 0 deletions src/controls/combinators/OneOfControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ describe("OneOf control", () => {
await userEvent.click(screen.getByText("Delivery"))
screen.getByLabelText("Address")
})
test("OneOf Control with segmented UISchema allows switching between subschemas", async () => {
render({
schema,
uischema: {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/deliveryOption",
options: { optionType: "segmented" } satisfies OneOfControlOptions,
},
],
},
})
await screen.findByText("Pickup")
screen.getByLabelText("Location")

await userEvent.click(screen.getByText("Delivery"))
screen.getByLabelText("Address")
})
test("OneOf Control persists state when switching between subschemas", async () => {
render({ schema })
await screen.findByText("Pickup")
Expand Down
24 changes: 24 additions & 0 deletions src/stories/controls/OneOfControl.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ export const Dropdown: Story = {
},
}

export const Segmented: Story = {
parameters: { controls: { expanded: true } },
tags: ["autodocs"],
args: {
jsonSchema: schema,
uiSchema: {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/deliveryOption",
options: { optionType: "segmented" } satisfies OneOfControlOptions,
},
],
},
},
argTypes: {
jsonSchema: {
control: "object",
description: "this is a minimal oneOf combinator schema",
},
},
}

export const OneOfTitleLabelStyling: Story = {
parameters: { controls: { expanded: true } },
tags: ["autodocs"],
Expand Down
1 change: 1 addition & 0 deletions src/ui-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const OneOfControlOptions = [
"dropdown",
"radio",
"toggle",
"segmented"
] as const

export type OneOfControlOption = (typeof OneOfControlOptions)[number]
Expand Down

0 comments on commit 4b47cb9

Please sign in to comment.