๐Ÿ“ฆ payloadcms / website

๐Ÿ“„ index.tsx ยท 173 lines
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173'use client'

import { CopyToClipboard } from '@components/CopyToClipboard/index'
import { Tooltip } from '@components/Tooltip/index'
import Label from '@forms/Label/index'
import { EyeIcon } from '@root/icons/EyeIcon/index'
import React, { Fragment, useEffect } from 'react'

import type { FieldProps } from '../types'

import Error from '../../Error/index'
import { useField } from '../useField/index'
import classes from './index.module.scss'

export const Text: React.FC<
  {
    copy?: boolean
    customOnChange?: (e: any) => void
    elementAttributes?: React.InputHTMLAttributes<HTMLInputElement>
    readOnly?: boolean
    suffix?: React.ReactNode
    type?: 'hidden' | 'password' | 'text'
    value?: string
  } & FieldProps<string>
> = (props) => {
  const {
    name,
    type = 'text',
    className,
    copy = false,
    customOnChange,
    description,
    disabled,
    elementAttributes = {
      autoCapitalize: 'none',
      autoComplete: 'off',
      autoCorrect: 'off',
    },
    fullWidth = true,
    icon,
    initialValue,
    label,
    onChange: onChangeFromProps,
    path,
    placeholder,
    readOnly,
    required = false,
    showError: showErrorFromProps,
    suffix,
    validate,
    value: valueFromProps,
  } = props

  const prevValueFromProps = React.useRef(valueFromProps)

  const [isHidden, setIsHidden] = React.useState(type === 'password')

  const defaultValidateFunction = React.useCallback(
    (fieldValue: boolean): string | true => {
      if (required && !fieldValue) {
        return 'Please enter a value.'
      }

      if (fieldValue && typeof fieldValue !== 'string') {
        return 'This field can only be a string.'
      }

      return true
    },
    [required],
  )

  const {
    errorMessage,
    onChange,
    showError,
    value: valueFromContext,
  } = useField<string>({
    initialValue,
    onChange: onChangeFromProps,
    path,
    required,
    validate: validate || defaultValidateFunction,
  })

  const value = valueFromProps || valueFromContext

  useEffect(() => {
    if (
      valueFromProps !== undefined &&
      valueFromProps !== prevValueFromProps.current &&
      valueFromProps !== valueFromContext
    ) {
      prevValueFromProps.current = valueFromProps
      onChange(valueFromProps)
    }
  }, [valueFromProps, onChange, valueFromContext])

  return (
    <div
      className={[
        className,
        classes.component,
        (showError || showErrorFromProps) && classes.showError,
        classes[`type--${type}`],
        fullWidth && classes.fullWidth,
      ]
        .filter(Boolean)
        .join(' ')}
    >
      {/*
        This field is display flex in column-reverse, so the html structure is opposite of other fields
        This is so tabs go to the input before the label actions slot
      */}
      {description && <p className={classes.description}>{description}</p>}
      <div className={classes.inputWrap}>
        <input
          {...elementAttributes}
          className={classes.input}
          disabled={disabled}
          id={path}
          name={name ?? path}
          onChange={(e) => {
            const onChangeFunction = customOnChange ? customOnChange : onChange

            if (!disabled && !readOnly) {
              onChangeFunction(e.target.value)
            }
          }}
          placeholder={placeholder}
          readOnly={readOnly}
          type={type === 'password' && !isHidden ? 'text' : type}
          value={value || ''}
        />
        {(icon || suffix) && (
          <div className={classes.iconWrapper}>
            {suffix && <div className={classes.suffix}>{suffix}</div>}
            {icon && <div className={classes.icon}>{icon}</div>}
          </div>
        )}
      </div>
      {type !== 'hidden' && (
        <>
          <Error
            message={errorMessage}
            showError={Boolean((showError || showErrorFromProps) && errorMessage)}
          />
          <Label
            actionsSlot={
              <Fragment>
                {copy && <CopyToClipboard value={value} />}
                {type === 'password' && (
                  <Tooltip
                    className={classes.tooltipButton}
                    onClick={() => setIsHidden((h) => !h)}
                    text={isHidden ? 'show' : 'hide'}
                  >
                    <EyeIcon closed={isHidden} size="large" />
                  </Tooltip>
                )}
              </Fragment>
            }
            htmlFor={path}
            label={label}
            margin={false}
            required={required}
          />
        </>
      )}
    </div>
  )
}