-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
DefaultPropsProvider
for setting default props in R…
…eact components (#922) * feat: introduce `DefaultPropsProvider` for setting default props in React components * docs(react-docs): Use `DefaultPropsProvider` in the main application file * feat(react/accordion): apply default props * feat(react/alert): apply default props * chore: create changeset `tonic-ui-73.md` * refactor: add input validation for the `useDefaultProps` hook * feat(react/badge): apply default props * feat(react/button): apply default props * feat(react/checkbox): apply default props * refactor(react-docs): use `useConst` to memoize custom theme in `_app.page.js` * feat(react/code): apply default props * feat(react/color-mode): apply default props * feat(react/color-style): apply default props * feat(react/date-pickers): apply default props * feat(react/divider): apply default props * feat(react/flex): apply default props * feat(react/grid): apply default props * feat(react/image): apply default props * feat(react/link): apply default props * feat(react/portal): apply default props * feat(react/icons): apply default props * feat(react/drawer): apply default props * feat(react/modal): apply default props * feat(react/input): apply default props * feat(react/menu): apply default props * feat(react/pagination): apply default props * feat(react/popover): apply default props * chore: eliminate `DefaultPropsProvider` and `useDefaultProps` from named exports * feat(react/progress): apply default props * feat(react/radio): apply default props * feat(react/resize-handle): apply default props * feat(react/scrollbar): apply default props * feat(react/search-input): apply default props * feat(react/select): apply default props * feat(react/skeleton): apply default props * feat(react/space): apply default props * feat(react/spinner): apply default props * feat(react/stack): apply default props * feat(react/switch): apply default props * feat(react/table): apply default props * feat(react/tabs): apply default props * feat(react/tag): apply default props * feat(react/text): apply default props * feat(react/textarea): apply default props * feat(react/toast): apply default props * feat(react/tooltip): apply default props * feat(react/transitions): apply default props * feat(react/tree): apply default props * feat(react/truncate): apply default props * feat(react/visually-hidden): apply default props * refactor(resolveProps): use `Object.entries()` for cleaner object iteration * feat: add a default value to the context * test: add `use-default-props.test.js` to check for component name mismatch in the `useDefaultProps` hook * test: enhance the `use-default-props` test * feat(react/radio): update `useRadioStyle` in `Radio` component to ensure proper context handling before applying styles
- Loading branch information
Showing
157 changed files
with
1,279 additions
and
763 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 @@ | ||
--- | ||
"@tonic-ui/react": minor | ||
--- | ||
|
||
feat: introduce `DefaultPropsProvider` for setting default props in React components |
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,89 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { globSync } from 'glob'; | ||
import { parse } from '@babel/parser'; | ||
import traverse from '@babel/traverse'; | ||
|
||
/** | ||
* Example 1: with `forwardRef` | ||
* ```js | ||
* const Accordion = forwardRef((inProps, ref) => { | ||
* const { | ||
* children, | ||
* ...rest | ||
* } = useDefaultProps({ props: inProps, name: 'Accordion' }); | ||
* `` | ||
* | ||
* Example 2: without `forwardRef` | ||
* ``` | ||
* const Portal = (inProps) => { | ||
* const { | ||
* appendToParentPortal = false, | ||
* children, | ||
* containerRef, | ||
* } = useDefaultProps({ props: inProps, name: 'Portal' }); | ||
* ``` | ||
*/ | ||
test('the `name` property in `useDefaultProps` should match the component name', () => { | ||
const files = globSync([ | ||
path.resolve(__dirname, '../src/*/**/*.js'), | ||
], { | ||
'ignore': [ | ||
path.resolve(__dirname, '../src/*/**/*.test.js'), | ||
], | ||
}).sort(); // Sort files alphabetically | ||
|
||
let matchCount = 0; | ||
let passCount = 0; | ||
|
||
for (const file of files) { | ||
const code = fs.readFileSync(file, { encoding: 'utf-8' }); | ||
|
||
if (code.indexOf(' useDefaultProps(') !== -1) { | ||
++matchCount; | ||
} | ||
|
||
const ast = parse(code, { | ||
sourceType: 'module', | ||
plugins: ['jsx'], | ||
}); | ||
|
||
traverse(ast, { | ||
VariableDeclarator(path) { // eslint-disable-line no-loop-func | ||
if (!path.node.init) { | ||
return; | ||
} | ||
|
||
const isForwardRef = (path.node.init.callee && path.node.init.callee.name === 'forwardRef'); | ||
const isFunctionOrArrowFunctionExpression = (path.node.init.type === 'ArrowFunctionExpression' || path.node.init.type === 'FunctionExpression'); | ||
|
||
if (isForwardRef || isFunctionOrArrowFunctionExpression) { | ||
const componentName = path.node.id.name; | ||
path.traverse({ | ||
CallExpression(innerPath) { | ||
if (innerPath.node.callee.name === 'useDefaultProps') { | ||
const nameProperty = innerPath.node.arguments[0].properties.find(prop => prop.key.name === 'name'); | ||
const namePropertyValue = nameProperty?.value?.value; | ||
if (!namePropertyValue) { | ||
console.error(`Error: No 'name' property found in 'useDefaultProps' in file "${file}"`); | ||
return; | ||
} | ||
|
||
try { | ||
expect(namePropertyValue).toEqual(componentName); | ||
} catch (err) { | ||
throw new Error(`Mismatch in file "${file}": Expected component name '${componentName}' but found '${namePropertyValue}'`); | ||
} | ||
|
||
passCount++; | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
expect(matchCount).toBeGreaterThan(0); | ||
expect(matchCount).toEqual(passCount); | ||
}); |
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
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
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
Oops, something went wrong.