<commit_before>import React from 'react'
import classNames from 'classnames'
import {FOCUS_TERMINATOR} from '@sanity/util/paths'
import {isValidationMarker, Marker, Path} from '@sanity/types'
import defaultStyles from 'part:@sanity/components/fieldsets/default-style'
import ArrowDropDown from 'part:@sanity/base/arrow-drop-down'
import DefaultLabel from 'part:@sanity/components/labels/default'
import ValidationStatus from 'part:@sanity/components/validation/status'
import {ChangeIndicator, ChangeIndicatorContextProvidedProps} from '../../../../change-indicators'
import {FieldPresence, FormFieldPresence} from '../../../../presence'
import FieldStatus from './FieldStatus'

const EMPTY_ARRAY = []

interface FieldsetProps {
  description?: string
  legend: string
  columns?: number
  isCollapsible?: boolean
  onFocus?: (path: Path) => void
  isCollapsed?: boolean
  fieldset: {
    description?: string
    legend?: string
  }
  children?: React.ReactNode
  level?: number
  className?: string
  tabIndex?: number
  transparent?: boolean
  styles?: Record<string, string>
  markers?: Marker[]
  presence: FormFieldPresence[]
  changeIndicator: ChangeIndicatorContextProvidedProps | boolean
}

interface State {
  isCollapsed: boolean
  hasBeenToggled: boolean
}

export default class Fieldset extends React.PureComponent<FieldsetProps, State> {
  _focusElement: HTMLDivElement | null = null

  static defaultProps = {
    children: undefined,
    className: '',
    columns: undefined,
    description: undefined,
    level: 1,
    fieldset: {},
    isCollapsed: false,
    isCollapsible: false, // can collapsing be toggled by user?
    markers: EMPTY_ARRAY,
    onFocus: undefined,
    styles: undefined,
    tabIndex: undefined,
    transparent: undefined,
    changeIndicator: true,
    presence: EMPTY_ARRAY,
  }

  constructor(props: FieldsetProps) {
    super(props)

    this.state = {
      isCollapsed: props.isCollapsed || false,
      hasBeenToggled: false,
    }
  }

  handleToggle = () => {
    this.setState((prevState) => ({
      isCollapsed: !prevState.isCollapsed,
      hasBeenToggled: true,
    }))

    // Let parent know field has been toggled
    const {onFocus} = this.props
    if (onFocus) {
      onFocus([FOCUS_TERMINATOR])
    }
  }

  handleFocus = (event: React.FocusEvent<HTMLDivElement>) => {
    // Make sure we don't trigger onFocus for child elements
    if (event.target === this._focusElement) {
      if (this.props.onFocus) this.props.onFocus([FOCUS_TERMINATOR])
    }
  }<commit_msg>[base] Don't call onFocus when toggling fieldsets (#2193)<commit_after>import React from 'react'
import classNames from 'classnames'
import {FOCUS_TERMINATOR} from '@sanity/util/paths'
import {isValidationMarker, Marker, Path} from '@sanity/types'
import defaultStyles from 'part:@sanity/components/fieldsets/default-style'
import ArrowDropDown from 'part:@sanity/base/arrow-drop-down'
import DefaultLabel from 'part:@sanity/components/labels/default'
import ValidationStatus from 'part:@sanity/components/validation/status'
import {ChangeIndicator, ChangeIndicatorContextProvidedProps} from '../../../../change-indicators'
import {FieldPresence, FormFieldPresence} from '../../../../presence'
import FieldStatus from './FieldStatus'

const EMPTY_ARRAY = []

interface FieldsetProps {
  description?: string
  legend: string
  columns?: number
  isCollapsible?: boolean
  onFocus?: (path: Path) => void
  isCollapsed?: boolean
  fieldset: {
    description?: string
    legend?: string
  }
  children?: React.ReactNode
  level?: number
  className?: string
  tabIndex?: number
  transparent?: boolean
  styles?: Record<string, string>
  markers?: Marker[]
  presence: FormFieldPresence[]
  changeIndicator: ChangeIndicatorContextProvidedProps | boolean
}

interface State {
  isCollapsed: boolean
  hasBeenToggled: boolean
}

export default class Fieldset extends React.PureComponent<FieldsetProps, State> {
  _focusElement: HTMLDivElement | null = null

  static defaultProps = {
    children: undefined,
    className: '',
    columns: undefined,
    description: undefined,
    level: 1,
    fieldset: {},
    isCollapsed: false,
    isCollapsible: false, // can collapsing be toggled by user?
    markers: EMPTY_ARRAY,
    onFocus: undefined,
    styles: undefined,
    tabIndex: undefined,
    transparent: undefined,
    changeIndicator: true,
    presence: EMPTY_ARRAY,
  }

  constructor(props: FieldsetProps) {
    super(props)

    this.state = {
      isCollapsed: props.isCollapsed || false,
      hasBeenToggled: false,
    }
  }

  handleToggle = () => {
    this.setState((prevState) => ({
      isCollapsed: !prevState.isCollapsed,
      hasBeenToggled: true,
    }))
  }

  handleFocus = (event: React.FocusEvent<HTMLDivElement>) => {
    // Make sure we don't trigger onFocus for child elements
    if (event.target === this._focusElement) {
      if (this.props.onFocus) this.props.onFocus([FOCUS_TERMINATOR])
    }
  }
{'fraction_non_alphanumeric': 0.11305572319452827, 'fraction_numerical': 0.0012070006035003018, 'mean_word_length': 3.986960882647944, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5939937', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1495, 'n_words': 374}