-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from bangank36/feat/colorPalette-implementation
WP-Builder: Feat/colorPalette-implementation
- Loading branch information
Showing
5 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,60 @@ | ||
import React from "react"; | ||
import isEmpty from 'lodash/isEmpty'; | ||
import { withJsonFormsControlProps } from "@jsonforms/react"; | ||
import { rankWith, isStringControl, and, optionIs } from "@jsonforms/core"; | ||
import { ColorPalette, SlotFillProvider, Popover } from '@wordpress/components'; | ||
import { useState } from '@wordpress/element'; | ||
|
||
const TextControl = (props) => { | ||
const { | ||
id, | ||
description, | ||
errors, | ||
label, | ||
uischema, | ||
path, | ||
visible, | ||
required, | ||
config, | ||
data, | ||
input, | ||
handleChange | ||
} = props; | ||
|
||
const colors = uischema.options.colors || [ | ||
{ name: 'red', color: '#f00' }, | ||
{ name: 'white', color: '#fff' }, | ||
{ name: 'blue', color: '#00f' }, | ||
]; | ||
|
||
return ( | ||
<SlotFillProvider> | ||
<ColorPalette | ||
colors={ colors } | ||
value={ data } | ||
onChange={ ( value ) => | ||
handleChange(path, value === '' ? undefined : value) | ||
} | ||
/> | ||
<Popover.Slot /> | ||
</SlotFillProvider> | ||
) | ||
}; | ||
|
||
const optionIsNotEmpty = | ||
(optionName) => | ||
(uischema) => { | ||
if (isEmpty(uischema)) { | ||
return false; | ||
} | ||
|
||
const options = uischema.options; | ||
return !isEmpty(options) && !isEmpty(options[optionName]); | ||
}; | ||
|
||
export const colorPaletteControlTester = rankWith( | ||
6, //increase rank as needed | ||
and(isStringControl, optionIs('format', 'color'), optionIsNotEmpty('colors')) | ||
); | ||
|
||
export default withJsonFormsControlProps(TextControl); |