UNPKG

@korautils/forms

Version:

![image](https://github.com/user-attachments/assets/ccad1514-7b15-4952-9856-fd03b971403a)

1 lines 12.2 kB
{"version":3,"sources":["../src/modules/core/components/shared/AutocompleteField/styles.tsx","../src/modules/builder/utils/index.ts","../src/modules/builder/hooks/useSelectApiRequest.ts","../src/modules/core/components/shared/AutocompleteField/AutocompleteField.tsx","../src/modules/core/components/shared/AutocompleteField/index.ts"],"names":["AutocompleteFieldWrapper","styled","getValueFromPath","obj","path","parts","pathSummary","lastFound","pathTemp","getProp","getDataFromPath","_path","value","finalPath","useSelectApiRequest","api","renderProps","options","fetchData","loading","response","useApiRequest_default","parseOptions","data","isArrayEmpty","item","launch","useCallback","useEffect","useSelectApiRequest_default","AutocompleteField","React","name","uuidv4","label","onChange","formHandler","error","helperText","required","multiple","props","ref","optionsList","selectedValue","selectedItem","jsx","Autocomplete","event","newValue","option","params","TextField","AutocompleteField_default"],"mappings":"yRAEO,IAAMA,CAAgCC,CAAAA,MAAAA,CAAO,KAAK,CAAA;AAAA;AAAA;AAAA;ECoElD,IAAMC,CAAAA,CAAmB,CAACC,CAAaC,CAAAA,CAAAA,GAAsB,CAClE,IAAMC,CAAAA,CAAQD,CAAK,CAAA,KAAA,CAAM,GAAG,CAAA,CAExBE,EAAc,EAAC,CACfC,EAAY,IAEhB,CAAA,IAAA,IAAWH,KAAQC,CAAM,CAAA,OAAA,EAAW,CAAA,CAClCC,CAAY,CAAA,IAAA,CAAKF,CAAI,CAErB,CAAA,IAAMI,EAAW,CAAC,GAAGF,CAAW,CAAE,CAAA,OAAA,EAAU,CAAA,IAAA,CAAK,GAAG,CAAA,CACtCG,IAAQN,CAAKK,CAAAA,CAAQ,IAGjCD,CAAYC,CAAAA,CAAAA,EAEhB,CAEA,OAAOC,GAAAA,CAAQN,CAAKI,CAAAA,CAAAA,EAAaH,CAAI,CACvC,EAEaM,CAAkB,CAAA,CAACP,EAAaC,CAAiB,GAAA,CAC5D,IAAMC,CAAQD,CAAAA,CAAAA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,CAAAA,CAAAA,CAAA,CACC,QAAA,CAAA8C,IAACC,CAAA,CAAA,CACC,IAAKL,CACL,CAAA,EAAA,CAAIV,GAAQC,CAAO,EAAA,CACnB,KAAOW,CAAAA,CAAAA,CACP,QAAUJ,CAAAA,CAAAA,CACV,SAAU,CAACQ,CAAAA,CAAYC,IAAkB,CACvC,OAAOd,GAAa,UAClBA,EAAAA,CAAAA,CAAS,CAAE,MAAA,CAAQ,CAAE,IAAA,CAAAH,EAAM,KAAOiB,CAAAA,CAAS,CAAE,CAAC,EAClD,EAKA,OAASN,CAAAA,CAAAA,CACT,cAAiBO,CAAAA,CAAAA,EAAWA,CAAQ,EAAA,KAAA,EAAS,GAC7C,oBAAsB,CAAA,CAACA,EAAQtC,CAAUsC,GAAAA,CAAAA,EAAQ,QAAUtC,CAAO,EAAA,KAAA,CAClE,WAAcuC,CAAAA,CAAAA,EACZL,GAACM,CAAAA,CAAAA,CAAA,CACE,GAAGD,CAAAA,CACJ,MAAOjB,CACP,CAAA,UAAA,CAAYI,EACZ,KAAOD,CAAAA,CAAAA,CACT,CAEF,CAAA,SAAA,CAAS,CACR,CAAA,CAAA,GAAGI,EACN,CACF,CAAA,CAEJ,CAAC,CAEMY,CAAAA,CAAAA,CAAQtB,EAAM,IAAKD,CAAAA,CAAiB,CCzF3C,CAAA,IAAOuB,EAAQA,CAAAA","file":"AutocompleteField-VJG3HWL7.mjs","sourcesContent":["import { styled } from '@mui/material'\n\nexport const AutocompleteFieldWrapper:any = styled('div')`\n display: flex;\n flex-direction: column;\n width: 100%;\n`\n","import { OptionSelect } from '@/modules/builder/interfaces/elements/types'\nimport { getProp, isArrayEmpty, uuidv4 } from '@/modules/core/utils'\nimport { TableItem } from '@/modules/builder/interfaces/general'\n\nexport const arrayToOptions = (array: Array<string>) => {\n if (isArrayEmpty(array)) {\n return []\n }\n\n return array.map((item) => ({ label: item, value: item }))\n}\n\nexport const stringToOption = (string: string, upperCase: boolean = true): OptionSelect | undefined => {\n if (!string) {\n return\n }\n\n return {\n label: upperCase ? string.toUpperCase() : string,\n value: upperCase ? string.toUpperCase() : string,\n }\n}\n\nexport const objectToTableData = (object: TableItem) => {\n const objectKeys = object ? Object.keys(object) : []\n\n const data: Array<any> = []\n\n if (objectKeys.length > 0) {\n for (const key in object) {\n if (object.hasOwnProperty(key)) {\n data.push({\n id: uuidv4(),\n name: key,\n value: getProp(object, key),\n })\n }\n }\n }\n\n return data\n}\n\n/**\n * Ordena las claves de un objeto colocando las claves con valores de tipo array al final.\n * @param {Object} obj - El objeto cuyas claves se quieren ordenar.\n * @returns {Array<string>} - Un array con las claves del objeto ordenadas.\n */\nexport const sortKeysByType = (obj: any) => {\n return Object.keys(obj).sort((a, b) => {\n const isArrayA = Array.isArray(obj[a])\n const isArrayB = Array.isArray(obj[b])\n\n if (isArrayA && !isArrayB) {\n return 1 // `a` después de `b` si `a` es un array y `b` no lo es\n } else if (!isArrayA && isArrayB) {\n return -1 // `a` antes de `b` si `a` no es un array y `b` sí lo es\n }\n return 0 // Mantén el orden si ambos son arrays o ninguno lo es\n })\n}\n\nexport const limitArrayChilds = (obj: any) => {\n if (Array.isArray(obj) && !isArrayEmpty(obj)) {\n return obj.slice(0, 1)\n }\n\n return obj\n}\n\nexport const getValueFromPath = (obj: object, path: string): any => {\n const parts = path.split('.')\n\n let pathSummary = []\n let lastFound = null\n\n for (const path of parts.reverse()) {\n pathSummary.push(path)\n\n const pathTemp = [...pathSummary].reverse().join('.')\n const value = getProp(obj, pathTemp)\n\n if (value) {\n lastFound = pathTemp\n }\n }\n\n return getProp(obj, lastFound || path)\n}\n\nexport const getDataFromPath = (obj: object, path: string) => {\n const parts = path.split('.')\n\n let pathSummary = []\n let lastFound = null\n\n for (const path of parts) {\n pathSummary.push(path)\n const _path = [...pathSummary].join('.')\n const value = getProp(obj, _path)\n\n if (value && Array.isArray(value)) {\n lastFound = _path\n }\n\n const finalPath = lastFound || path\n let finalValue = ['[0]', '0'].includes(finalPath)\n ? obj\n : getProp(obj, finalPath)\n return finalValue\n }\n}\n\nexport const parseNum = (num: number | string | undefined) => {\n if (num === undefined || num === '') {\n return\n }\n\n if (typeof num === 'string') {\n return Number(num)\n }\n\n return num\n}\n","import { OptionSelect, RenderProps } from '@/modules/builder/interfaces/elements/types'\nimport useApiRequest from '@/modules/builder/hooks/useApiRequest'\nimport { useCallback, useEffect } from 'react'\nimport { getProp, isArrayEmpty } from '@/modules/core/utils'\nimport { getValueFromPath } from '@/modules/builder/utils'\nimport { getDataFromPath } from '@/modules/builder/utils/index'\nimport { ApiRequestConfig } from '@/modules/builder/interfaces/general'\nimport { Option } from '@/modules/builder/interfaces/elements/components'\n\ninterface Props {\n renderProps?: RenderProps\n api?: ApiRequestConfig\n options?: Array<OptionSelect>\n}\n\nconst useSelectApiRequest = ({ api, renderProps, options }: Props) => {\n const { fetchData, loading, response } = useApiRequest()\n\n const parseOptions = (renderProps?: RenderProps): Array<Option> => {\n if (loading || !renderProps || !response) {\n return []\n }\n\n const data: Array<any> = getDataFromPath(\n getProp(response, 'data'),\n renderProps.value\n )\n\n if (!isArrayEmpty(data)) {\n return data.map((item) => ({\n ...(renderProps.includeAll ? { ...item } : {}),\n label: getValueFromPath(item, renderProps.label),\n value: getValueFromPath(item, renderProps.value),\n }))\n }\n\n return []\n }\n\n const launch = useCallback(() => {\n if (api) {\n fetchData(api)\n }\n }, [api, fetchData])\n\n useEffect(() => {\n launch()\n }, [launch])\n\n const optionsList =\n (isArrayEmpty(options) ? parseOptions(renderProps) : options) || []\n\n return { optionsList }\n}\n\nexport default useSelectApiRequest\n","import React from 'react'\nimport { uuidv4 } from '@/modules/core/utils'\nimport { AutocompleteFieldWrapper } from '@/modules/core/components/shared/AutocompleteField/styles'\nimport TextField from '@mui/material/TextField'\nimport Autocomplete from '@mui/material/Autocomplete'\n\nimport {\n ElementProps,\n FormHandlerProps,\n OptionSelect,\n} from '@/modules/builder/interfaces/elements/types'\nimport useSelectApiRequest from '@/modules/builder/hooks/useSelectApiRequest'\n\ninterface Props extends Omit<ElementProps, 'value'> {\n name: string\n value?: OptionSelect | OptionSelect[] | null\n formHandler?: FormHandlerProps\n options: Array<OptionSelect>\n}\n\nconst AutocompleteField: React.FC<Props> = React.forwardRef(function Field(\n {\n name = uuidv4(),\n label,\n value = null,\n options = [],\n onChange,\n formHandler,\n error,\n helperText,\n renderProps,\n required,\n api,\n multiple,\n ...props\n },\n ref\n) {\n // const [inputValue, setInputValue] = React.useState('')\n const { optionsList } = useSelectApiRequest({\n renderProps,\n api,\n options,\n })\n\n // const selectedValue = multiple ? optionsList.filter(item => item?.value ===)\n // optionsList.find((item) => item?.value === value?.value) || null\n\n const selectedValue = multiple\n ? optionsList.filter((item) =>\n (value as OptionSelect[])?.some(\n (selectedItem: any) => selectedItem.value === item.value\n )\n )\n : optionsList.find(\n (item) => item?.value === (value as OptionSelect)?.value\n ) || null\n\n return (\n <AutocompleteFieldWrapper>\n <Autocomplete\n ref={ref}\n id={name || uuidv4()}\n value={selectedValue}\n multiple={multiple}\n onChange={(event: any, newValue: any) => {\n typeof onChange === 'function' &&\n onChange({ target: { name, value: newValue } })\n }}\n // inputValue={inputValue}\n // onInputChange={(event, newInputValue) => {\n // setInputValue(newInputValue)\n // }}\n options={optionsList}\n getOptionLabel={(option) => option?.label || ''}\n isOptionEqualToValue={(option, value) => option?.value === value?.value}\n renderInput={(params) => (\n <TextField\n {...params}\n label={label}\n helperText={helperText}\n error={error}\n />\n )}\n fullWidth\n {...props}\n />\n </AutocompleteFieldWrapper>\n )\n})\n\nexport default React.memo(AutocompleteField)\n","import AutocompleteField from '@/modules/core/components/shared/AutocompleteField/AutocompleteField'\n\nexport default AutocompleteField\n"]}