Skip to content

Commit

Permalink
Merge pull request #84 from takenet/bugfix/no-results-found-texts
Browse files Browse the repository at this point in the history
fix(components): add 'no results text' adjustments
  • Loading branch information
samwx authored Nov 22, 2019
2 parents 8244761 + 084519b commit a335948
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
4 changes: 4 additions & 0 deletions sandbox/blipSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ const amazingSelect = document.getElementById('select')
const selectInstance = new BlipSelect({
onSelectOption: ($event) => console.log($event),
placeholderIcon: '<img src="https://png.pngtree.com/svg/20170418/work_155602.png" />',
mode: 'autocomplete',
descriptionPosition: 'bottom',
canAddOptions: {
text: 'Criar organização',
alwaysEnabled: true,
},
clearAfterAdd: false,
noResultsText: 'Nothing here',
noResultsFoundText: 'No results found',
onAddOption: ({ $event: { value, label, element } }) => console.log(value, label, element),
})

const select = selectInstance.render({
options: [
{
Expand Down
29 changes: 23 additions & 6 deletions src/components/blipSelect/CreatableOptionsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import { EventEmitter } from '@lib/eventEmitter'
import { renderEmptyOption } from '../shared'

export class CreatebleOptionsList extends Component {
$defaults = {
noResultsText: '',
noResultsFoundText: '',
}

constructor(options) {
super()

this.options = options
this.options = {
...this.$defaults,
...options,
}

this.props = {
options: [],
newOption: '',
addOptionText: '',
emptyMessage: '',
alwaysEnabled: false,
blockNewEntries: false,
OptionCreator: undefined,
Expand All @@ -29,6 +36,16 @@ export class CreatebleOptionsList extends Component {
...props,
}

const chooseEmptyOptionsMessage = () => {
if (this.props.options.length === 0 && (this.props.newOption === '' || this.props.newOption === undefined)) {
return this.options.noResultsText
} else if (this.props.options.length === 0 && this.newOption !== '') {
return this.options.noResultsFoundText
}

return ''
}

const renderOption = optionProps => {
const { OptionCreator } = this.props

Expand All @@ -42,7 +59,8 @@ export class CreatebleOptionsList extends Component {
return html`
<ul>
${this.props.options.map(renderOption)}
${this.shouldRenderEmptyOption() ? renderEmptyOption(this.props.emptyMessage) : this._renderAddOption(this.props.newOption)}
${this.shouldRenderEmptyOption() ? renderEmptyOption(chooseEmptyOptionsMessage()) : ''}
${this._renderAddOption(this.props.newOption)}
</ul>
`
}
Expand Down Expand Up @@ -72,16 +90,15 @@ export class CreatebleOptionsList extends Component {
* Check if component should render empty option
*/
shouldRenderEmptyOption() {
return this.props.options.length === 0 &&
this.props.blockNewEntries
return this.props.options.length === 0
}

/**
* Render add new option
*/
_renderAddOption(newOption) {
const addOptionHtml = this.options.addOptionText
? html`<small class="blip-prompt-add-option">${this.options.addOptionText}</small>`
? html`<span class="blip-prompt-add-option">${this.options.addOptionText}</span>`
: ''

return this._canAddOption(newOption)
Expand Down
3 changes: 3 additions & 0 deletions src/components/blipSelect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class BlipSelect extends Component {
descriptionPosition: 'right', // right || bottom
mode: 'select',
noResultsText: 'No results found',
noResultsFoundText: 'No results found',
clearAfterAdd: true, // Clear input after add new option
onBeforeOpenSelect: () => { },
onAfterOpenSelect: () => { },
Expand Down Expand Up @@ -329,6 +330,8 @@ export class BlipSelect extends Component {
onAddOption: this._handleAddOption.bind(this),
addOptionText: this.configOptions.canAddOptions.text,
alwaysEnabled: this.configOptions.canAddOptions.alwaysEnabled,
noResultsText: this.configOptions.noResultsText,
noResultsFoundText: this.configOptions.noResultsFoundText,
})
}

Expand Down

0 comments on commit a335948

Please sign in to comment.