@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
1 lines • 5.46 kB
Source Map (JSON)
{"version":3,"file":"use-native-select.cjs","names":["useFieldProps","computedChildren: ReactNode","rest","items","index","label","computedChildren","getRootProps: PropGetter","props","mergeRefs"],"sources":["../../../../src/components/native-select/use-native-select.tsx"],"sourcesContent":["\"use client\"\n\nimport type { ReactNode } from \"react\"\nimport type { HTMLProps, PropGetter } from \"../../core\"\nimport type { FieldProps } from \"../field\"\nimport { cloneElement, useCallback, useMemo } from \"react\"\nimport { ariaAttr, handlerAll, mergeRefs } from \"../../utils\"\nimport { useFieldProps } from \"../field\"\n\ninterface NativeSelectSharedItem\n extends Omit<HTMLProps<\"option\">, \"children\" | \"label\" | \"value\"> {\n label: string\n}\n\ninterface NativeSelectItemWithValue extends NativeSelectSharedItem {\n value?: HTMLProps<\"option\">[\"value\"]\n}\n\ninterface NativeSelectItemWithItems extends NativeSelectSharedItem {\n items: NativeSelectItemWithValue[]\n}\n\nexport type NativeSelectItem =\n | NativeSelectItemWithItems\n | NativeSelectItemWithValue\n\nexport interface UseNativeSelectProps extends HTMLProps<\"select\">, FieldProps {\n /**\n * If `true`, include placeholder in options.\n *\n * @default true\n */\n includePlaceholder?: boolean\n /**\n * If provided, generate options based on items.\n *\n * @default []\n */\n items?: NativeSelectItem[]\n /**\n * The placeholder for select.\n */\n placeholder?: string\n}\n\nexport const useNativeSelect = (props: UseNativeSelectProps = {}) => {\n const {\n props: {\n children,\n disabled,\n includePlaceholder = true,\n items = [],\n placeholder,\n readOnly,\n ...rest\n },\n ariaProps,\n dataProps,\n eventProps,\n } = useFieldProps(props)\n const interactive = !(readOnly || disabled)\n\n const computedChildren = useMemo(() => {\n let computedChildren: ReactNode = null\n\n if (children) {\n computedChildren = children\n } else if (items.length) {\n computedChildren = items.map((item, index) => {\n if (\"items\" in item) {\n const { items = [], label, ...rest } = item\n\n return cloneElement(<optgroup />, {\n key: index,\n children: items.map(({ label, ...rest }, index) =>\n cloneElement(<option />, {\n key: index,\n children: label,\n ...rest,\n }),\n ),\n label,\n ...rest,\n })\n } else {\n const { label, ...rest } = item\n\n return cloneElement(<option />, {\n key: index,\n children: label,\n ...rest,\n })\n }\n })\n }\n\n return (\n <>\n {placeholder ? (\n <option hidden={!includePlaceholder} value=\"\">\n {placeholder}\n </option>\n ) : null}\n\n {computedChildren}\n </>\n )\n }, [children, items, placeholder, includePlaceholder])\n\n const getRootProps: PropGetter = useCallback(\n (props) => ({ ...dataProps, ...props }),\n [dataProps],\n )\n\n const getFieldProps: PropGetter<\"select\"> = useCallback(\n ({ ref, ...props } = {}) => ({\n disabled,\n readOnly,\n tabIndex: interactive ? undefined : -1,\n ...ariaProps,\n ...dataProps,\n \"aria-disabled\": ariaAttr(!interactive),\n \"aria-label\": placeholder,\n ...rest,\n ...props,\n ref: mergeRefs(ref, rest.ref),\n children: props.children ?? computedChildren,\n onBlur: handlerAll(eventProps.onBlur, props.onBlur),\n onFocus: handlerAll(eventProps.onFocus, props.onFocus),\n }),\n [\n interactive,\n ariaProps,\n computedChildren,\n dataProps,\n eventProps,\n rest,\n placeholder,\n disabled,\n readOnly,\n ],\n )\n\n const getIconProps: PropGetter = useCallback(\n (props) => ({ ...dataProps, ...props }),\n [dataProps],\n )\n\n return { getFieldProps, getIconProps, getRootProps }\n}\n\nexport type UseNativeSelectReturn = ReturnType<typeof useNativeSelect>\n"],"mappings":";;;;;;;;;;;;;AA6CA,MAAa,mBAAmB,QAA8B,EAAE,KAAK;CACnE,MAAM,EACJ,OAAO,EACL,UACA,UACA,qBAAqB,MACrB,QAAQ,EAAE,EACV,aACA,SACA,GAAG,QAEL,WACA,WACA,eACEA,sCAAc,MAAM;CACxB,MAAM,cAAc,EAAE,YAAY;CAElC,MAAM,4CAAiC;EACrC,IAAIC,qBAA8B;AAElC,MAAI,SACF,sBAAmB;WACV,MAAM,OACf,sBAAmB,MAAM,KAAK,MAAM,UAAU;AAC5C,OAAI,WAAW,MAAM;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