Skip to content

Commit

Permalink
[form-builder] Fix bug causing editing slugs in array to have no effe…
Browse files Browse the repository at this point in the history
…ct (#648)
  • Loading branch information
bjoerge committed Mar 6, 2018
1 parent 82578ea commit 0b47ac2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/@sanity/form-builder/src/inputs/Slug/SlugInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default withDocument(
}),
document: PropTypes.shape({_id: PropTypes.string}).isRequired,
onChange: PropTypes.func,
onFocus: PropTypes.func,
markers: PropTypes.arrayOf(
PropTypes.shape({
type: PropTypes.string.isRequired
Expand All @@ -55,14 +56,14 @@ export default withDocument(
this._isMounted = false
}

updateValue(value) {
if (!value) {
this.props.onChange(PatchEvent.from(unset()))
updateCurrent(current) {
const {onChange, type} = this.props
if (!current) {
onChange(PatchEvent.from(unset(['current'])))
return
}

this.props.onChange(
PatchEvent.from(setIfMissing({_type: this.props.type.name}), set(value, ['current']))
onChange(
PatchEvent.from(setIfMissing({_type: type.name}), set(current, ['current']))
)
}

Expand Down Expand Up @@ -98,7 +99,11 @@ export default withDocument(
}

handleChange = event => {
this.updateValue(event.target.value.toString())
this.updateCurrent(event.target.value)
}

handleFocusCurrent = event => {
this.props.onFocus(['current'])
}

handleGenerateSlug = () => {
Expand All @@ -114,7 +119,7 @@ export default withDocument(
const newFromSource = typeof source === 'function' ? source(document) : get(document, source)
this.setState({loading: true})
this.slugify(newFromSource || '')
.then(newSlug => this.updateValue(newSlug))
.then(newSlug => this.updateCurrent(newSlug))
.catch(err => {
// eslint-disable-next-line no-console
console.error(
Expand All @@ -125,7 +130,7 @@ export default withDocument(
}

render() {
const {value, type, level, markers, document, ...rest} = this.props
const {value, type, level, markers, onFocus, document, ...rest} = this.props
const {loading, inputText} = this.state

const hasSourceField = type.options && type.options.source
Expand Down Expand Up @@ -154,6 +159,7 @@ export default withDocument(
disabled={loading}
placeholder={type.placeholder}
onChange={this.handleChange}
onFocus={this.handleFocusCurrent}
value={typeof inputText === 'string' ? inputText : value.current}
/>
</div>
Expand Down
12 changes: 12 additions & 0 deletions packages/test-studio/schemas/slugs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ export default {
!/^hei/i.test(value) && options.defaultIsUnique(value, options)
}
},
{
name: 'arrayOfSlugs',
type: 'array',
of: [
{
options: {
source: 'title'
},
type: 'slug'
}
]
},
{
name: 'deprecatedSlugifyFnField',
type: 'slug',
Expand Down

0 comments on commit 0b47ac2

Please sign in to comment.