-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fields: Refactor fields abstraction to allow nested fields
- Loading branch information
1 parent
2a836da
commit 7959e87
Showing
15 changed files
with
382 additions
and
417 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { map } from "lodash"; | ||
|
||
import { compose } from "@wordpress/element"; | ||
import { withSelect } from "@wordpress/data"; | ||
import { withInstanceId } from "@wordpress/components"; | ||
import { __ } from "@wordpress/i18n"; | ||
|
||
function FieldForm({ | ||
field, | ||
instanceId, | ||
onChange, | ||
fieldType, | ||
availableFieldTypes | ||
}) { | ||
const id = `gcf-field-${instanceId}`; | ||
|
||
const onChangeProperty = property => { | ||
return event => { | ||
const value = event.target.value; | ||
const newField = { | ||
...field, | ||
[property]: value | ||
}; | ||
onChange(newField); | ||
}; | ||
}; | ||
|
||
const onBlurFieldName = event => { | ||
const value = event.target.value; | ||
if (!!field.title) { | ||
return; | ||
} | ||
const newField = { | ||
...field, | ||
title: value | ||
}; | ||
onChange(newField); | ||
}; | ||
|
||
return ( | ||
<div key={field.id} className="gcf-fields__field-form"> | ||
<div> | ||
<label htmlFor={`template-fields-name-${field.id}-${instanceId}`}> | ||
{__("Name", "gutenberg-custom-fields")} | ||
</label> | ||
<input | ||
type="text" | ||
id={`template-fields-name-${field.id}-${instanceId}`} | ||
value={field.name || ""} | ||
onChange={onChangeProperty("name")} | ||
onBlur={onBlurFieldName} | ||
/> | ||
</div> | ||
|
||
<div> | ||
<label htmlFor={`template-fields-title-${field.id}-${instanceId}`}> | ||
{__("Title", "gutenberg-custom-fields")} | ||
</label> | ||
<input | ||
type="text" | ||
id={`template-fields-title-${field.id}-${instanceId}`} | ||
value={field.title || ""} | ||
onChange={onChangeProperty("title")} | ||
/> | ||
</div> | ||
|
||
<div> | ||
<label htmlFor={`template-fields-type-${field.id}-${instanceId}`}> | ||
{__("Type", "gutenberg-custom-fields")} | ||
</label> | ||
<select | ||
id={`template-fields-type-${field.id}-${instanceId}`} | ||
value={field.type} | ||
onChange={onChangeProperty("type")} | ||
> | ||
{map(availableFieldTypes, fieldType => ( | ||
<option key={fieldType.name} value={fieldType.name}> | ||
{fieldType.label} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
|
||
{fieldType.configForm && | ||
fieldType.configForm({ field, instanceId, onChange })} | ||
</div> | ||
); | ||
} | ||
|
||
export default compose([ | ||
withSelect((select, { field }) => ({ | ||
availableFieldTypes: select("gcf/fields").all(), | ||
fieldType: select("gcf/fields").get(field.type) | ||
})), | ||
withInstanceId | ||
])(FieldForm); |
Oops, something went wrong.