Skip to content

Commit

Permalink
fix: destructure inputProps (#81)
Browse files Browse the repository at this point in the history
* Fix [Object object]

* remove control option props
  • Loading branch information
elenajdanova authored Jun 4, 2024
1 parent 37fd72a commit 27051e7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
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 {
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 @@ function TextControlInput({
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 (
<Input.TextArea
{...{
...rest,
...(inputProps as TextAreaProps),
}}
/>
)
case "singleline":
return <Input {...{ ...rest, ...textControlOptions }} />
return (
<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)
} catch (e) {
return <Input {...{ ...rest, ...textControlOptions }} />
return (
<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

0 comments on commit 27051e7

Please sign in to comment.