Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # resq (React Element Selector Query) ![npm](https://img.shields.io/npm/v/resq.svg) [![Build Status](https://travis-ci.org/baruchvlz/resq.svg?branch=master)](https://travis-ci.org/baruchvlz/resq) [![codecov](https://codecov.io/gh/baruchvlz/resq/branch/master/graph/badge.svg)](https://codecov.io/gh/baruchvlz/resq)
  2. ## Requirements
  3. - React v16 or higher
  4. - Node 8 or higher
  5. - React DevTools (optional)
  6. This library tries to implement something similar to `querySelector` and `querySelectorAll`, but through the React VirtualDOM. You can query for React composite elements or HTML elements. It provides two functions `resq$` and `resq$$` for selecting one or multiple components, respectively.
  7. ## Installation
  8. ```
  9. $ npm install --save resq
  10. $ yarn add resq
  11. ```
  12. ## Usage
  13. To get the most out of the library, we recommend you use React Dev Tools to verify the component names you want to select. Granted for basic usage you don't need this as long as you know the component name beforehand, but for Styled components and MaterialUI components it will be of great help.
  14. #### Type definition
  15. ```typescript
  16. interface RESQNode {
  17. name: 'MyComponent',
  18. node: HTMLElement | null,
  19. isFragment: boolean,
  20. state: string | boolean | any[] | {},
  21. props: {},
  22. children: RESQNode[]
  23. }
  24. resq$(selector: string, element?: HTMLElement): RESQNode
  25. resq$$(selector: string, element?: HTMLElement): Array<RESQNode>
  26. ```
  27. * [Basic Usage](README.md#basic-usage)
  28. * [Wildcard selection](README.md#wildcard-selection)
  29. * [Async selection](README.md#async-selection)
  30. * [Filtering selection](README.md#filtering-selection)
  31. #### Basic Usage
  32. Take this React App:
  33. ```jsx
  34. // imports
  35. const MyComponent = () => (
  36. <div>My Component</div>
  37. )
  38. const App = () => (
  39. <div><MyComponent /></div>
  40. )
  41. ReactDOM.render(<App />, document.getElementById('root'))
  42. ```
  43. Selecting `MyComponent`:
  44. ```js
  45. import { resq$ } from 'resq'
  46. const root = document.getElementById('root');
  47. resq$('MyComponent', root);
  48. /*
  49. {
  50. name: 'MyComponent',
  51. node: <div />,
  52. isFragment: false,
  53. state: {},
  54. props: {},
  55. children: []
  56. }
  57. */
  58. ```
  59. #### Wildcard selection
  60. You can select your components by partial name use a wildcard selectors:
  61. ```jsx
  62. // imports
  63. const MyComponent = () => (
  64. <div>My Component</div>
  65. )
  66. const MyAnotherComponent = () => (
  67. <div>My Another Component</div>
  68. )
  69. const App = () => (
  70. <div>
  71. <MyComponent />
  72. <MyAnotherComponent />
  73. </div>
  74. )
  75. ReactDOM.render(<App />, document.getElementById('root'))
  76. ```
  77. Selecting both components by wildcard:
  78. ```js
  79. import { resq$$ } from 'resq'
  80. const root = document.getElementById('root');
  81. resq$$('My*', root);
  82. /*
  83. [
  84. {
  85. name: 'MyComponent',
  86. node: <div />,
  87. isFragment: false,
  88. state: {},
  89. props: {},
  90. children: []
  91. },
  92. {
  93. name: 'MyAnotherComponent',
  94. node: <div />,
  95. isFragment: false,
  96. state: {},
  97. props: {},
  98. children: []
  99. },
  100. ]
  101. */
  102. ```
  103. Selecting `MyAnotherComponent` by wildcard:
  104. ```js
  105. import { resq$ } from 'resq'
  106. const root = document.getElementById('root');
  107. resq$('My*Component', root);
  108. /*
  109. {
  110. name: 'MyAnotherComponent',
  111. node: <div />,
  112. isFragment: false,
  113. state: {},
  114. props: {},
  115. children: []
  116. }
  117. */
  118. ```
  119. #### Async selection
  120. Going by the same example as in [basic usage](README.md#basic-usage), if you don't want to pass the root element to the function, you can do it this way:
  121. ```js
  122. import { resq$, waitToLoadReact } from 'resq'
  123. async function getReactElement(name) {
  124. try {
  125. await waitToLoadReact(2000) // time in MS to wait before erroring
  126. return resq$(name)
  127. } catch (error) {
  128. console.warn('resq error', error)
  129. }
  130. }
  131. getReactElement('MyComponent')
  132. ```
  133. #### Filtering selection
  134. You can filter your selections `byState` or `byProps`. These are methods attached to the RESQNode return objects.
  135. Example app:
  136. ```jsx
  137. // imports
  138. const MyComponent = ({ someBooleanProp }) => (
  139. <div>My Component {someBooleanProp ? 'show this' : ''} </div>
  140. )
  141. const App = () => (
  142. <div>
  143. <MyComponent />
  144. <MyComponent someBooleanProp={true} />
  145. </div>
  146. )
  147. ReactDOM.render(<App />, document.getElementById('root'))
  148. ```
  149. To select the first instance of `MyComponent` where `someBooleanProp` is true:
  150. ```js
  151. import { resq$ } from 'resq'
  152. const root = document.getElementById('root')
  153. const myComponent = resq$('MyComponent', root)
  154. const filtered = myComponent.byProps({ someBooleanProp: true })
  155. console.log(filtered)
  156. /*
  157. {
  158. name: 'MyComponent',
  159. node: <div />,
  160. isFragment: false,
  161. state: {},
  162. props: {
  163. someBooleanProp: true,
  164. },
  165. children: []
  166. }
  167. */
  168. ```
  169. **Deep Matching with `exact` flag**
  170. If you are in need of filtering `byProps` or `byState` and require the filter to match exactly every property and value in the object (or nested objects), you can pass the `exact` flag to the function:
  171. ```js
  172. import { resq$ } from 'resq'
  173. const root = document.getElementById('root')
  174. const myComponent = resq$('MyComponent', root)
  175. const filtered = myComponent.byProps({ someBooleanProp: true }, { exact: true })
  176. ```