From 05ef955973128648b8e4f16f1aec41fd752eb1e9 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Wed, 11 Dec 2024 05:37:02 -0500 Subject: [PATCH] Add Submit button and pretty fix --- wrappers/js/vanilla/index.jsx | 309 +++++++++++------- .../ts/vanilla/DateTimeLocalField.test.jsx | 12 +- wrappers/ts/vanilla/index.tsx | 251 +++++++------- wrappers/tsconfig.json | 2 +- 4 files changed, 332 insertions(+), 242 deletions(-) diff --git a/wrappers/js/vanilla/index.jsx b/wrappers/js/vanilla/index.jsx index b91d0b9..3e10e95 100644 --- a/wrappers/js/vanilla/index.jsx +++ b/wrappers/js/vanilla/index.jsx @@ -7,14 +7,14 @@ * There is no style and structured with bare necessities. You should modify * these components to fit your design needs. */ -import React, { useContext, createContext, useMemo } from 'react'; -export const ValidationContext = createContext({}); -export const useErrorKeyValidation = ({ errorKey, }) => { - const errors = useContext(ValidationContext); - return useMemo(() => { - return errors[errorKey]; - }, [errors, errorKey]); -}; +import React, { useContext, createContext, useMemo } from 'react' +export const ValidationContext = createContext({}) +export const useErrorKeyValidation = ({ errorKey }) => { + const errors = useContext(ValidationContext) + return useMemo(() => { + return errors[errorKey] + }, [errors, errorKey]) +} /** * Extras renders the hidden inputs generated by form_props. * @@ -22,24 +22,28 @@ export const useErrorKeyValidation = ({ errorKey, }) => { * utf8, crsf_token, _method */ export const Extras = (hiddenInputAttributes) => { - const hiddenProps = Object.values(hiddenInputAttributes); - const hiddenInputs = hiddenProps.map((props) => ()); - return <>{hiddenInputs}; -}; + const hiddenProps = Object.values(hiddenInputAttributes) + const hiddenInputs = hiddenProps.map((props) => ( + + )) + return <>{hiddenInputs} +} /** * A basic form component that supports inline errors. * * It's meant to be used with FormProps and mimics the ways that * Rails forms are generated. */ -export const Form = ({ extras, validationErrors, children, ...props }) => { - return (
+export const Form = ({ extras, validationErrors = {}, children, ...props }) => { + return ( + {children} -
); -}; + + ) +} /** * An inline error component. * @@ -47,84 +51,121 @@ export const Form = ({ extras, validationErrors, children, ...props }) => { * Please modify this to your liking. */ export const FieldError = ({ errorKey }) => { - const errors = useContext(ValidationContext); - if (!errorKey || !errors) { - return null; - } - const validationError = errors[errorKey]; - const hasErrors = errorKey && validationError; - if (!hasErrors) { - return null; - } - const errorMessages = Array.isArray(validationError) - ? validationError - : [validationError]; - return {errorMessages.join(' ')}; -}; + const errors = useContext(ValidationContext) + if (!errorKey || !errors) { + return null + } + const validationError = errors[errorKey] + const hasErrors = errorKey && validationError + if (!hasErrors) { + return null + } + const errorMessages = Array.isArray(validationError) + ? validationError + : [validationError] + return {errorMessages.join(' ')} +} /** * A Field component. * * Combines a label, input and a FieldError. Please modify this to your liking. */ export const FieldBase = ({ label, errorKey, children, ...props }) => { - return (<> + return ( + <> - {children || } - - ); -}; -export const Checkbox = ({ type: _type, includeHidden, uncheckedValue, errorKey, ...rest }) => { - const { name } = rest; - return ( - {includeHidden && ()} + {children || } + + + ) +} +export const Checkbox = ({ + type: _type, + includeHidden, + uncheckedValue, + errorKey, + ...rest +}) => { + const { name } = rest + return ( + + {includeHidden && ( + + )} - ); -}; + + ) +} /** * A collection checkbox component. * * Designed to work with a payload form_props's [collection_check_boxes helper](https://github.com/thoughtbot/form_props?tab=readme-ov-file#collection-select). * Mimics the rails equivalent. Please modify to your liking. */ -export const CollectionCheckboxes = ({ includeHidden, collection, label, errorKey, }) => { - if (collection.length == 0) { - return null; - } - const checkboxes = collection.map((options) => { - return ; - }); - const { name } = collection[0]; - return (<> - {includeHidden && ()} +export const CollectionCheckboxes = ({ + includeHidden, + collection, + label, + errorKey, +}) => { + if (collection.length == 0) { + return null + } + const checkboxes = collection.map((options) => { + return + }) + const { name } = collection[0] + return ( + <> + {includeHidden && ( + + )} {checkboxes} - - ); -}; + + + ) +} /** * A collection radio button component. * * Designed to work with a payload form_props's [collection_radio_buttons helper](https://github.com/thoughtbot/form_props?tab=readme-ov-file#collection-select). * Mimics the rails equivalent. Please modify to your liking. */ -export const CollectionRadioButtons = ({ includeHidden, collection, label, errorKey, }) => { - if (collection.length == 0) { - return null; - } - const radioButtons = collection.map((options) => { - return (
- +export const CollectionRadioButtons = ({ + includeHidden, + collection, + label, + errorKey, +}) => { + if (collection.length == 0) { + return null + } + const radioButtons = collection.map((options) => { + return ( +
+ -
); - }); - const { name } = collection[0]; - return (<> - {includeHidden && ()} +
+ ) + }) + const { name } = collection[0] + return ( + <> + {includeHidden && ( + + )} {radioButtons} - - ); -}; + + + ) +} /** * A text field component. * @@ -132,8 +173,8 @@ export const CollectionRadioButtons = ({ includeHidden, collection, label, error * Mimics the rails equivalent. Please modify to your liking. */ export const TextField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A email field component. * @@ -141,8 +182,8 @@ export const TextField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const EmailField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A color field component. * @@ -150,8 +191,8 @@ export const EmailField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const ColorField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A date field component. * @@ -159,8 +200,8 @@ export const ColorField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const DateField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A date field component. * @@ -168,8 +209,8 @@ export const DateField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const DateTimeLocalField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A search field component. * @@ -177,8 +218,8 @@ export const DateTimeLocalField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const SearchField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A tel field component. * @@ -186,8 +227,8 @@ export const SearchField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const TelField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A url field component. * @@ -195,8 +236,8 @@ export const TelField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const UrlField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A month field component. * @@ -204,8 +245,8 @@ export const UrlField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const MonthField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A month field component. * @@ -213,8 +254,8 @@ export const MonthField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const TimeField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A number field component. * @@ -222,8 +263,8 @@ export const TimeField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const NumberField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A range field component. * @@ -231,8 +272,8 @@ export const NumberField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const RangeField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A password field component. * @@ -240,8 +281,8 @@ export const RangeField = ({ type: _type, ...rest }) => { * Mimics the rails equivalent. Please modify to your liking. */ export const PasswordField = ({ type: _type, ...rest }) => { - return ; -}; + return +} /** * A select component. * @@ -250,26 +291,42 @@ export const PasswordField = ({ type: _type, ...rest }) => { * * Please modify to your liking. */ -export const Select = ({ includeHidden, name, id, children, options, multiple, type: _type, ...rest }) => { - const addHidden = includeHidden && multiple; - const optionElements = options.map((item) => { - if ('options' in item) { - return ( - {item.options.map((opt) => (); - } - else { - return + {item.options.map((opt) => ( + + ) + } else { + return