File size: 6,688 Bytes
9697ede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import {
  createContext,
  createEffect,
  createSignal,
  createUniqueId,
  onCleanup,
  onMount,
  splitProps,
  useContext,
  Show,
  type ComponentProps,
  type ParentProps,
} from "solid-js"
import { TooltipV2 } from "./tooltip-v2"
import "./field-v2.css"

type FieldContextValue = {
  controlId: string
  labelId: string
  prefixId: string
  suffixId: string
  invalid: () => boolean
  registerPrefix: () => void
  unregisterPrefix: () => void
  registerSuffix: () => void
  unregisterSuffix: () => void
  getDescribedBy: () => string | undefined
}

const FieldContext = createContext<FieldContextValue>()

function useField() {
  const ctx = useContext(FieldContext)
  if (!ctx) {
    throw new Error("Field subcomponents must be used within <Field>")
  }
  return ctx
}

const CONTROL_SELECTOR = [
  "[data-slot='text-input-v2-input']",
  "[data-slot='textarea-v2-textarea']",
  "[data-slot='inline-input-v2-input']",
].join(", ")

export interface FieldV2Props extends ComponentProps<"div"> {
  invalid?: boolean
}

function FieldV2Root(props: ParentProps<FieldV2Props>) {
  const [local, rest] = splitProps(props, ["invalid", "class", "classList", "children"])

  const controlId = `field-control-${createUniqueId()}`
  const labelId = `field-label-${createUniqueId()}`
  const prefixId = `field-prefix-${createUniqueId()}`
  const suffixId = `field-suffix-${createUniqueId()}`

  const [prefixCount, setPrefixCount] = createSignal(0)
  const [suffixCount, setSuffixCount] = createSignal(0)

  let rootRef: HTMLDivElement | undefined

  const ctx: FieldContextValue = {
    controlId,
    labelId,
    prefixId,
    suffixId,
    invalid: () => !!local.invalid,
    registerPrefix: () => setPrefixCount((n) => n + 1),
    unregisterPrefix: () => setPrefixCount((n) => Math.max(0, n - 1)),
    registerSuffix: () => setSuffixCount((n) => n + 1),
    unregisterSuffix: () => setSuffixCount((n) => Math.max(0, n - 1)),
    getDescribedBy: () => {
      const ids: string[] = []
      if (prefixCount() > 0) ids.push(prefixId)
      if (suffixCount() > 0) ids.push(suffixId)
      return ids.length > 0 ? ids.join(" ") : undefined
    },
  }

  const syncControlA11y = () => {
    const root = rootRef
    if (!root) return

    const control = root.querySelector(CONTROL_SELECTOR) as HTMLInputElement | HTMLTextAreaElement | null
    if (!control) return

    const shell = control.closest(
      "[data-component='text-input-v2'], [data-component='textarea-v2'], [data-component='inline-input-v2']",
    ) as HTMLElement | null

    control.id = controlId
    control.setAttribute("aria-labelledby", labelId)

    const describedBy = ctx.getDescribedBy()
    if (describedBy) {
      control.setAttribute("aria-describedby", describedBy)
    } else {
      control.removeAttribute("aria-describedby")
    }

    if (ctx.invalid()) {
      control.setAttribute("aria-invalid", "true")
      shell?.setAttribute("data-invalid", "")
    } else {
      control.removeAttribute("aria-invalid")
      shell?.removeAttribute("data-invalid")
    }
  }

  onMount(() => {
    syncControlA11y()
  })

  createEffect(() => {
    prefixCount()
    suffixCount()
    local.invalid
    syncControlA11y()
  })

  return (
    <FieldContext.Provider value={ctx}>
      <div
        {...rest}
        ref={rootRef}
        data-component="field-v2"
        data-invalid={local.invalid ? "" : undefined}
        classList={{
          ...local.classList,
          [local.class ?? ""]: !!local.class,
        }}
      >
        {local.children}
      </div>
    </FieldContext.Provider>
  )
}

function FieldLabelInfoIcon() {
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
      <path
        fill-rule="evenodd"
        clip-rule="evenodd"
        d="M13 13H3V3H13V13ZM6.46777 6.81641V7.81641H7.5791V11.3721H8.5791V6.81641H6.46777ZM7.30078 4.62891V5.62891H8.85645V4.62891H7.30078Z"
        fill="currentColor"
      />
    </svg>
  )
}

export interface FieldLabelProps extends ComponentProps<"label"> {
  /** When set, shows the info icon with a tooltip containing this text. */
  tooltip?: string
}

function FieldLabel(props: ParentProps<FieldLabelProps>) {
  const [local, rest] = splitProps(props, ["class", "classList", "children", "tooltip"])
  const field = useField()

  return (
    <label
      {...rest}
      id={field.labelId}
      for={field.controlId}
      data-slot="field-v2-label"
      classList={{
        ...local.classList,
        [local.class ?? ""]: !!local.class,
      }}
    >
      <span data-slot="field-v2-label-text">{local.children}</span>
      <Show when={local.tooltip}>
        {(tooltip) => (
          <TooltipV2 value={tooltip()}>
            <button
              type="button"
              data-slot="field-v2-label-info"
              aria-label={tooltip()}
              onClick={(e) => e.stopPropagation()}
            >
              <FieldLabelInfoIcon />
            </button>
          </TooltipV2>
        )}
      </Show>
    </label>
  )
}

function FieldPrefix(props: ParentProps<ComponentProps<"div">>) {
  const [local, rest] = splitProps(props, ["class", "classList", "children"])
  const field = useField()

  onMount(() => {
    field.registerPrefix()
    onCleanup(() => field.unregisterPrefix())
  })

  return (
    <div
      {...rest}
      id={field.prefixId}
      data-slot="field-v2-prefix"
      classList={{
        ...local.classList,
        [local.class ?? ""]: !!local.class,
      }}
    >
      {local.children}
    </div>
  )
}

function FieldSuffix(props: ParentProps<ComponentProps<"div">>) {
  const [local, rest] = splitProps(props, ["class", "classList", "children"])
  const field = useField()

  onMount(() => {
    field.registerSuffix()
    onCleanup(() => field.unregisterSuffix())
  })

  return (
    <div
      {...rest}
      id={field.suffixId}
      data-slot="field-v2-suffix"
      classList={{
        ...local.classList,
        [local.class ?? ""]: !!local.class,
      }}
    >
      {local.children}
    </div>
  )
}

/** Optional layout wrapper around the control. */
function FieldControl(props: ParentProps<ComponentProps<"div">>) {
  const [local, rest] = splitProps(props, ["class", "classList", "children"])

  return (
    <div
      {...rest}
      data-slot="field-v2-control"
      classList={{
        ...local.classList,
        [local.class ?? ""]: !!local.class,
      }}
    >
      {local.children}
    </div>
  )
}

export const FieldV2 = Object.assign(FieldV2Root, {
  Label: FieldLabel,
  Prefix: FieldPrefix,
  Suffix: FieldSuffix,
  Control: FieldControl,
})

export const Field = FieldV2