UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 5.4 kB
{"version":3,"file":"use-field-props.cjs","names":["useEnvironment","useFieldsetContext","useFieldContext"],"sources":["../../../../src/components/field/use-field-props.ts"],"sourcesContent":["\"use client\"\n\nimport type { FocusEventHandler } from \"react\"\nimport type { Dict } from \"../../utils\"\nimport type { FieldProps } from \"./field\"\nimport { useMemo, useState } from \"react\"\nimport { useEnvironment } from \"../../core\"\nimport {\n ariaAttr,\n cx,\n dataAttr,\n handlerAll,\n useSafeLayoutEffect,\n} from \"../../utils\"\nimport { useFieldsetContext } from \"../fieldset\"\nimport { useFieldContext } from \"./field\"\n\nexport interface UseFieldProps<Y extends HTMLElement> extends FieldProps {\n id?: string\n notSupportReadOnly?: boolean\n onBlur?: FocusEventHandler<Y>\n onFocus?: FocusEventHandler<Y>\n}\n\nexport const useFieldProps = <Y extends HTMLElement, M extends Dict>(\n {\n id,\n \"aria-describedby\": ariaDescribedby,\n disabled,\n invalid,\n notSupportReadOnly,\n readOnly,\n required,\n onBlur,\n onFocus,\n ...rest\n }: M & UseFieldProps<Y> = {} as M & UseFieldProps<Y>,\n) => {\n const { getDocument } = useEnvironment()\n const fieldsetContext = useFieldsetContext()\n const fieldContext = useFieldContext()\n const [errorMessageId, setErrorMessageId] = useState<string | undefined>(\n undefined,\n )\n const [helperMessageId, setHelperMessageId] = useState<string | undefined>(\n undefined,\n )\n\n id ??= fieldContext?.id\n disabled ??= fieldContext?.disabled ?? fieldsetContext?.disabled\n required ??= fieldContext?.required ?? fieldsetContext?.required\n readOnly ??= fieldContext?.readOnly ?? fieldsetContext?.readOnly\n invalid ??= fieldContext?.invalid ?? fieldsetContext?.invalid\n\n useSafeLayoutEffect(() => {\n const hasErrorMessage =\n !!fieldContext?.errorMessageId &&\n !!getDocument()?.getElementById(fieldContext.errorMessageId)\n const hasHelperMessage =\n !!fieldContext?.helperMessageId &&\n !!getDocument()?.getElementById(fieldContext.helperMessageId)\n\n setErrorMessageId(\n invalid && hasErrorMessage ? fieldContext.errorMessageId : undefined,\n )\n setHelperMessageId(\n (!fieldContext?.replace || !invalid) && hasHelperMessage\n ? fieldContext.helperMessageId\n : undefined,\n )\n }, [\n fieldContext?.errorMessageId,\n fieldContext?.helperMessageId,\n fieldContext?.replace,\n invalid,\n ])\n\n const props = useMemo(\n () => ({\n id,\n disabled,\n readOnly,\n required,\n ...rest,\n }),\n [id, disabled, readOnly, required, rest],\n )\n const dataProps = useMemo(\n () => ({\n \"data-disabled\": dataAttr(disabled),\n \"data-invalid\": dataAttr(invalid),\n \"data-readonly\": dataAttr(readOnly),\n \"data-required\": dataAttr(required),\n }),\n [disabled, invalid, readOnly, required],\n )\n const ariaProps = useMemo(\n () => ({\n \"aria-describedby\": cx(errorMessageId, helperMessageId, ariaDescribedby),\n \"aria-disabled\": ariaAttr(\n notSupportReadOnly ? readOnly || disabled : disabled,\n ),\n \"aria-invalid\": ariaAttr(invalid),\n \"aria-readonly\": notSupportReadOnly ? undefined : ariaAttr(readOnly),\n \"aria-required\": ariaAttr(required),\n }),\n [\n errorMessageId,\n helperMessageId,\n ariaDescribedby,\n notSupportReadOnly,\n readOnly,\n disabled,\n invalid,\n required,\n ],\n )\n const eventProps = useMemo(\n () => ({\n onBlur: handlerAll(fieldContext?.onBlur, onBlur),\n onFocus: handlerAll(fieldContext?.onFocus, onFocus),\n }),\n [fieldContext?.onBlur, fieldContext?.onFocus, onBlur, onFocus],\n )\n\n return {\n context: fieldContext,\n props,\n ariaProps,\n dataProps,\n eventProps,\n }\n}\n\nexport const resetFieldProps = {\n disabled: false,\n invalid: false,\n readOnly: false,\n required: false,\n}\n"],"mappings":";;;;;;;;;;;;;AAwBA,MAAa,iBACX,EACE,IACA,oBAAoB,iBACpB,UACA,SACA,oBACA,UACA,UACA,QACA,QACA,GAAG,SACqB,EAAE,KACzB;CACH,MAAM,EAAE,gBAAgBA,6CAAgB;CACxC,MAAM,kBAAkBC,qCAAoB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