Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const {Component, useState, useEffect, memo} = React;

const Title = memo(({size = 'medium', text}) => {
  switch(size) {
    case 'x-small':
      return <h6 class="title">{text}</h6>
    case 'small':
      return <h5 class="title">{text}</h5>
    case 'main':
      return <h1 class="title">{text}</h1>
    case 'large':
      return <h3 class="title">{text}</h3>
    case 'x-large':
      return <h2 class="title">{text}</h2>
    default:
      return <h4 class="title">{text}</h4>
  }
})

const InputField = memo(({
  type = "text", 
  value = "", 
  placeholder = "Placeholder", 
  onChange
}) => {
  const handleChange = e => {
    e.persist();
    onChange(e);
  };
  
  return (
    <input 
      type={type} 
      value={value} 
      placeholder={placeholder} 
      onChange={handleChange}/>
  )
})

const SearchField = memo(({onSearchTerm}) => {
  const [searchTerm, updateSearchTerm] = useState('');
  
  const onInput = ({target}) => {
    if(target.value.trim() !== searchTerm.trim()) {
      onSearchTerm(target.value.trim())
    }
    
    updateSearchTerm(target.value)
  };
  
  return (
    <InputField 
      onChange={onInput} 
      value={searchTerm}
      type="search"
      placeholder="Search..." />
  )
});

const List = memo(({list, renderItem, wrapperTag = '', renderWhenEmpty}) => {
  if(list instanceof Set) {
    list = Array.from(list)
  }
  
  if(list instanceof Map) {
    list = Array.from(list.values())
  }
  
  if(typeof list === 'object') {
    list = Object.values(list);
  }
  
  if(list.length === 0) {
    return renderWhenEmpty();
  }
  
  const itemList = list.map((item, index) => renderItem(item, index));
  
  if(wrapperTag) {
    const Tag = `${wrapperTag}`;
    
    return <Tag>{itemList}</Tag>;
  }
  
  return itemList;
});

const ListItem = memo(({label}) => (<li class="list-item">{label}</li>));

const SearchResultsView = memo(({results, onSearch}) => {
  let timer = null;
  let searchTerm = '';
  const renderNoResult = () => searchTerm.length ? 
    <p>Nothing Matched!</p> :
    <p></p>;
  const renderResult = (result) => 
    <ListItem key={result.id} label={result.name}/>;
  
  const debounceSearch = (value) => {
    clearTimeout(timer);
    searchTerm = value;
    timer = setTimeout(() => {
      if(value.length >= 3) {
        onSearch(value)
      }
    }, 350)
  }
  
  return (
    <section class="search-view">
      <Title text="Find Items"/>
      <SearchField onSearchTerm={debounceSearch}/>
      <List 
        list={results} 
        renderItem={renderResult} 
        renderWhenEmpty={renderNoResult} 
        wrapperTag="ul"
        />
    </section>
  )
})

const data = [
  {id: 1, name: 'Lorem ipsum dolor sit amet consectetur'},
  {id: 2, name: 'adipisicing elit. Accusamus perferendis'},
  {id: 3, name: 'iste earum, ipsum illo officiis nobis recusandae'},
  {id: 4, name: 'dicta hic laboriosam saepe lorem facere'},
  {id: 5, name: 'repellat tenetur voluptatem atque'},
  {id: 6, name: 'similique lorem officia nisi ad. Itaque'}
];

const fetchData = async (term) => 
  data.filter(dt => dt.name.search(new RegExp(term, 'i')) >= 0);

const withSearchData = Component => props => {
  const [searchResults, saveSearchResults] = useState([])
        
  const handleSearch = term => {
    if (term.trim().length) {
      fetchData(term)
        .then(res => {
          return saveSearchResults(res);
        })
    } 
  }
  
  return <Component {...props} onSearch={handleSearch} results={searchResults}/>
}

const SearchWithData = memo(withSearchData(SearchResultsView));

class App extends Component {
  render() {
    return (
      <>
        <h1>App</h1>
        <SearchWithData/>
      </>
    )
  }
}

ReactDOM.render(
  <App/>,
  document.getElementById('app')
)
              
            
!
999px

Console