MCPcopy
hub / github.com/shadcn-ui/ui / getStateAttributes

Function getStateAttributes

packages/react/src/use-render/index.ts:137–170  ·  view source on GitHub ↗
(
  state: TState,
  mapping?: StateAttributesMapping<TState>
)

Source from the content-addressed store, hash-verified

135}
136
137function getStateAttributes<TState extends RenderState>(
138 state: TState,
139 mapping?: StateAttributesMapping<TState>
140) {
141 const props: Record<string, unknown> = {}
142
143 for (const key of Object.keys(state) as Array<keyof TState>) {
144 const value = state[key]
145 const attributes = mapping?.[key]?.(value)
146
147 if (attributes) {
148 Object.assign(props, attributes)
149 continue
150 }
151
152 if (key === "slot") {
153 props["data-slot"] = value
154 continue
155 }
156
157 const attribute = `data-${String(key).replace(
158 /[A-Z]/g,
159 (letter) => `-${letter.toLowerCase()}`
160 )}`
161
162 if (typeof value === "boolean") {
163 props[attribute] = value ? "" : undefined
164 } else if (value !== null && value !== undefined) {
165 props[attribute] = String(value)
166 }
167 }
168
169 return props
170}
171
172type EventHandler = (event: React.SyntheticEvent) => void
173

Callers 1

useRenderFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected