Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inputComponent: ConditionalFields breaks hotspot #14

Closed
dnlmzw opened this issue May 27, 2021 · 3 comments
Closed

inputComponent: ConditionalFields breaks hotspot #14

dnlmzw opened this issue May 27, 2021 · 3 comments
Labels
needs info Waiting on more info to confirm issue.

Comments

@dnlmzw
Copy link

dnlmzw commented May 27, 2021

Hi there,
Although this isn't the core to what you made, I still thought I'd want to mention that hotspot on image assets stops working, when using your ConditionalFields script.

@ndimatteo
Copy link
Owner

Hey there @dnlmzw !

Can you let me know what you mean?

Are the hotspot options not showing up for figure fields used in conditional fields? Or are you not seeing hotspots being applied on the front-end? Or something else?

Just a few preliminary comments:

  1. If you are adding new image fields you'll need to use the imageMeta GROQ from the api to ensure the hotspot data makes it into the frontend.
  2. If you have that in place are you able to log the photo data somewhere to see if your hotspot data is coming through?
  3. If the hotspot options aren't showing up in your Studio, this tells me it's an issue with the custom Conditional Field plugin, which was adapted from here: https://gist.github.com/coxmi/f41aef54d46588fff27651cd0d35212f

@ndimatteo ndimatteo added the needs info Waiting on more info to confirm issue. label May 28, 2021
@dnlmzw
Copy link
Author

dnlmzw commented May 31, 2021

Thanks! Option 3 is what's happening, so I'll head over there and see if there's anything to be done.

@dnlmzw dnlmzw closed this as completed May 31, 2021
@dnlmzw
Copy link
Author

dnlmzw commented Jun 9, 2021

For anyone else interested:

I took the one from @hdoro that can be found here and included the getContext bit from you script and the hotspot is now working.

import React from 'react'
import {
  withDocument,
  withValuePath,
  FormBuilderInput
} from 'part:@sanity/form-builder'

class ConditionalField extends React.PureComponent {
  fieldRef = React.createRef()

  focus() {
    if (this.fieldRef?.current) {
      this.fieldRef.current.focus()
    }
  }

  getContext(level = 1) {
    // gets value path from withValuePath HOC, and applies path to document
    // we remove the last 𝑥 elements from the valuePath

    const valuePath = this.props.getValuePath()
    const removeItems = -Math.abs(level)
    return valuePath.length + removeItems <= 0
      ? this.props.document
      : valuePath.slice(0, removeItems).reduce((context, current) => {
          // basic string path
          if (typeof current === 'string') {
            return context[current] || {}
          }

          // object path with key used on arrays
          if (
            typeof current === 'object' &&
            Array.isArray(context) &&
            current._key
          ) {
            return (
              context.filter(
                item => item._key && item._key === current._key
              )[0] || {}
            )
          }
        }, this.props.document)
  }

  render() {
    const {
      document,
      type,
      value,
      level,
      focusPath,
      onFocus,
      onBlur,
      onChange,
      getValuePath,
      markers = [],
      presence = [],
      compareValue
    } = this.props
    const shouldRenderField = type?.options?.condition
    const renderField = shouldRenderField
      ? shouldRenderField(document, this.getContext.bind(this))
      : true

    if (!renderField) {
      return <div style={{ marginBottom: '-32px' }} />
    }

    const { type: _unusedType, inputComponent, ...usableType } = type
    return (
      <FormBuilderInput
        level={level}
        type={usableType}
        value={value}
        onChange={patchEvent => onChange(patchEvent)}
        path={getValuePath()}
        focusPath={focusPath}
        onFocus={onFocus}
        onBlur={onBlur}
        ref={this.fieldRef}
        markers={markers}
        presence={presence}
        compareValue={compareValue}
      />
    )
  }
}

export default withValuePath(withDocument(ConditionalField))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs info Waiting on more info to confirm issue.
Projects
None yet
Development

No branches or pull requests

2 participants