Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: destructure inputProps #81

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 54 additions & 10 deletions src/controls/TextControl.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ChangeEvent } from "react"
import { useCallback, useEffect } from "react"
import { Input, Form } from "antd"
import { Input, Form, InputProps } from "antd"
import type { Rule } from "antd/es/form"
import type {
ControlElement,
Expand All @@ -10,6 +10,7 @@
import type { ControlUISchema, TextControlOptions } from "../ui-schema"
import { assertNever } from "../common/assert-never"
import { withJsonFormsControlProps } from "@jsonforms/react"
import { TextAreaProps } from "antd/es/input/TextArea"
type ControlProps = Omit<JSFControlProps, "uischema"> & {
data: string
handleChange(path: string, value: string): void
Expand Down Expand Up @@ -95,26 +96,69 @@
textControlOptions,
...rest
}: TextControlInputProps) {
const { inputProps, ...restTextControlOptions } = textControlOptions

if (
!(`type` in textControlOptions) ||
textControlOptions.type === undefined
!(`type` in restTextControlOptions) ||
restTextControlOptions.type === undefined
) {
return <Input {...{ ...rest, ...textControlOptions }} />
return (
<Input
{...{
...rest,
...restTextControlOptions,
elenajdanova marked this conversation as resolved.
Show resolved Hide resolved
...(inputProps as InputProps),
// hate this but because of destructuring we have to go back to casting
}}
/>
)
}

switch (textControlOptions.type) {
switch (restTextControlOptions.type) {
case "multiline":
return <Input.TextArea {...{ ...rest, ...textControlOptions }} />
return (

Check warning on line 119 in src/controls/TextControl.tsx

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L119

Added line #L119 was not covered by tests
<Input.TextArea
{...{
...rest,
...restTextControlOptions,
...(inputProps as TextAreaProps),
}}
/>
)
case "singleline":
return <Input {...{ ...rest, ...textControlOptions }} />
return (

Check warning on line 129 in src/controls/TextControl.tsx

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L129

Added line #L129 was not covered by tests
<Input
{...{
...rest,
...restTextControlOptions,
...(inputProps as InputProps),
}}
/>
)
case "password":
return <Input.Password {...{ ...rest, ...textControlOptions }} />
return (
<Input.Password
{...{
...rest,
...restTextControlOptions,
...(inputProps as InputProps),
}}
/>
)

default:
try {
assertNever(textControlOptions.type)
assertNever(restTextControlOptions.type)

Check warning on line 151 in src/controls/TextControl.tsx

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L151

Added line #L151 was not covered by tests
} catch (e) {
return <Input {...{ ...rest, ...textControlOptions }} />
return (

Check warning on line 153 in src/controls/TextControl.tsx

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L153

Added line #L153 was not covered by tests
<Input
{...{
...rest,
...textControlOptions,
...(inputProps as InputProps),
}}
/>
)
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/stories/controls/TextControl.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export const Password: Story = {
type: "Control",
scope: "#/properties/name",
label: "Name",
options: { type: "password", rules: [] },
options: {
type: "password",
rules: [],
inputProps: { autoComplete: "new-password" },
},
},
],
} satisfies UISchema<typeof schema>,
Expand Down
Loading