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 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
59 changes: 49 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,64 @@
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,
...(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 118 in src/controls/TextControl.tsx

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L118

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

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

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L127

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

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

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

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L147

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

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

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L149

Added line #L149 was not covered by tests
<Input
{...{
...rest,
...(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