-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: (block-editor-utils) Add RangeField (#1575)
* Feat: (block-editor-utils) Add RangeField * Chore: Changeset * Deps: (block-editor-utils) Update package.json
- Loading branch information
Showing
11 changed files
with
518 additions
and
15,294 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: Add `RangeControl` field in `block-editor-utils`. |
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as React from 'react'; | ||
import { RangeControl } from '@wordpress/components'; | ||
import { ControlProps, RangeField } from '../types/index.js'; | ||
|
||
function Range<T extends Record<string, any>>({ | ||
config, | ||
props, | ||
}: ControlProps<T>) { | ||
const onChange = (newContent: number | undefined) => { | ||
props.setAttributes({ [config.name]: newContent }); | ||
}; | ||
const params: RangeField = { | ||
...config, | ||
control: 'range', | ||
}; // TODO use satisfies operator when using TS v5 | ||
return ( | ||
<RangeControl | ||
label={params.label} | ||
onChange={onChange} | ||
value={props.attributes[config.name]} | ||
min={params?.min} | ||
max={params?.max} | ||
/> | ||
); | ||
} | ||
|
||
export default Range; |
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,49 @@ | ||
import React from 'react'; | ||
import { screen, render } from '@testing-library/react'; | ||
import Range from '../../src/controls/Range'; | ||
import { Field } from '../../src/types'; | ||
|
||
const config: Field = { | ||
label: 'rangeField', | ||
name: 'rangeField', | ||
type: 'number', | ||
control: 'range', | ||
location: 'inspector', | ||
min: 0, | ||
max: 50, | ||
}; | ||
const props = { | ||
setAttributes: jest.fn(), | ||
attributes: { | ||
rangeField: 20, | ||
}, | ||
}; | ||
|
||
const setup = () => { | ||
const utils = render(<Range config={config} props={props} />); | ||
const input = screen.findAllByDisplayValue(props.attributes.rangeField!); | ||
return { | ||
input, | ||
...utils, | ||
}; | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('RangeField', () => { | ||
it('should mount', () => { | ||
const { input } = setup(); | ||
expect(input).toBeTruthy(); | ||
}); | ||
|
||
it('should have correct display value from props', () => { | ||
setup(); | ||
expect( | ||
screen.getByRole('spinbutton', { | ||
name: /rangefield/i, | ||
}) | ||
).toHaveValue(props.attributes.rangeField); | ||
}); | ||
}); |
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,13 @@ | ||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), // Deprecated | ||
removeListener: jest.fn(), // Deprecated | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})), | ||
}); |
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,14 +1,14 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"outDir": "dist/mjs", | ||
"target": "es2017", | ||
"rootDir": "src", | ||
"jsx": "react", | ||
"types": ["node", "jest"], | ||
"typeRoots": ["./node_modules/@types", "./src/types"] | ||
}, | ||
"exclude": ["node_modules", "dist"], | ||
"include": ["src"] | ||
} | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"outDir": "dist/mjs", | ||
"target": "es2017", | ||
"rootDir": "src", | ||
"jsx": "react", | ||
"types": ["node", "jest"], | ||
"typeRoots": ["./node_modules/@types", "./src/types"] | ||
}, | ||
"exclude": ["node_modules", "dist"], | ||
"include": ["src", "./jest-setup.ts"] | ||
} |
ee8c08e
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.