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rD,CAAC,CAAC,CAAC;AAOH,MAAM,gBAAgB,GAAG,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;KACxC,UAAU,EAAuB;KACjC,QAAQ,EAAE,CAAC;AAEb,MAAM,CAAC,MAAM,SAAS,GAAG,gBAAgB,CAAC,SAAS,SAAS,CAAC,EAC5D,UAAU,EACV,QAAQ,EACR,UAAU,EAAE,EAAE,KAAK,EAAE,EACrB;IACA,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,CAAC;IAChC,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAErC,OAAO,CACN,aACC,GAAG,EAAC,MAAM,EACV,OAAO,EAAE;YACR,KAAK,CAAC,OAAO,EAAE;YACf,SAAS,CAAC,SAAS;YACnB,CAAC,MAAM,IAAI,SAAS,CAAC,GAAG;YACxB,MAAM,IAAI,SAAS,CAAC,MAAM;SAC1B,IAEA,QAAQ,EAAE,CACN,CACN,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,MAAM,gBAAgB,GAAG,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;AAE3C,MAAM,CAAC,MAAM,SAAS,GAAG,gBAAgB,CAAC,SAAS,SAAS,CAAC,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,KAAK,EAAE,EAAE;IAC/F,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAErC,OAAO,CACN,aAAK,GAAG,EAAC,MAAM,EAAC,OAAO,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,SAAS,CAAC,SAAS,CAAC,IAC7D,QAAQ,EAAE,CACN,CACN,CAAC;AACH,CAAC,CAAC,CAAC","sourcesContent":["import { RenderResult, VNodeProperties } from '@dojo/framework/core/interfaces';\nimport icache from '@dojo/framework/core/middleware/icache';\nimport theme from '../middleware/theme';\nimport { create, tsx } from '@dojo/framework/core/vdom';\n\nimport * as css from '../theme/default/form.m.css';\nimport createFormMiddleware, { FormMiddleware, FormValue } from './middleware';\n\nconst form = createFormMiddleware();\n\ntype Omit<T, E> = Pick<T, Exclude<keyof T, E>>;\n\ninterface BaseFormProperties {\n\t/** The initial form value */\n\tinitialValue?: FormValue;\n\t/** Controlled form value component */\n\tvalue?: FormValue;\n\t/** Callback called when a form value changes */\n\tonValue?(values: FormValue): void;\n\t/** The name property of the form */\n\tname?: string;\n\n\tonSubmit?: never;\n\taction?: never;\n}\n\ninterface SubmitFormProperties extends Omit<BaseFormProperties, 'onSubmit'> {\n\t/** Callback for when the form is submitted with valid values */\n\tonSubmit(values: FormValue): void;\n}\n\ninterface ActionFormProperties extends Omit<BaseFormProperties, 'action'> {\n\t/** Action url for the form on submit */\n\taction: string;\n\t/** method of submit, defaults to `post` */\n\tmethod?: 'post' | 'get';\n}\n\nexport type FormProperties = BaseFormProperties | SubmitFormProperties | ActionFormProperties;\n\nfunction valueEqual(a: FormValue, b: FormValue = {}) {\n\treturn (\n\t\tObject.keys(a).length === Object.keys(b).length &&\n\t\tObject.keys(a).every((key) => a[key] === b[key])\n\t);\n}\n\nexport type FormChildRenderer<S extends FormValue = any> = (\n\tproperties: FormMiddleware<S>\n) => RenderResult;\n\nfunction isSubmitForm(properties: FormProperties): properties is SubmitFormProperties {\n\treturn (properties as SubmitFormProperties).onSubmit !== undefined;\n}\n\nfunction isActionForm(properties: FormProperties): properties is ActionFormProperties {\n\treturn (properties as ActionFormProperties).action !== undefined;\n}\n\nconst factory = create({ form, theme, icache })\n\t.properties<FormProperties>()\n\t.children<FormChildRenderer>();\n\nexport default factory(function Form({\n\tproperties,\n\tchildren,\n\tmiddleware: { form, theme, icache }\n}) {\n\tconst themedCss = theme.classes(css);\n\tconst props = properties();\n\n\tlet formProps: Partial<VNodeProperties> = {\n\t\tclasses: [theme.variant(), themedCss.root],\n\t\tname: props.name\n\t};\n\n\tconst { initialValue, value, onValue } = props;\n\n\tif (isSubmitForm(props)) {\n\t\tformProps = {\n\t\t\t...formProps,\n\t\t\tonsubmit: (event) => {\n\t\t\t\tevent.preventDefault();\n\t\t\t\tform.submit((value) => props.onSubmit(value));\n\t\t\t}\n\t\t};\n\t} else if (isActionForm(props)) {\n\t\tconst { action, method = 'post' } = props;\n\t\tformProps = {\n\t\t\t...formProps,\n\t\t\taction,\n\t\t\tmethod\n\t\t};\n\t}\n\n\tconst [renderer] = children();\n\n\tonValue && form.onValue(onValue);\n\n\tif (value) {\n\t\tform.value(value);\n\t} else if (initialValue !== undefined && !valueEqual(initialValue, icache.get('initial'))) {\n\t\ticache.set('initial', initialValue, false);\n\t\tform.value(initialValue);\n\t}\n\n\treturn <form {...formProps}>{renderer(form)}</form>;\n});\n\nexport interface FormGroupProperties {\n\t/** Render this grouping in a vertical column */\n\tcolumn?: boolean;\n}\n\nconst formGroupFactory = create({ theme })\n\t.properties<FormGroupProperties>()\n\t.children();\n\nexport const FormGroup = formGroupFactory(function FormGroup({\n\tproperties,\n\tchildren,\n\tmiddleware: { theme }\n}) {\n\tconst { column } = properties();\n\tconst themedCss = theme.classes(css);\n\n\treturn (\n\t\t<div\n\t\t\tkey=\"root\"\n\t\t\tclasses={[\n\t\t\t\ttheme.variant(),\n\t\t\t\tthemedCss.groupRoot,\n\t\t\t\t!column && themedCss.row,\n\t\t\t\tcolumn && themedCss.column\n\t\t\t]}\n\t\t>\n\t\t\t{children()}\n\t\t</div>\n\t);\n});\n\nconst formFieldFactory = create({ theme });\n\nexport const FormField = formFieldFactory(function FormField({ children, middleware: { theme } }) {\n\tconst themedCss = theme.classes(css);\n\n\treturn (\n\t\t<div key=\"root\" classes={[theme.variant(), themedCss.fieldRoot]}>\n\t\t\t{children()}\n\t\t</div>\n\t);\n});\n"]}