-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: booleanFIeld input hook and demo
- Loading branch information
1 parent
3b0c40e
commit d6957ba
Showing
4 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { booleanField } from "./booleanField"; | ||
import { useBooleanFieldProps } from "./useBooleanFieldProps"; | ||
import { FieldLabel } from "../../components"; | ||
import { FieldErrors } from "../../components/field-errors"; | ||
import { FormStory, fixArgs, meta } from "../../scenarios/StoryForm"; | ||
import { SelectFieldProps, useSelectOptions } from "../select-field"; | ||
|
||
export default { | ||
...meta, | ||
title: "fields/booleanField", | ||
}; | ||
|
||
const YesNoOptions = <Option,>({ | ||
field, | ||
label, | ||
getValue, | ||
getLabel, | ||
options, | ||
}: SelectFieldProps<Option, boolean>) => { | ||
const props = useBooleanFieldProps(field); | ||
|
||
const { renderOptions } = useSelectOptions(field, { | ||
getValue, | ||
getLabel, | ||
options, | ||
}); | ||
|
||
return ( | ||
<div style={{ margin: "20px 0" }}> | ||
<FieldLabel field={field} label={label} /> | ||
{renderOptions.map(({ id, value, label, isActive }) => ( | ||
<div key={id}> | ||
<input | ||
type="radio" | ||
{...props} | ||
id={id} | ||
value={`${value}`} | ||
checked={isActive} | ||
/> | ||
<label htmlFor={id}>{label}</label> | ||
</div> | ||
))} | ||
<div> | ||
<FieldErrors field={field} /> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const yesNoOptions = [ | ||
{ | ||
label: "Yes, I accept the Funeral terms.", | ||
value: true, | ||
}, | ||
{ | ||
label: "No, I don't accept the Funeral terms.", | ||
value: false, | ||
}, | ||
]; | ||
export const Required: FormStory = { | ||
args: fixArgs({ | ||
fields: { | ||
termsOfService: booleanField(), | ||
}, | ||
children: ({ fields }) => ( | ||
<YesNoOptions | ||
field={fields.termsOfService} | ||
label="Do you accept the Funeral terms?" | ||
options={yesNoOptions} | ||
getLabel={({ label }) => label} | ||
getValue={({ value }) => value} | ||
/> | ||
), | ||
}), | ||
}; | ||
|
||
// export const Optional: FormStory = { | ||
// args: fixArgs({ | ||
// fields: { | ||
// newsletter: checkboxField({ | ||
// optional: true, | ||
// }), | ||
// }, | ||
// children: ({ fields }) => ( | ||
// <CheckboxInput | ||
// field={fields.newsletter} | ||
// label="Subscribe to newsletter" | ||
// /> | ||
// ), | ||
// }), | ||
// parameters: { | ||
// docs: { | ||
// description: { | ||
// story: | ||
// "Optional checkboxField will have true or false value in the submit data.", | ||
// }, | ||
// }, | ||
// }, | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ChangeEvent } from "react"; | ||
|
||
import { BooleanFieldAtom } from "./booleanField"; | ||
import { FieldProps, useFieldProps } from "../../hooks"; | ||
|
||
export type NumberFieldProps = FieldProps<BooleanFieldAtom>; | ||
|
||
const valueAsBoolean = (event: ChangeEvent<HTMLInputElement>) => { | ||
const value = JSON.parse(event.currentTarget.value); | ||
|
||
return typeof value === "boolean" ? value : undefined; | ||
}; | ||
|
||
export const useBooleanFieldProps = (field: BooleanFieldAtom) => | ||
useFieldProps(field, valueAsBoolean); |