feat(form): add form action for submit (#237)
This commit is contained in:
parent
5256243858
commit
234ffaf38c
4 changed files with 73 additions and 1 deletions
|
@ -4,6 +4,7 @@ import Form, {
|
||||||
FormGroup,
|
FormGroup,
|
||||||
FormControl,
|
FormControl,
|
||||||
} from "@astro-reactive/form";
|
} from "@astro-reactive/form";
|
||||||
|
import type { Submit } from "@astro-reactive/common/types";
|
||||||
import { Validators } from "@astro-reactive/validator";
|
import { Validators } from "@astro-reactive/validator";
|
||||||
import Layout from "../components/Layout.astro";
|
import Layout from "../components/Layout.astro";
|
||||||
|
|
||||||
|
@ -94,6 +95,11 @@ form.get("email")?.setValue("invalid-email");
|
||||||
// switch between light and dark mode
|
// switch between light and dark mode
|
||||||
const title = "Form Demo";
|
const title = "Form Demo";
|
||||||
const theme = "dark";
|
const theme = "dark";
|
||||||
|
|
||||||
|
const submit: Submit = {
|
||||||
|
name: "submit",
|
||||||
|
type: "submit",
|
||||||
|
};
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title={title} theme={theme}>
|
<Layout title={title} theme={theme}>
|
||||||
|
@ -102,5 +108,8 @@ const theme = "dark";
|
||||||
showValidationHints={true}
|
showValidationHints={true}
|
||||||
formGroups={form}
|
formGroups={form}
|
||||||
theme={theme}
|
theme={theme}
|
||||||
|
submitControl={submit}
|
||||||
|
action="https://localhost"
|
||||||
|
method="POST"
|
||||||
/>
|
/>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
|
@ -27,6 +27,17 @@ export type InputType =
|
||||||
| "url"
|
| "url"
|
||||||
| "week";
|
| "week";
|
||||||
|
|
||||||
|
export type HTTPRequestMethodType =
|
||||||
|
| "GET"
|
||||||
|
| "POST"
|
||||||
|
| "PUT"
|
||||||
|
| "PATCH"
|
||||||
|
| "DELETE"
|
||||||
|
| "TRACE"
|
||||||
|
| "OPTIONS"
|
||||||
|
| "CONNECT"
|
||||||
|
| "HEAD";
|
||||||
|
|
||||||
export type ControlType = InputType | "dropdown" | "textarea";
|
export type ControlType = InputType | "dropdown" | "textarea";
|
||||||
|
|
||||||
export interface ControlBase {
|
export interface ControlBase {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
import type { Submit } from '@astro-reactive/common';
|
import type { HTTPRequestMethodType, Submit } from '@astro-reactive/common';
|
||||||
import { FormGroup, FormControl } from '../core';
|
import { FormGroup, FormControl } from '../core';
|
||||||
import FieldSet from './FieldSet.astro';
|
import FieldSet from './FieldSet.astro';
|
||||||
import Field from './Field.astro';
|
import Field from './Field.astro';
|
||||||
|
@ -12,6 +12,8 @@ export interface Props {
|
||||||
validateOnLoad?: boolean;
|
validateOnLoad?: boolean;
|
||||||
submitControl?: Submit;
|
submitControl?: Submit;
|
||||||
theme?: 'light' | 'dark';
|
theme?: 'light' | 'dark';
|
||||||
|
action?: string;
|
||||||
|
method?: HTTPRequestMethodType;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
@ -21,6 +23,8 @@ const {
|
||||||
showValidationHints = false,
|
showValidationHints = false,
|
||||||
validateOnLoad = false,
|
validateOnLoad = false,
|
||||||
readOnly = false,
|
readOnly = false,
|
||||||
|
action = "",
|
||||||
|
method = "GET"
|
||||||
} = Astro.props;
|
} = Astro.props;
|
||||||
|
|
||||||
const uid = new ShortUniqueId({ length: 9 });
|
const uid = new ShortUniqueId({ length: 9 });
|
||||||
|
@ -50,6 +54,8 @@ if (validateOnLoad) {
|
||||||
name={formName}
|
name={formName}
|
||||||
id={formId}
|
id={formId}
|
||||||
data-validator-hints={showValidationHints.toString()}
|
data-validator-hints={showValidationHints.toString()}
|
||||||
|
action={action}
|
||||||
|
method={method}
|
||||||
>
|
>
|
||||||
{
|
{
|
||||||
Array.isArray(formGroups)
|
Array.isArray(formGroups)
|
||||||
|
|
|
@ -87,6 +87,7 @@ describe('Form.astro test', () => {
|
||||||
// assert
|
// assert
|
||||||
expect(matches.length).to.equal(expectedCount);
|
expect(matches.length).to.equal(expectedCount);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should render readOnly fields if the flag is passed as true', async () => {
|
it('Should render readOnly fields if the flag is passed as true', async () => {
|
||||||
// arrange
|
// arrange
|
||||||
const expectedCount = 3;
|
const expectedCount = 3;
|
||||||
|
@ -110,5 +111,50 @@ describe('Form.astro test', () => {
|
||||||
// assert
|
// assert
|
||||||
expect(matches.length).to.equal(expectedCount);
|
expect(matches.length).to.equal(expectedCount);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should render a submit button correctly', async () => {
|
||||||
|
// arrange
|
||||||
|
const expectedType = 'type="submit"';
|
||||||
|
const expectedName = 'name="submitNameTest"';
|
||||||
|
const submit = {
|
||||||
|
name: 'submitNameTest',
|
||||||
|
type: 'submit',
|
||||||
|
};
|
||||||
|
const props = { submitControl: submit };
|
||||||
|
component = await getComponentOutput('./components/Form.astro', props);
|
||||||
|
|
||||||
|
// act
|
||||||
|
const actualResult = cleanString(component.html);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
expect(actualResult).to.contain(expectedType);
|
||||||
|
expect(actualResult).to.contain(expectedName);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should render an action property correctly', async () => {
|
||||||
|
// arrange
|
||||||
|
const expectedResult = 'action="https://localhost"';
|
||||||
|
const props = { action: 'https://localhost' };
|
||||||
|
component = await getComponentOutput('./components/Form.astro', props);
|
||||||
|
|
||||||
|
// act
|
||||||
|
const actualResult = cleanString(component.html);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
expect(actualResult).to.contain(expectedResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should render a method property correctly', async () => {
|
||||||
|
// arrange
|
||||||
|
const expectedResult = 'method="GET"';
|
||||||
|
const props = { method: 'GET' };
|
||||||
|
component = await getComponentOutput('./components/Form.astro', props);
|
||||||
|
|
||||||
|
// act
|
||||||
|
const actualResult = cleanString(component.html);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
expect(actualResult).to.contain(expectedResult);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue