Skip to content

Commit

Permalink
docs(Search): remove imports of other examples (Semantic-Org#3494)
Browse files Browse the repository at this point in the history
* docs(Search): Implement own standard custom.

* docs(Search): Implement own category custom example.

* docs(Search): Implement own example for fluid search.

* docs(Search): Implement own example for aligned search.

* docs(Search): Implement own example for input search and fix typo.
  • Loading branch information
Fabianopb authored and mbakiev committed Jun 17, 2019
1 parent 551483f commit f297649
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 28 deletions.
104 changes: 96 additions & 8 deletions docs/src/examples/modules/Search/Types/SearchExampleCategoryCustom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import PropTypes from 'prop-types'
import React from 'react'
import { Label } from 'semantic-ui-react'

import SearchExampleCategory from './SearchExampleCategory'
import _ from 'lodash'
import faker from 'faker'
import React, { Component } from 'react'
import { Search, Grid, Header, Segment, Label } from 'semantic-ui-react'

const categoryRenderer = ({ name }) => <Label as={'span'} content={name} />

Expand All @@ -17,8 +17,96 @@ resultRenderer.propTypes = {
description: PropTypes.string,
}

const SearchExampleCategoryCustom = () => (
<SearchExampleCategory categoryRenderer={categoryRenderer} resultRenderer={resultRenderer} />
)
const getResults = () =>
_.times(5, () => ({
title: faker.company.companyName(),
description: faker.company.catchPhrase(),
image: faker.internet.avatar(),
price: faker.finance.amount(0, 100, 2, '$'),
}))

const source = _.range(0, 3).reduce((memo) => {
const name = faker.hacker.noun()

// eslint-disable-next-line no-param-reassign
memo[name] = {
name,
results: getResults(),
}

return memo
}, {})

export default class SearchExampleCategory extends Component {
componentWillMount() {
this.resetComponent()
}

resetComponent = () =>
this.setState({ isLoading: false, results: [], value: '' })

handleResultSelect = (e, { result }) => this.setState({ value: result.title })

handleSearchChange = (e, { value }) => {
this.setState({ isLoading: true, value })

export default SearchExampleCategoryCustom
setTimeout(() => {
if (this.state.value.length < 1) return this.resetComponent()

const re = new RegExp(_.escapeRegExp(this.state.value), 'i')
const isMatch = result => re.test(result.title)

const filteredResults = _.reduce(
source,
(memo, data, name) => {
const results = _.filter(data.results, isMatch)
if (results.length) memo[name] = { name, results } // eslint-disable-line no-param-reassign

return memo
},
{},
)

this.setState({
isLoading: false,
results: filteredResults,
})
}, 300)
}

render() {
const { isLoading, value, results } = this.state

return (
<Grid>
<Grid.Column width={8}>
<Search
category
categoryRenderer={categoryRenderer}
loading={isLoading}
onResultSelect={this.handleResultSelect}
onSearchChange={_.debounce(this.handleSearchChange, 500, {
leading: true,
})}
resultRenderer={resultRenderer}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={8}>
<Segment>
<Header>State</Header>
<pre style={{ overflowX: 'auto' }}>
{JSON.stringify(this.state, null, 2)}
</pre>
<Header>Options</Header>
<pre style={{ overflowX: 'auto' }}>
{JSON.stringify(source, null, 2)}
</pre>
</Segment>
</Grid.Column>
</Grid>
)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import PropTypes from 'prop-types'
import React from 'react'
import { Label } from 'semantic-ui-react'
import _ from 'lodash'
import faker from 'faker'
import React, { Component } from 'react'
import { Search, Grid, Header, Segment, Label } from 'semantic-ui-react'

import SearchExampleStandard from './SearchExampleStandard'
const source = _.times(5, () => ({
title: faker.company.companyName(),
description: faker.company.catchPhrase(),
image: faker.internet.avatar(),
price: faker.finance.amount(0, 100, 2, '$'),
}))

const resultRenderer = ({ title }) => <Label content={title} />

Expand All @@ -11,6 +18,63 @@ resultRenderer.propTypes = {
description: PropTypes.string,
}

const SearchExampleStandardCustom = () => <SearchExampleStandard resultRenderer={resultRenderer} />
export default class SearchExampleStandard extends Component {
componentWillMount() {
this.resetComponent()
}

export default SearchExampleStandardCustom
resetComponent = () =>
this.setState({ isLoading: false, results: [], value: '' })

handleResultSelect = (e, { result }) => this.setState({ value: result.title })

handleSearchChange = (e, { value }) => {
this.setState({ isLoading: true, value })

setTimeout(() => {
if (this.state.value.length < 1) return this.resetComponent()

const re = new RegExp(_.escapeRegExp(this.state.value), 'i')
const isMatch = result => re.test(result.title)

this.setState({
isLoading: false,
results: _.filter(source, isMatch),
})
}, 300)
}

render() {
const { isLoading, value, results } = this.state

return (
<Grid>
<Grid.Column width={6}>
<Search
loading={isLoading}
onResultSelect={this.handleResultSelect}
onSearchChange={_.debounce(this.handleSearchChange, 500, {
leading: true,
})}
results={results}
value={value}
resultRenderer={resultRenderer}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={10}>
<Segment>
<Header>State</Header>
<pre style={{ overflowX: 'auto' }}>
{JSON.stringify(this.state, null, 2)}
</pre>
<Header>Options</Header>
<pre style={{ overflowX: 'auto' }}>
{JSON.stringify(source, null, 2)}
</pre>
</Segment>
</Grid.Column>
</Grid>
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
import React from 'react'
import SearchExampleStandard from '../Types/SearchExampleStandard'
import _ from 'lodash'
import faker from 'faker'
import React, { Component } from 'react'
import { Search, Grid, Header, Segment } from 'semantic-ui-react'

const SearchExampleAligned = () => <SearchExampleStandard aligned='right' />
const source = _.times(5, () => ({
title: faker.company.companyName(),
description: faker.company.catchPhrase(),
image: faker.internet.avatar(),
price: faker.finance.amount(0, 100, 2, '$'),
}))

export default SearchExampleAligned
export default class SearchExampleStandard extends Component {
componentWillMount() {
this.resetComponent()
}

resetComponent = () =>
this.setState({ isLoading: false, results: [], value: '' })

handleResultSelect = (e, { result }) => this.setState({ value: result.title })

handleSearchChange = (e, { value }) => {
this.setState({ isLoading: true, value })

setTimeout(() => {
if (this.state.value.length < 1) return this.resetComponent()

const re = new RegExp(_.escapeRegExp(this.state.value), 'i')
const isMatch = result => re.test(result.title)

this.setState({
isLoading: false,
results: _.filter(source, isMatch),
})
}, 300)
}

render() {
const { isLoading, value, results } = this.state

return (
<Grid>
<Grid.Column width={6}>
<Search
aligned='right'
loading={isLoading}
onResultSelect={this.handleResultSelect}
onSearchChange={_.debounce(this.handleSearchChange, 500, {
leading: true,
})}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={10}>
<Segment>
<Header>State</Header>
<pre style={{ overflowX: 'auto' }}>
{JSON.stringify(this.state, null, 2)}
</pre>
<Header>Options</Header>
<pre style={{ overflowX: 'auto' }}>
{JSON.stringify(source, null, 2)}
</pre>
</Segment>
</Grid.Column>
</Grid>
)
}
}
74 changes: 70 additions & 4 deletions docs/src/examples/modules/Search/Variations/SearchExampleFluid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
import React from 'react'
import SearchExampleStandard from '../Types/SearchExampleStandard'
import _ from 'lodash'
import faker from 'faker'
import React, { Component } from 'react'
import { Search, Grid, Header, Segment } from 'semantic-ui-react'

const SearchExampleFluid = () => <SearchExampleStandard fluid />
const source = _.times(5, () => ({
title: faker.company.companyName(),
description: faker.company.catchPhrase(),
image: faker.internet.avatar(),
price: faker.finance.amount(0, 100, 2, '$'),
}))

export default SearchExampleFluid
export default class SearchExampleStandard extends Component {
componentWillMount() {
this.resetComponent()
}

resetComponent = () =>
this.setState({ isLoading: false, results: [], value: '' })

handleResultSelect = (e, { result }) => this.setState({ value: result.title })

handleSearchChange = (e, { value }) => {
this.setState({ isLoading: true, value })

setTimeout(() => {
if (this.state.value.length < 1) return this.resetComponent()

const re = new RegExp(_.escapeRegExp(this.state.value), 'i')
const isMatch = result => re.test(result.title)

this.setState({
isLoading: false,
results: _.filter(source, isMatch),
})
}, 300)
}

render() {
const { isLoading, value, results } = this.state

return (
<Grid>
<Grid.Column width={6}>
<Search
fluid
loading={isLoading}
onResultSelect={this.handleResultSelect}
onSearchChange={_.debounce(this.handleSearchChange, 500, {
leading: true,
})}
results={results}
value={value}
{...this.props}
/>
</Grid.Column>
<Grid.Column width={10}>
<Segment>
<Header>State</Header>
<pre style={{ overflowX: 'auto' }}>
{JSON.stringify(this.state, null, 2)}
</pre>
<Header>Options</Header>
<pre style={{ overflowX: 'auto' }}>
{JSON.stringify(source, null, 2)}
</pre>
</Segment>
</Grid.Column>
</Grid>
)
}
}
Loading

0 comments on commit f297649

Please sign in to comment.