-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat: (editor-utils) NumberField component.
- Loading branch information
1 parent
1bb5e94
commit 457933b
Showing
6 changed files
with
92 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@faustwp/block-editor-utils': patch | ||
--- | ||
|
||
Feat: Handle NumberControl fields in the block-editor-utils. |
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,22 @@ | ||
import * as React from 'react'; | ||
import { TextControl as NumberControl } from '@wordpress/components'; | ||
import { ControlProps } from '../types/index.js'; | ||
|
||
function NumberField<T extends Record<string, any>>({ | ||
config, | ||
props, | ||
}: ControlProps<T>) { | ||
const onChange = (newContent: string) => { | ||
props.setAttributes({ [config.name]: Number(newContent) }); | ||
}; | ||
return ( | ||
<NumberControl | ||
type="number" | ||
label={config.label} | ||
value={props.attributes[config.name]} | ||
onChange={onChange} | ||
/> | ||
); | ||
} | ||
|
||
export default NumberField; |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import registerControl from '../helpers/registerControl.js'; | ||
import Text from './Text.js'; | ||
import NumberField from './Number.js'; | ||
import Color from './Color.js'; | ||
|
||
registerControl('text', Text); | ||
registerControl('number', NumberField); | ||
registerControl('color', Color); |
43 changes: 43 additions & 0 deletions
43
packages/block-editor-utils/tests/controls/Number.test.tsx
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,43 @@ | ||
import React from 'react'; | ||
import { screen, render } from '@testing-library/react'; | ||
import NumberField from '../../src/controls/Number'; | ||
import { Field } from '../../src/types'; | ||
|
||
const config: Field = { | ||
label: 'numberField', | ||
name: 'numberField', | ||
type: 'number', | ||
control: 'number', | ||
location: 'inspector', | ||
}; | ||
const props = { | ||
setAttributes: jest.fn(), | ||
attributes: { | ||
numberField: 634571, | ||
}, | ||
}; | ||
|
||
const setup = () => { | ||
const utils = render(<NumberField config={config} props={props} />); | ||
const input = screen.findAllByDisplayValue(props.attributes.numberField!); | ||
return { | ||
input, | ||
...utils, | ||
}; | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('NumberField', () => { | ||
it('should mount', () => { | ||
const { input } = setup(); | ||
expect(input).toBeTruthy(); | ||
}); | ||
|
||
it('should have correct display value from props', () => { | ||
setup(); | ||
expect(screen.getByDisplayValue(props.attributes.numberField)).toBeTruthy(); | ||
}); | ||
}); |
457933b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check out the recent updates to your Atlas environment:
Learn more about building on Atlas in our documentation.