# All files / src/_hooks TextEntry.tsx

94.28% Statements 33/35
83.33% Branches 5/6
90% Functions 9/10
91.66% Lines 22/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    1462x
    2x
    2x

    2x
    11x
    11x

    11x

    11x

    1x
    1x

    3x
    3x

    2x
    2x

    2x

    4x
    2x

    2x

    2x
    2x

    2x
    3x

     import { isEmpty } from 'ramda'
    import React from 'react'
    import { useBuffer } from './Buffer'

    /* TYPES */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export type ChangeEvent = React.ChangeEvent<HTMLInputElement>

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface TextEntryConfig {
      initialValue?: string
      onChange: (event: ChangeEvent) => void
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface TextEntryApi {
      clear: () => void
      empty: boolean
      onChange: (event: ChangeEvent) => void
      reset: () => void
      value: string
    }

    interface Props extends TextEntryConfig {
      children?: React.ReactNode
    }

    interface RenderProps extends TextEntryConfig {
      children: (props: TextEntryApi) => {}
    }

    /* HOOK */
    /**
     * @deprecatedSince 9.7.0
     * @deprecated A wrapper around useBuffer (deprecated) that would also call onChange. A trivial hook for storing a string. Write your own or use a hook utility lib.
     *
     * **Source:**
     * ```
    export function useTextEntry({
      initialValue = '',
      onChange = () => {},
    }) {
      const { value, update, clear, reset } = useBuffer({ initialValue })

      return {
        clear: () => {
          onChange({ target: { value: '' } })
          clear()
        },
        empty: Ramda.isEmpty(value),
        onChange: (event) => {
          onChange(event)
          update(event.target.value)
        },
        reset: () => {
          onChange({ target: { value } })
          reset()
        },
        value,
      }
    }
    ```
     */
    export function useTextEntry({
      initialValue = '',
      onChange = () => {},
    }: TextEntryConfig) {
      const { value, update, clear, reset } = useBuffer({ initialValue })

      return {
        clear: () => {
          onChange({ target: { value: '' } } as ChangeEvent)
          clear()
        },
        empty: isEmpty(value),
        onChange: (event: ChangeEvent) => {
          onChange(event)
          update(event.target.value)
        },
        reset: () => {
          onChange({ target: { value } } as ChangeEvent)
          reset()
        },
        value,
      }
    }

    /* RENDER PROP */
    /**
     * @deprecatedSince 9.7.0
     * @deprecated
     */
    export function TextEntry({ children, ...props }: RenderProps) {
      const textEntry = useTextEntry(props)

      return typeof children === 'function' ? children(textEntry) : children
    }

    /* CONTEXT */

    const noop = (method: string) => () =>
      console.warn(
        `Could not call \`${method}\`, please wrap your component in a \`<TextEntryProvider>\``
      )

    /**
     * @deprecatedSince 9.7.0
     * @deprecated
     */
    export const TextEntryContext = React.createContext({
      value: '',
      empty: true,
      clear: noop('clear'),
      reset: noop('reset'),
    })

    /**
     * @deprecatedSince 9.7.0
     * @deprecated
     */
    export function TextEntryProvider({ children, ...props }: Props) {
      return (
        <TextEntryContext.Provider value={useTextEntry(props)}>
          {children}
        </TextEntryContext.Provider>
      )
    }

    /**
     * @deprecatedSince 9.7.0
     * @deprecated
     */
    export function useTextEntryContext() {
      return React.useContext(TextEntryContext)
    }