import * as React from 'react'; import { themr, ThemedComponentClass } from '@friendsofreactjs/react-css-themr'; import { classNames } from '@shopify/react-utilities/styles'; import { isEqual } from 'lodash'; import { PICKER } from '../ThemeIdentifiers'; import Chip from '../Chip'; import { IAutoSuggestMethods, IItemList } from './Picker'; import * as baseTheme from './Picker.scss'; import Autosuggest from '../Autosuggest'; import Button from '../Button'; export interface IStateProps { chipListState: IItemList[]; suggestions: any[]; inputProps: any; value?: string; removable: boolean; multiSection?: any; reachedMax?: boolean; processingIds?: any[]; } export interface Props { theme?: any; placeholder?: string; autoSuggestMethods?: IAutoSuggestMethods; stateProps?: IStateProps; handleInputFocus?: any; } interface State { visibleCount: number; showAll: boolean; } class AutoSuggestText extends React.PureComponent { containerRef: React.RefObject; rafId: number | null = null; lastKnownWidth = -1; constructor(props: Props) { super(props); this.containerRef = React.createRef(); this.state = { visibleCount: this.props?.stateProps?.chipListState.length || 0, showAll: false, }; } componentDidUpdate(prevProps: Props) { const chipList = this.props?.stateProps?.chipListState; const prevChipList = prevProps?.stateProps?.chipListState; const isCountSame = this.state.visibleCount===chipList?.length; if(!isCountSame && !isEqual(chipList, prevChipList)){ this.setState({ visibleCount: chipList?.length || 0, }); } if (isCountSame) { this.updateVisibleItems(); } } componentDidMount() { this.checkVisibilityLoop(); } componentWillUnmount() { if (this.rafId) cancelAnimationFrame(this.rafId); } checkVisibilityLoop = () => { const el = this.containerRef.current; if (el) { const width = el.offsetWidth; if (width !== this.lastKnownWidth) { this.lastKnownWidth = width; if (width > 0) { console.log('✅ Element is visible, width:', width); this.updateVisibleItems(); } else { console.log('⛔ Hidden or not rendered yet'); } } } this.rafId = requestAnimationFrame(this.checkVisibilityLoop); }; measureNodeWidth(node: HTMLElement): number { // Clone the node const clone = node.cloneNode(true) as HTMLElement; // Apply necessary styles clone.style.visibility = 'hidden'; clone.style.position = 'absolute'; clone.style.top = '-9999px'; clone.style.left = '-9999px'; clone.style.display = 'inline-block'; clone.style.whiteSpace = 'nowrap'; // Append to body, measure, then remove document.body.appendChild(clone); const width = clone.offsetWidth; document.body.removeChild(clone); return width; } updateVisibleItems = () => { const container = this.containerRef.current; if (!container) return; const children = Array.from(container.children); const chipList: any = this.props?.stateProps?.chipListState; const containerWidth = container.offsetWidth; let usedWidth = 0; let count = 0; for (let i = 0; i < chipList.length; i++) { const chip = children[i] as HTMLElement; if (!chip) break; const chipWidth = this.measureNodeWidth(chip) + 6; // margin const remaining = chipList.length - i - 1; const moreChipWidth = remaining > 0 ? 38 : 0; // approx width of "+X" chip if (usedWidth + chipWidth + moreChipWidth > containerWidth) break; usedWidth += chipWidth; count++; } this.setState({ visibleCount: count }); }; getFilteredSuggestions = (list= this.props?.stateProps?.suggestions || [], selectedList= this.props?.stateProps?.chipListState || []) => { const newSuggestions = list.filter((it: any) => { let isValid = false; selectedList.forEach((cls: any) => { isValid = isValid || it === cls; }); return !isValid; }); return newSuggestions; } render() { const { theme, stateProps }: any = this.props; const className = classNames( theme.containerWrapper, this.props.stateProps ? this.props.stateProps.chipListState.length ? null : theme.empty : null ); const chipList = this.props?.stateProps?.chipListState || []; const { visibleCount, showAll } = this.state; const visibleItems = chipList.slice(0, visibleCount); const hiddenCount = chipList.length - visibleCount; const isActive = this.props?.autoSuggestMethods?.getInputReference() === document.activeElement; const shouldRenderSuggestions = this.props?.autoSuggestMethods?.shouldRenderSuggestions || (() => { return false; }) as any; const processingIds:any = this.props?.stateProps?.processingIds || []; return (
{(showAll ? chipList : visibleItems).map((input: any) => this.props.autoSuggestMethods ? this.props.autoSuggestMethods.chipRemove(input) : null} key={input.key}> {input.icon && } ) } {hiddenCount > 0 && ( { e.stopPropagation(); this.setState(prevState => { return { ...prevState, showAll: !prevState.showAll } }); }} > ) }
{ !stateProps.reachedMax ? this.props.autoSuggestMethods && this.props.autoSuggestMethods.shouldRenderSuggestions ? { }) as any} getSectionSuggestions={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.getSectionSuggestions : (() => { }) as any} onSuggestionSelected={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.onSuggestionSelected : () => {} } suggestions={this.getFilteredSuggestions()} onSuggestionsFetchRequested={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.onSuggestionsFetchRequested : () => {}} onSuggestionsClearRequested={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.onSuggestionsClearRequested : (() => {}) as any} getSuggestionValue={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.getSuggestionValue : (() => {}) as any} // shouldRenderSuggestions use for open list on Focus Event shouldRenderSuggestions={shouldRenderSuggestions} renderSuggestion={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.renderSuggestion : (() => {}) as any} inputProps={this.props.stateProps ? this.props.stateProps.inputProps : null} renderSuggestionsContainer={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.renderSuggestionsContainer : (() => {}) as any} shouldKeepSuggestionsOnSelect={isActive ? shouldRenderSuggestions : () => { }} /> : { }) as any} getSectionSuggestions={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.getSectionSuggestions : (() => { }) as any} onSuggestionSelected={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.onSuggestionSelected : () => {} } suggestions={this.getFilteredSuggestions()} onSuggestionsFetchRequested={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.onSuggestionsFetchRequested : () => {}} onSuggestionsClearRequested={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.onSuggestionsClearRequested : (() => {}) as any} getSuggestionValue={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.getSuggestionValue : (() => {}) as any} renderSuggestion={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.renderSuggestion : (() => {}) as any} inputProps={this.props.stateProps ? this.props.stateProps.inputProps : null} renderSuggestionsContainer={this.props.autoSuggestMethods ? this.props.autoSuggestMethods.renderSuggestionsContainer : (() => {}) as any} /> : null }
); } } export default themr(PICKER, baseTheme)(AutoSuggestText) as ThemedComponentClass;